之前利用Aspose.Cells.dll插件生成Excel表格信息,缺失標題欄信息,先在原先方法進行修改
不知道可訪問上一篇查看:https://www.cnblogs.com/xielong/p/13597757.html
直接上代碼,輔助方法
public static class ExportHelper { /// <summary> /// DataTable數據生成Excel文件(調用第三方Aspose.Cells.dll插件) /// </summary> /// <param name="data">導入Excel的dataTable數據源</param> /// <param name="filepath">保存的文件路徑</param> public static void CreateExcelByAsposeCells(DataTable data, string filepath) { try { //Workbook book = new Workbook("E:\\test.xlsx"); //打開工作簿 Workbook book = new Workbook(); //創建工作簿 Worksheet sheet = book.Worksheets[0]; //創建工作表 Cells cells = sheet.Cells; //單元格 //創建 列名行的樣式 Style style = book.Styles[book.Styles.Add()]; style.Borders[BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 左邊界線 style.Borders[BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 右邊界線 style.Borders[BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 上邊界線 style.Borders[BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 下邊界線 style.HorizontalAlignment = TextAlignmentType.Center; //單元格內容的水平對齊方式文字居中 style.Font.Name = "宋體"; //字體 style.Font.IsBold = true; //設置粗體 style.Font.Size = 12; //設置字體大小 style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色 style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景樣式 style.IsTextWrapped = true; //單元格內容自動換行 //創建 數據行的樣式 Style conStyle = book.Styles[book.Styles.Add()]; conStyle.Borders[BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 左邊界線 conStyle.Borders[BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 右邊界線 conStyle.Borders[BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 上邊界線 conStyle.Borders[BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 下邊界線 conStyle.Font.Name = "宋體"; conStyle.HorizontalAlignment = TextAlignmentType.Left; //單元格內容的水平對齊方式文字居中 conStyle.Font.IsBold = false; conStyle.Font.Size = 11; //表格填充數據 int Colnum = data.Columns.Count;//表格列數 int Rownum = data.Rows.Count;//表格行數 //生成行 列名行 for (int i = 0; i < Colnum; i++) { cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表頭 cells[0, i].SetStyle(style); //添加樣式 //cells.SetColumnWidth(i, data.Columns[i].ColumnName.Length * 2 + 1.5); //自定義列寬 //cells.SetRowHeight(0, 30); //自定義高 } //生成數據行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加數據 cells[1 + i, k].SetStyle(conStyle); //添加樣式 style } } sheet.AutoFitColumns(); //自適應寬 book.Save(filepath); //保存 GC.Collect(); } catch (Exception e) { //logger.Error("生成excel出錯:" + e.Message); } } /// <summary> /// DataTable數據生成Excel文件,包含標題名(調用第三方Aspose.Cells.dll插件) /// </summary> /// <param name="data">導入Excel的dataTable數據源</param> /// <param name="filepath">保存的文件路徑</param> /// <param name="excelTitle">excel標題名稱</param> public static void CreateExcelByAsposeCells(DataTable data, string filepath,string excelTitle) { try { Workbook book = new Workbook(); //創建工作簿 Worksheet sheet = book.Worksheets[0]; //創建工作表 Cells cells = sheet.Cells; //單元格 //創建標題行的樣式 Style titleStyle = book.Styles[book.Styles.Add()]; titleStyle.HorizontalAlignment = TextAlignmentType.Center; //單元格內容的水平對齊方式文字居中 titleStyle.Font.Name = "宋體"; //字體 titleStyle.Font.IsBold = true; //設置粗體 titleStyle.Font.Size = 15; //設置字體大小 titleStyle.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色(黃綠色) titleStyle.Pattern = Aspose.Cells.BackgroundType.Solid; //背景樣式 titleStyle.IsTextWrapped = true; //單元格內容自動換行 titleStyle.IsJustifyDistributed = true; //創建 列名行的樣式 Style style = book.Styles[book.Styles.Add()]; style.Borders[BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 左邊界線 style.Borders[BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 右邊界線 style.Borders[BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 上邊界線 style.Borders[BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 下邊界線 style.HorizontalAlignment = TextAlignmentType.Center; //單元格內容的水平對齊方式文字居中 style.ForegroundColor = System.Drawing.Color.FromArgb(230, 230, 250); //背景色(淡紫色) style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景樣式 style.Font.Name = "宋體"; //字體 style.Font.IsBold = true; //設置粗體 style.Font.Size = 12; //設置字體大小 //創建 數據行的樣式 Style conStyle = book.Styles[book.Styles.Add()]; conStyle.Borders[BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 左邊界線 conStyle.Borders[BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 右邊界線 conStyle.Borders[BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 上邊界線 conStyle.Borders[BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 下邊界線 conStyle.Font.Name = "宋體"; conStyle.HorizontalAlignment = TextAlignmentType.Left; //單元格內容的水平對齊方式文字居中 conStyle.Font.IsBold = false; conStyle.Font.Size = 11; //表格填充數據 int Colnum = data.Columns.Count;//表格列數 int Rownum = data.Rows.Count;//表格行數 //生成標題行 for (int t = 0; t < Colnum; t++) { cells[0, t].PutValue(excelTitle); //添加表頭 cells[0, t].SetStyle(titleStyle); //添加樣式 } //合併第一行的所有列 cells.Merge(0, 0, 1, Colnum); //生成列名行 for (int i = 0; i < Colnum; i++) { cells[1, i].PutValue(data.Columns[i].ColumnName); //添加表頭 cells[1, i].SetStyle(style); //添加樣式 } //生成數據行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1+1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加數據 cells[1+1 + i, k].SetStyle(conStyle); //添加樣式 style } } sheet.AutoFitColumns(); //自適應寬 book.Save(filepath); //保存 GC.Collect(); } catch (Exception e) { } } }
代碼調用方法
/// <summary> /// 將DataTable轉成Excel文件 /// </summary> public void ExportExcel() { //獲取tab數據 DataTable tabs = GetDataTableDemo(); //執行導出的數據 // ExportHelper.CreateExcelByAsposeCells(tabs, "D:\\Excel導出測試.xlsx"); //執行導出的數據(帶標題) ExportHelper.CreateExcelByAsposeCells(tabs, "D:\\Excel導出測試(標題).xlsx","高一(3)班信息表"); }
運行生成效果圖
附上Demo下載地址:
鏈接:https://pan.baidu.com/s/1Q1KpW3YydU_Va1_5NFMZNA
提取碼:1234