網(wǎng)站首頁 編程語言 正文
實現(xiàn)DataTable轉(zhuǎn)為Excel文件,和上次分享的Excel文件轉(zhuǎn)為DataTable互為反操作。DataTable轉(zhuǎn)化為Excel文件是通過傳入一個DataTable類型的參數(shù),然后將傳入的DataTable轉(zhuǎn)化為Excel文件。實現(xiàn)關(guān)鍵代碼如下:
方法一:
////// DataTable數(shù)據(jù)導(dǎo)出Excel /// /// /// public static void DataTableExport(DataTable data, string filepath) { try { //Workbook book = new Workbook("E:\\test.xlsx"); //打開工作簿 Workbook book = new Workbook(); //創(chuàng)建工作簿 Worksheet sheet = book.Worksheets[0]; //創(chuàng)建工作表 Cells cells = sheet.Cells; //單元格 //創(chuàng)建樣式 Aspose.Cells.Style style = book.Styles[book.Styles.Add()]; style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 左邊界線 style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 右邊界線 style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 上邊界線 style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 下邊界線 style.HorizontalAlignment = TextAlignmentType.Center; //單元格內(nèi)容的水平對齊方式文字居中 style.Font.Name = "宋體"; //字體 style1.Font.IsBold = true; //設(shè)置粗體 style.Font.Size = 11; //設(shè)置字體大小 //style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色 //style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景樣式 //style.IsTextWrapped = true; //單元格內(nèi)容自動換行 //表格填充數(shù)據(jù) int Colnum = data.Columns.Count;//表格列數(shù) int Rownum = data.Rows.Count;//表格行數(shù) //生成行 列名行 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); //自定義高 } //生成數(shù)據(jù)行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加數(shù)據(jù) cells[1 + i, k].SetStyle(style); //添加樣式 } cells[1 + i, 5].Formula = "=B" + (2 + i) + "+C" + (2 + i);//給單元格設(shè)置計算公式,計算班級總?cè)藬?shù) } sheet.AutoFitColumns(); //自適應(yīng)寬 book.Save(filepath); //保存 GC.Collect(); } catch (Exception e) { logger.Error("生成excel出錯:" + e.Message); } }
方法二:
public void ToExcel(DataTable dt) { #region 3s var FilePath = @"D:\ToExcel\";//產(chǎn)生Excel文件路徑 DeleteFile(FilePath); //創(chuàng)建全新的Workbook var workbook = new HSSFWorkbook();//一個sheet最多65536行 var count = 0; for (double i = 0; i < Convert.ToDouble(dt.Rows.Count) / Convert.ToDouble(65534); i++)//每個Excel文件的一個頁簽只能存放65536行數(shù)據(jù) { var row_index = 0; //創(chuàng)建Sheet workbook.CreateSheet("Sheet" + i); //根據(jù)Sheet名字獲得Sheet對象 var sheet = workbook.GetSheet("Sheet" + i); IRow row; row = sheet.CreateRow(row_index); //寫入標(biāo)題 for (int j = 0; j < dt.Columns.Count; j++) { row.CreateCell(j).SetCellValue(dt.Columns[j].Caption.ToString()); } row = sheet.CreateRow(++row_index); //寫入數(shù)據(jù) for (int j = 0; j < (dt.Rows.Count - count > 65534 ? 65534 : dt.Rows.Count - count); j++) { var r = dt.Rows[j + count]; for (int k = 0; k
原文鏈接:https://www.cnblogs.com/wml-it/p/12149040.html
相關(guān)推薦
- 2022-10-30 詳解Python?中的命名空間、變量和范圍_python
- 2022-11-14 Python中__init__.py文件的作用_python
- 2022-12-15 深入了解Golang中的格式化輸出_Golang
- 2022-08-02 源碼解析python中randint函數(shù)的效率缺陷_python
- 2023-02-10 python中以函數(shù)作為參數(shù)(回調(diào)函數(shù))的實現(xiàn)方法_python
- 2022-01-10 修改代碼后,刷新頁面沒有更新的解決辦法。Disable cache禁止
- 2024-01-27 ioc,ioc實際運(yùn)用
- 2022-06-12 Python使用matplotlib.pyplot?as?plt繪圖圖層優(yōu)先級問題_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支