網站首頁 編程語言 正文
在工作開發中,客戶經常要求數據庫中數據導出到Excel表格。以前方法是引用office相關組件,如果客戶沒有安裝office,功能就會遇到問題。
現在用Npoi導出Excel,導出表格是合并行列,如圖:
導出的要求:合計列要進行合并,序號一致的要合并。最后一行要合并列。
因為相同序號數量不是固定的,要動態算合并的行數。
合并行列接口:XXX.AddMergedRegion(new CellRangeAddress(開始行, 最后一行, 開始列, 最后一列));
隱藏指定:sheet.SetColumnHidden(cellIndex, true);
引用組件:
NPOI.dll;
NPOI.OOXML.dll;
NPOI.OpenXml4Net.dll;
NPOI.OpenXmlFormats.dll;
ICSharpCode.SharpZipLib.dll;
代碼如下:
//////? /// /// 數據源 /// 保存路徑 /// 序號 public void Export(DataTable dtSource,string strFileName,DataView dvXH=null) ? ? ? ? { ? ? ? ? ? ? //創建工作簿 office2007以上 ? ? ? ? ? ? XSSFWorkbook workbook = new XSSFWorkbook(); ? ? ? ? ? ? //為工作簿創建工作表并命名 ? ? ? ? ? ? ISheet sheet = workbook.CreateSheet("商品表"); ? ? ? ? ? ? ICellStyle dateStyle = workbook.CreateCellStyle(); ? ? ? ? ? ? IDataFormat format = workbook.CreateDataFormat(); ? ? ? ? ? ? dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); ? ? ? ? ? ? #region 表頭及樣式 ? ? ? ? ? ? int cellIndex = 0; ? ? ? ? ? ? IRow headerRow = sheet.CreateRow(0); ? ? ? ? ? ? for (int i = 0; i < dtSource.Columns.Count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? #region MyRegion ? ? ? ? ? ? ? ? string ColumnsName = dtSource.Columns[i].ToString(); ? ? ? ? ? ? ? ? if (dtSource.Columns[i].ColumnName.EndsWith("XH")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ColumnsName = "序號"; ? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex, 3000); ? ? ? ? ? ? ? ? ? ?//sheet.SetColumnHidden(cellIndex, true);隱藏指定列 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (dtSource.Columns[i].ColumnName.EndsWith("GoogName")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ColumnsName = "商品名稱"; ? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex,10000);//設置列寬 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (dtSource.Columns[i].ColumnName.EndsWith("Num")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ColumnsName = "數量"; ? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex, 5000); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (dtSource.Columns[i].ColumnName.EndsWith("Summation")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ColumnsName = "合計(元)"; ? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex, 5000); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? //設置行高 ? ? ? ? ? ? ? ? headerRow.HeightInPoints = 35;? ? ? ? ? ? ? ? ? headerRow.CreateCell(cellIndex).SetCellValue(ColumnsName); ? ? ? ? ? ? ? ? ICellStyle headStyle = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? headStyle.WrapText = true; ? ? ? ? ? ? ? ? IFont font = workbook.CreateFont(); ? ? ? ? ? ? ? ? //字體大小 ? ? ? ? ? ? ? ? font.FontHeightInPoints = 12; ? ? ? ? ? ? ? ? font.Boldweight = 360; ? ? ? ? ? ? ? ? headStyle.SetFont(font); ? ? ? ? ? ? ? ? headerRow.GetCell(cellIndex).CellStyle = headStyle; ? ? ? ? ? ? ? ? cellIndex++; ? ? ? ? ? ? } ? ? ? ? ? ? #endregion ? ? ? ? ? ? int rowIndex = 1;//行數一定要從1行開始 ? ? ? ? ? ? int count = 1; ? ? ? ? ? ? int startRow = 1; ? ? ? ? ? ? DataView dvSource = dtSource.DefaultView; ? ? ? ? ? ? if (dvXH!=null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? foreach (DataRowView drv in dvXH) ? ? ? ? ? ? ? ? {//1-10.11-12,13-14,15-16 ? ? ? ? ? ? ? ? ? ? int rowcout = 0; ? ? ? ? ? ? ? ? ? ? dvSource.RowFilter = "XH='" + drv["XH"] + "'"; ? ? ? ? ? ? ? ? ? ? foreach (DataRowView row in dvSource) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? #region 填充內容 ? ? ? ? ? ? ? ? ? ? ? ? IRow dataRow = sheet.CreateRow(rowIndex); ? ? ? ? ? ? ? ? ? ? ? ? //序號 ? ? ? ? ? ? ? ? ? ? ? ? ICell newCel0 = dataRow.CreateCell(0); ? ? ? ? ? ? ? ? ? ? ? ? ICellStyle style0 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? ? ? style0.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? ? ? newCel0.SetCellValue(row["XH"].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? //標的名稱 ? ? ? ? ? ? ? ? ? ? ? ? ICell newCel2 = dataRow.CreateCell(1); ? ? ? ? ? ? ? ? ? ? ? ? ICellStyle style2 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? ? ? style2.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? ? ? newCel2.SetCellValue(row["GoogName"].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? //標的數量 ? ? ? ? ? ? ? ? ? ? ? ? ICell newCel4 = dataRow.CreateCell(2); ? ? ? ? ? ? ? ? ? ? ? ? ICellStyle style4 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? ? ? style4.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? ? ? newCel4.SetCellValue(row["Num"].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? //合計(元) ? ? ? ? ? ? ? ? ? ? ? ? ICell newCel8 = dataRow.CreateCell(3); ? ? ? ? ? ? ? ? ? ? ? ? ICellStyle style8 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? ? ? style8.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? ? ? newCel8.SetCellValue(row["Summation"].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? ? ? ? ? rowIndex++; ? ? ? ? ? ? ? ? ? ? ? ? rowcout++; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (count == 1) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //合并行數 ? ? ? ? ? ? ? ? ? ? ? ? sheet.AddMergedRegion(new CellRangeAddress(startRow, rowcout, 3, 3)); ? ? ? ? ? ? ? ? ? ? ? ? startRow = startRow + rowcout; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? sheet.AddMergedRegion(new CellRangeAddress(startRow, startRow + rowcout - 1, 3, 3)); ? ? ? ? ? ? ? ? ? ? ? ? startRow = startRow + rowcout; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? count++; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? #region MyRegion ? ? ? ? ? ? ? ? foreach (DataRowView row in dvSource) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? #region 填充內容 ? ? ? ? ? ? ? ? ? ? IRow dataRow = sheet.CreateRow(rowIndex); ? ? ? ? ? ? ? ? ? ? //序號 ? ? ? ? ? ? ? ? ? ? ICell newCel0 = dataRow.CreateCell(0); ? ? ? ? ? ? ? ? ? ? ICellStyle style0 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? style0.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? newCel0.SetCellValue(row["XH"].ToString()); ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //商品名稱 ? ? ? ? ? ? ? ? ? ? ICell newCel1 = dataRow.CreateCell(1); ? ? ? ? ? ? ? ? ? ? ICellStyle style1 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? style1.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? newCel1.SetCellValue(row["GoogName"].ToString()); ? ? ? ? ? ? ? ? ? ? //數量 ? ? ? ? ? ? ? ? ? ? ICell newCel2 = dataRow.CreateCell(2); ? ? ? ? ? ? ? ? ? ? ICellStyle style2 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? style2.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? newCel2.SetCellValue(row["Num"].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //合計(元) ? ? ? ? ? ? ? ? ? ? ICell newCel3 = dataRow.CreateCell(3); ? ? ? ? ? ? ? ? ? ? ICellStyle style3 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? style3.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? ? ? ? ? newCel3.SetCellValue(row["Summation"].ToString()); ? ? ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? ? ? ? ? rowIndex++; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? #endregion ? ? ? ? ? ? } ? ? ? ? ? ? #region 拼接最后一行 ? ? ? ? ? ? IFont fontLast = workbook.CreateFont(); ? ? ? ? ? ? fontLast.FontHeightInPoints = 30; ? ? ? ? ? ? fontLast.Boldweight = 480; ? ? ? ? ? ? IRow dataRowLast = sheet.CreateRow(rowIndex); ? ? ? ? ? ? dataRowLast.HeightInPoints = 40; ? ? ? ? ? ? ICell newCelLast = dataRowLast.CreateCell(0); ? ? ? ? ? ? ICellStyle styleLast = workbook.CreateCellStyle(); ? ? ? ? ? ? styleLast.DataFormat = format.GetFormat("text"); ? ? ? ? ? ? styleLast.SetFont(fontLast); ? ? ? ? ? ? newCelLast.SetCellValue("制作人:張三"); ? ? ? ? ? ? sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 3)); ? ? ? ? ? ? #endregion ? ? ? ? ? ? MemoryStream stream = new MemoryStream(); ? ? ? ? ? ? workbook.Write(stream); ? ? ? ? ? ? var buf = stream.ToArray(); ? ? ? ? ? ? using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? fs.Write(buf, 0, buf.Length); ? ? ? ? ? ? ? ? fs.Flush(); ? } }
實際運用中,涉及到數據,方法中有很多校驗等操作,方法直觀可讀性不是太好,下面附上簡單導出的方法:
實際上導出Excel,總結有幾點:
1、引用相關組件
2、創建一個工作簿,創建工作表并命名;
3、設置表頭及樣式;
4、填充數據;
5、保存數據到指定位置;
////// 簡單導出數據 /// /// 數據源 /// 保存路徑 /// 序號 ? ? ? ? public void Export1(DataTable dtSource, string strFileName) ? ? ? ? { ? ? ? ? ? ? //創建工作簿 ? ? ? ? ? ? XSSFWorkbook workbook = new XSSFWorkbook(); ? ? ? ? ? ? //為工作簿創建工作表并命名 ? ? ? ? ? ? ISheet sheet = workbook.CreateSheet("商品表"); ? ? ? ? ? ? IDataFormat format = workbook.CreateDataFormat(); ? ? ? ? ? ? #region 表頭及樣式 ? ? ? ? ? ? int cellIndex = 0; ? ? ? ? ? ? IRow headerRow = sheet.CreateRow(0); ? ? ? ? ? ? for (int i = 0; i < dtSource.Columns.Count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //設置行高 ? ? ? ? ? ? ? ? headerRow.HeightInPoints = 35; ? ? ? ? ? ? ? ? headerRow.CreateCell(cellIndex).SetCellValue(dtSource.Columns[i].ToString()); ? ? ? ? ? ? ? ? ICellStyle headStyle = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? headStyle.WrapText = true; ? ? ? ? ? ? ? ? IFont font = workbook.CreateFont(); ? ? ? ? ? ? ? ? //字體大小 ? ? ? ? ? ? ? ? font.FontHeightInPoints = 12; ? ? ? ? ? ? ? ? font.Boldweight = 360; ? ? ? ? ? ? ? ? headStyle.SetFont(font); ? ? ? ? ? ? ? ? headerRow.GetCell(cellIndex).CellStyle = headStyle; ? ? ? ? ? ? ? ? cellIndex++; ? ? ? ? ? ? } ? ? ? ? ? ? #endregion ? ? ? ? ? ? #region 數據填充 ? ? ? ? ? ? int rowIndex = 1;//行數一定要從1行開始,因為上面已經創建了表頭為0行; ? ? ? ? ? ? DataView dvSource = dtSource.DefaultView; ? ? ? ? ? ? foreach (DataRow row in dtSource.Rows) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? int ColumnIndex = 0; ? ? ? ? ? ? ? ? IRow dataRow = sheet.CreateRow(rowIndex); ? ? ? ? ? ? ? ? foreach (DataColumn column in dtSource.Columns) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //序號 ? ? ? ? ? ? ? ? ? ? ICell newCel0 = dataRow.CreateCell(ColumnIndex); ? ? ? ? ? ? ? ? ? ? ICellStyle style0 = workbook.CreateCellStyle(); ? ? ? ? ? ? ? ? ? ? style0.DataFormat = format.GetFormat("text");//數據類型 ? ? ? ? ? ? ? ? ? ? newCel0.SetCellValue(row[column.ColumnName].ToString()); ? ? ? ? ? ? ? ? ? ? ColumnIndex++; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? rowIndex++; ? ? ? ? ? ? } ? ? ? ? ? ? #endregion ? ? ? ? ? ? #region 保存到指定位置 ? ? ? ? ? ? MemoryStream stream = new MemoryStream(); ? ? ? ? ? ? workbook.Write(stream); ? ? ? ? ? ? var buf = stream.ToArray(); ? ? ? ? ? ? using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? fs.Write(buf, 0, buf.Length); ? ? ? ? ? ? ? ? fs.Flush(); ? ? ? ? ? ? } ? ? ? ? ? ? #endregion }
原文鏈接:https://blog.csdn.net/weixin_40233187/article/details/109142966
相關推薦
- 2022-03-30 C語言入門之淺談數據類型和變量常量_C 語言
- 2023-01-15 GoLang?RabbitMQ?TTL與死信隊列以及延遲隊列詳細講解_Golang
- 2022-09-14 python?服務器批處理得到PSSM矩陣的問題_python
- 2022-05-02 分布式利器redis及redisson的延遲隊列實踐_Redis
- 2022-02-22 Oracle10G序列名因標識符長度太大導致無法創建
- 2021-12-15 go語言處理TCP拆包/粘包的具體實現_Golang
- 2022-09-03 Docker?Buildx構建多平臺鏡像的實現_docker
- 2022-06-15 python多線程實現動態圖繪制_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支