/// <summary> /// DataTable數據表保存至Excel (合並行報表)【年度日報表】 /// </summary> /// <param name="dt"></param> /// <param name="filePath"></param> /// <param name="excelParm"></param> public static void ToExcel1(DataTable dt, string filePath, ExcelParm excelParm) { string unitId = Tools.GetAppSetting("UnitId"); string subTitle = string.Empty; if (!string.IsNullOrEmpty(excelParm.GrossWorker)) { subTitle += "二次司磅員:" + excelParm.GrossWorker + " "; } if (!string.IsNullOrEmpty(excelParm.CustomerName)) { subTitle += "客戶名稱:" + excelParm.CustomerName + " "; } if (!string.IsNullOrEmpty(excelParm.MaterialName)) { subTitle += "物料名稱:" + excelParm.MaterialName + " "; } if (!string.IsNullOrEmpty(excelParm.CarNumber)) { subTitle += "車號:" + excelParm.CarNumber + " "; } if (!string.IsNullOrEmpty(excelParm.ContractCode)) { subTitle += "合同號:" + excelParm.ContractCode + " "; } if (!string.IsNullOrEmpty(excelParm.Datatime1) && !string.IsNullOrEmpty(excelParm.Datatime2)) { subTitle += "日期:" + excelParm.Datatime1 + " 至 " + excelParm.Datatime2; } //新建工作簿 Workbook wb = new Workbook(); //新建工作表 Worksheet ws = wb.Worksheets[0]; ws.Name = dt.TableName; int rowIndex = 3; int colIndex = 0; int colCount = dt.Columns.Count; int rowCount = dt.Rows.Count; ws.Cells.SetRowHeight(rowIndex, 25);//設置行高 //創建樣式 Style style = wb.Styles[wb.Styles.Add()];//新增樣式 style.HorizontalAlignment = TextAlignmentType.Center; //單元格內容的水平對齊方式文字居中 style.Font.Name = "宋體"; //字體 style.Font.IsBold = true; //設置粗體 //style.Font.Color = Color.White;//設置字體顏色 style.Font.Size = 10; //設置字體大小 //style.ForegroundColor = Color.FromArgb(0, 196, 180); //背景色 style.Pattern = BackgroundType.Solid; style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.TopBorder].Color = Color.Black; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].Color = Color.Black; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.LeftBorder].Color = Color.Black; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].Color = Color.Black; //列名的處理 for (int i = 0; i < colCount; i++) { ws.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName); ws.Cells[rowIndex, colIndex].SetStyle(style);//給單元格關聯樣式 colIndex++; } Style style2 = wb.Styles[wb.Styles.Add()];//新增樣式 style2.Font.Name = "宋體";//文字字體 style2.Font.Size = 10;//文字大小 style2.ShrinkToFit = true; style2.VerticalAlignment = TextAlignmentType.Center; style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].Color = Color.Black; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].Color = Color.Black; style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.LeftBorder].Color = Color.Black; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].Color = Color.Black; Style style3 = wb.Styles[wb.Styles.Add()];//組織標題樣式 style3.Font.Name = "宋體";//文字字體 style3.Font.Size = 16;//文字大小 style3.HorizontalAlignment = TextAlignmentType.Center; ws.Cells.SetRowHeight(0, 25);//設置行高 ws.Cells.Merge(0, 0, 1, dt.Columns.Count); ws.Cells[0, 0].PutValue(excelParm.OrganName); ws.Cells[0, 0].SetStyle(style3); Style style5 = wb.Styles[wb.Styles.Add()];//標題樣式 style5.Font.Name = "宋體";//文字字體 style5.Font.IsBold = true; //設置粗體 style5.Font.Size = 14;//文字大小 style5.HorizontalAlignment = TextAlignmentType.Center; ws.Cells.SetRowHeight(1, 25);//設置行高 ws.Cells.Merge(1, 0, 1, dt.Columns.Count); ws.Cells[1, 0].PutValue(dt.TableName); ws.Cells[1, 0].SetStyle(style5); Style style4 = wb.Styles[wb.Styles.Add()];//新增查詢條件標題樣式 style4.Font.Name = "宋體";//文字字體 style4.Font.IsBold = true; //設置粗體 style4.Font.Size = 10;//文字大小 ws.Cells.SetRowHeight(2, 25);//設置行高 ws.Cells.Merge(2, 0, 1, dt.Columns.Count); ws.Cells[2, 0].PutValue(subTitle); ws.Cells[2, 0].SetStyle(style4); rowIndex++; for (int i = 0; i < rowCount; i++) { ws.Cells.SetRowHeight(rowIndex, 25);//設置行高 colIndex = 0; for (int j = 0; j < colCount; j++) { ws.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString() == "" ? null : dt.Rows[i][j].ToString()); style2.ForegroundColor = Color.White; style2.Pattern = BackgroundType.Solid; ws.Cells[rowIndex, colIndex].SetStyle(style2);//給單元格關聯樣式 colIndex++; } rowIndex++; } //設置所有列為自適應列寬 ws.AutoFitColumns(); for (int col = 0; col < colCount; col++) { ws.Cells.SetColumnWidthPixel(col, ws.Cells.GetColumnWidthPixel(col) + 20); } #region 合並單元格 int mergeDateStart = 4; int mergeDate = 1; int mergeAreaStart = 4; int mergeArea = 1; for (var i = 0; i < dt.Rows.Count; i++) { if (i + 1 < dt.Rows.Count) { //日期 if (dt.Rows[i]["日期"].ToString() != dt.Rows[i + 1]["日期"].ToString()) { ws.Cells.Merge(mergeDateStart, 0, mergeDate, 1); mergeDateStart += mergeDate; mergeDate = 1; } else { mergeDate++; } //區域 if (dt.Rows[i]["區域"].ToString() != dt.Rows[i + 1]["區域"].ToString()) { ws.Cells.Merge(mergeAreaStart, 1, mergeArea, 1); mergeAreaStart += mergeArea; mergeArea = 1; } else { mergeArea++; } } else { //日期 ws.Cells.Merge(mergeDateStart, 0, mergeDate, 1); //區域 ws.Cells.Merge(mergeAreaStart, 1, mergeArea, 1); } } #endregion string fullUpLoadPath = HttpContext.Current.Server.MapPath("~/UpLoad/Excel/"); //檢查本地上傳的物理路徑是否存在,不存在則創建 if (!System.IO.Directory.Exists(fullUpLoadPath)) { System.IO.Directory.CreateDirectory(fullUpLoadPath); } filePath = GetMapPath(filePath); if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath); System.IO.FileStream fs = System.IO.File.Create(filePath); fs.Close(); wb.Save(filePath); }