NPOI實(shí)現(xiàn)兩級分組合并功能(示例講解)
NPOI版本:2.2.1.0
最近公司有這樣的需求:
統(tǒng)計(jì)每個(gè)部門下面,多個(gè)費(fèi)用使用情況。部門存在多級,但統(tǒng)計(jì)時(shí),只需統(tǒng)計(jì)到2級,2級以下的,歸到第2級的部門下。并且要求,第2級部門有個(gè)小計(jì),第1級部門需要有個(gè)合計(jì)。最后,還需提供總計(jì)。
本來對NPOI研究的還不夠深入的,以前都是直接通過別人提供的代碼來實(shí)現(xiàn)對DataTable中的數(shù)據(jù)進(jìn)行全部導(dǎo)出,但里面不帶合并,及合計(jì)功能,不滿足當(dāng)前需求。不得已,只有好好地研究一下了。還好,最終實(shí)現(xiàn)了要求。
在此,也感謝其他提供相關(guān)資料的人員,讓我實(shí)現(xiàn)了此功能。
簡要說明一下使用:
1、Export2Template2方法直接使用。DataTable原始數(shù)據(jù),必須是已經(jīng)按要求排好序的數(shù)據(jù)。全部是逐行向下處理。
2、要導(dǎo)出的列名,取自cellKeys中。列名必須為source中存在的。
3、相同值合并的第1列,為cellKeys[0],合并的第2列,為cellKeys[1],如需要其它列的合并,可以此基礎(chǔ)上,按自己的需求進(jìn)行調(diào)整。(合并時(shí),只會比較上下行的數(shù)據(jù)內(nèi)容)
4、要導(dǎo)出的數(shù)據(jù)中,數(shù)值類型,自動(dòng)居右。其它類型,自動(dòng)居中。
5、小計(jì),合計(jì),總計(jì)的字體,全部加黑
6、小計(jì),合計(jì),總計(jì),自動(dòng)對數(shù)值類型進(jìn)行匯總。其它類型數(shù)據(jù)全部置空。
7、合并的列數(shù):mergeColumns。如果>2,自動(dòng)只處理前2列。如果<1,則不做合并處理。
直接上可用的代碼:
/// <summary> /// 根據(jù)模版導(dǎo)出Excel -- 特別處理,每個(gè)分組帶合計(jì) /// </summary> /// <param name="source">源DataTable</param> /// <param name="cellKeys">需要導(dǎo)出的對應(yīng)的列字段 例:string[] cellKeys = { "Date","Remarks" };</param> /// <param name="strFileName">要保存的文件名稱(包含后綴) 例:"要保存的文件名.xls"</param> /// <param name="templateFile">模版文件名(包含路徑后綴) 例:"模板文件名.xls"</param> /// <param name="rowIndex">從第幾行開始創(chuàng)建數(shù)據(jù)行,第一行為0</param> /// <param name="mergeColumns">值相同時(shí),可合并的前幾列 最多支持2列 1=只合并第一列,2=判斷前2列</param> /// <param name="isConver">是否覆蓋數(shù)據(jù),=false,將把原數(shù)據(jù)下移。=true,將覆蓋插入行后面的數(shù)據(jù)</param> /// <param name="isTotal">是否帶小計(jì)/合計(jì)項(xiàng)</param> /// <param name="addAllTotal">是否添加總計(jì)項(xiàng)</param> /// <returns>是否導(dǎo)出成功</returns> public static bool Export2Template2(DataTable source, string[] cellKeys, string strFileName, string templateFile, int rowIndex, int mergeColumns, bool isConver, bool isTotal, bool addAllTotal) { bool bn = false; int cellCount = cellKeys.Length; //總列數(shù),第一列為0 // IWorkbook workbook = null; HSSFWorkbook workbook = null; string temp0 = "", temp1 = ""; int start0 = 0, start1 = 0; // 記錄1,2列值相同的開始序號 int end0 = 0, end1 = 0;// 記錄1,2列值相同的結(jié)束序號 try { using (FileStream file = new FileStream(templateFile, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(file); } #region 定義四類數(shù)據(jù)的單元格樣式 // 內(nèi)容數(shù)據(jù)格式 -- 數(shù)值 ICellStyle styleNum = workbook.CreateCellStyle(); styleNum.BorderBottom = BorderStyle.Thin; styleNum.BorderLeft = BorderStyle.Thin; styleNum.BorderRight = BorderStyle.Thin; styleNum.BorderTop = BorderStyle.Thin; // styleNum.VerticalAlignment = VerticalAlignment.Center; // styleNum.Alignment = HorizontalAlignment.Center; // 內(nèi)容數(shù)據(jù)格式 -- 字符串(做居中處理) ICellStyle styleStr = workbook.CreateCellStyle(); styleStr.BorderBottom = BorderStyle.Thin; styleStr.BorderLeft = BorderStyle.Thin; styleStr.BorderRight = BorderStyle.Thin; styleStr.BorderTop = BorderStyle.Thin; styleStr.VerticalAlignment = VerticalAlignment.Center; styleStr.Alignment = HorizontalAlignment.Center; // 匯總數(shù)據(jù)格式 -- 數(shù)值 ICellStyle styleTotalNum = workbook.CreateCellStyle(); styleTotalNum.BorderBottom = BorderStyle.Thin; styleTotalNum.BorderLeft = BorderStyle.Thin; styleTotalNum.BorderRight = BorderStyle.Thin; styleTotalNum.BorderTop = BorderStyle.Thin; styleTotalNum.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; styleTotalNum.FillPattern = FillPattern.SolidForeground; styleTotalNum.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; // 設(shè)置字體顏色 HSSFFont ffont0 = (HSSFFont)workbook.CreateFont(); // ffont0.FontHeight = 14 * 14; // ffont0.FontName = "宋體"; ffont0.IsBold = true; //ffont0.Color = HSSFColor.Red.Index; styleTotalNum.SetFont(ffont0); // 匯總數(shù)據(jù)格式 -- 字符串(做居中處理) ICellStyle styleTotalStr = workbook.CreateCellStyle(); styleTotalStr.BorderBottom = BorderStyle.Thin; styleTotalStr.BorderLeft = BorderStyle.Thin; styleTotalStr.BorderRight = BorderStyle.Thin; styleTotalStr.BorderTop = BorderStyle.Thin; styleTotalStr.VerticalAlignment = VerticalAlignment.Center; styleTotalStr.Alignment = HorizontalAlignment.Center; styleTotalStr.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; styleTotalStr.FillPattern = FillPattern.SolidForeground; // 設(shè)置字體顏色 HSSFFont ffont1 = (HSSFFont)workbook.CreateFont(); // ffont1.FontHeight = 14 * 14; // ffont1.FontName = "宋體"; ffont1.IsBold = true; //ffont.Color = HSSFColor.Red.Index; styleTotalStr.SetFont(ffont1); #endregion ISheet sheet = workbook.GetSheetAt(0); // 打開第一個(gè)sheet頁 if (sheet != null && source != null && source.Rows.Count > 0) // 模板內(nèi)容為空,不做處理 { IRow row; for (int i = 0, len = source.Rows.Count; i < len; i++) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 #region 第一行,寫入數(shù)據(jù)后,對變量賦初值 if (i == 0) // 第一行,賦初值 { row = sheet.CreateRow(rowIndex); #region 創(chuàng)建列并插入數(shù)據(jù) //創(chuàng)建列并插入數(shù)據(jù) for (int index = 0; index < cellCount; index++) { ICell cell = row.CreateCell(index); string strValue = !(source.Rows[i][cellKeys[index]] is DBNull) ? source.Rows[i][cellKeys[index]].ToString() : string.Empty; // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(strValue, out intV); cell.CellStyle = styleNum; // 設(shè)置格式 cell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": double doubV = 0; double.TryParse(strValue, out doubV); cell.CellStyle = styleNum; // 設(shè)置格式 cell.SetCellValue(doubV); break; default: cell.CellStyle = styleStr; // 設(shè)置格式 cell.SetCellValue(strValue); break; } } #endregion if (mergeColumns > 0) { temp0 = source.Rows[i][cellKeys[0]].ToString(); // 保存第1列值 start0 = rowIndex; end0 = rowIndex; } if (mergeColumns > 1) { temp1 = source.Rows[i][cellKeys[1]].ToString(); // 保存第2列值 start1 = rowIndex; end1 = rowIndex; } rowIndex++; continue; } #endregion // 不是第一行數(shù)據(jù)的處理 // 判斷1列值變化沒 string cellText0 = source.Rows[i][cellKeys[0]].ToString(); if (temp0 != cellText0) // 第1列值有變化 { #region 第2列要合并 if (mergeColumns > 1) // 第2列要合并 { if (start1 != end1) // 開始行和結(jié)束行不相同,才進(jìn)行合并 { CellRangeAddress region1 = new CellRangeAddress(start1, end1, 1, 1); // 合并第二列 sheet.AddMergedRegion(region1); } #region 第2列加小計(jì) if (isTotal) // 加小計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal1 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 插入小計(jì)數(shù)據(jù) for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal1.CreateCell(index); if (index == 0) //第1列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(temp0); continue; } if (index == 1) // 第2列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("小計(jì)"); continue; } // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); int intV = 0; int.TryParse(obj1.ToString(), out intV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } } #endregion // 合并小計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 1, 2); // 合并小計(jì) sheet.AddMergedRegion(region0); } #endregion temp1 = source.Rows[i][cellKeys[1]].ToString(); end0++; rowIndex++; } #endregion #region 第1列要合并 if (mergeColumns > 0) // 第1列要合并 { if (start0 != end0) // 開始行和結(jié)束行不相同,才進(jìn)行合并 { CellRangeAddress region0 = new CellRangeAddress(start0, end0, 0, 0); // 合并第二列 sheet.AddMergedRegion(region0); } #region 第1列加合計(jì) if (isTotal) // 加合計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal0 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 加合計(jì)列 for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal0.CreateCell(index); if (index == 0) { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("合計(jì)"); //第1列 continue; } if (index == 1) { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); // 第2列 continue; } switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' ", cellKeys[0], temp0)); int intV = 0; int.TryParse(obj1.ToString(), out intV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' ", cellKeys[0], temp0)); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } } #endregion // 合并合計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 0, 2); // 合并合計(jì) sheet.AddMergedRegion(region0); end0++; rowIndex++; } #endregion temp0 = cellText0; } #endregion // 重新賦值 start0 = rowIndex; end0 = rowIndex; start1 = rowIndex; end1 = rowIndex; } else // 第1列值沒有變化 { end0++; // 判斷第2列是否有變化 string cellText1 = source.Rows[i][cellKeys[1]].ToString(); if (cellText1 != temp1) // 第1列沒變,第2列變化 { #region 第2列要合并 if (mergeColumns > 1) // 第2列要合并 { if (start1 != end1) // 開始行和結(jié)束行不相同,才進(jìn)行合并 { CellRangeAddress region1 = new CellRangeAddress(start1, end1, 1, 1); // 合并第二列 sheet.AddMergedRegion(region1); } #region 第2列加小計(jì) if (isTotal) // 加小計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal1 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 插入小計(jì)數(shù)據(jù) for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal1.CreateCell(index); if (index == 0) //第1列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(temp0); continue; } if (index == 1) // 第2列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("小計(jì)"); continue; } // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); int intV = 0; int.TryParse(obj1.ToString(), out intV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } } #endregion // 合并小計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 1, 2); // 合并小計(jì) sheet.AddMergedRegion(region0); end0++; rowIndex++; } temp1 = cellText1; // 要合并,才進(jìn)行重新賦值 start1 = rowIndex; end1 = rowIndex; #endregion } #endregion } else // 第1列值沒變,第2列也沒變 end1++; } // 插入當(dāng)前數(shù)據(jù) row = sheet.CreateRow(rowIndex); #region 創(chuàng)建行并插入當(dāng)前記錄的數(shù)據(jù) //創(chuàng)建行并插入當(dāng)前記錄的數(shù)據(jù) for (int index = 0; index < cellCount; index++) { ICell cell = row.CreateCell(index);<br> string strValue = !(source.Rows[i][cellKeys[index]] is DBNull) ? source.Rows[i][cellKeys[index]].ToString() : string.Empty; // 取值 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(strValue, out intV); cell.CellStyle = styleNum; cell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": double doubV = 0; double.TryParse(strValue, out doubV); cell.CellStyle = styleNum; cell.SetCellValue(doubV); break; default: cell.CellStyle = styleStr; cell.SetCellValue(strValue); break; } } #endregion // 下移一行 rowIndex++; } // 最后一條記錄的合計(jì) #region 對第2列進(jìn)行合并 if (mergeColumns > 1) // 對第2列合并 { if (start1 != end1) // 開始行和結(jié)束行不等,進(jìn)行合并 { CellRangeAddress region1 = new CellRangeAddress(start1, end1, 1, 1); // 合并第二列 sheet.AddMergedRegion(region1); } #region 第2列加小計(jì) if (isTotal) // 加小計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal1 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 插入小計(jì)數(shù)據(jù) for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal1.CreateCell(index); #region 列值處理 if (index == 0) //第1列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(temp0); continue; } if (index == 1) // 第2列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("小計(jì)"); continue; } // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); int intV = 0; int.TryParse(obj1.ToString(), out intV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' and {2} = '{3}' ", cellKeys[0], temp0, cellKeys[1], temp1)); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } #endregion } #endregion // 合并小計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 1, 2); // 合并小計(jì) sheet.AddMergedRegion(region0); rowIndex++; end0++; } #endregion } #endregion #region 對第1列合并 if (mergeColumns > 0) // 對第1列合并 { if (start0 != end0) // 開始行和結(jié)束行不等,進(jìn)行合并 { CellRangeAddress region1 = new CellRangeAddress(start0, end0, 0, 0); // 合并第二列 sheet.AddMergedRegion(region1); } #region 第1列加合計(jì) if (isTotal) // 加合計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal0 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 插入合計(jì)數(shù)據(jù) for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal0.CreateCell(index); #region 列值處理 if (index == 0) //第1列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("合計(jì)"); continue; } if (index == 1) // 第2列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); continue; } // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' ", cellKeys[0], temp0)); int intV = 0; newcell.CellStyle = styleTotalNum; int.TryParse(obj1.ToString(), out intV); newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), string.Format("{0} = '{1}' ", cellKeys[0], temp0)); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } #endregion } #endregion // 合并合計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 0, 2); // 合并合計(jì) sheet.AddMergedRegion(region0); } rowIndex++; #endregion } #endregion #region 進(jìn)行匯總 - 加總計(jì) if (addAllTotal) // 加總計(jì) { if (!isConver) sheet.ShiftRows(rowIndex, sheet.LastRowNum, 1, true, false); // 不覆蓋,數(shù)據(jù)向下移 IRow rowTotal0 = sheet.CreateRow(rowIndex); //創(chuàng)建列并插入數(shù)據(jù) #region 插入總計(jì)數(shù)據(jù) for (int index = 0; index < cellCount; index++) { object obj1; ICell newcell = rowTotal0.CreateCell(index); #region 列值處理 if (index == 0) //第1列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue("總計(jì)"); continue; } if (index == 1) // 第2列 { newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); continue; } // 其它列數(shù)據(jù),數(shù)值進(jìn)行匯總 switch (source.Columns[cellKeys[index]].DataType.ToString()) { case "System.Int16": //整型 case "System.Int32": case "System.Int64": case "System.Byte": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), ""); int intV = 0; int.TryParse(obj1.ToString(), out intV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(intV); break; case "System.Decimal": //浮點(diǎn)型 case "System.Double": case "System.Single": obj1 = source.Compute(string.Format("sum({0})", cellKeys[index]), ""); double doubV = 0; double.TryParse(obj1.ToString(), out doubV); newcell.CellStyle = styleTotalNum; newcell.SetCellValue(doubV); break; default: newcell.CellStyle = styleTotalStr; newcell.SetCellValue(""); break; } #endregion } #endregion // 合并總計(jì) CellRangeAddress region0 = new CellRangeAddress(rowIndex, rowIndex, 0, 2); // 合并總計(jì) sheet.AddMergedRegion(region0); } #endregion } return Save2Xls(strFileName, workbook); // 保存為xls文件 } catch (Exception ex) { // FileHelper.WriteLine(logfile, "處理數(shù)據(jù)異常:" + ex.Message); // msg = ex.Message; } return bn; }
保存文件的代碼:
public static bool Save2Xls(string fileName, IWorkbook workbook) { bool bn = false; try { FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate); MemoryStream ms = new MemoryStream(); workbook.Write(ms); BinaryWriter w = new BinaryWriter(fs); w.Write(ms.ToArray()); fs.Close(); ms.Close(); bn = true; } catch(Exception ex) { //FileHelper.WriteLine(logfile, "保存文件異常:" + ex.Message); } return bn; }
以上這篇NPOI實(shí)現(xiàn)兩級分組合并功能(示例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)定時(shí)任務(wù)Task Scheduler的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)定時(shí)任務(wù)Task Scheduler的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#實(shí)現(xiàn)合并多個(gè)word文檔的方法
這篇文章主要介紹了C#實(shí)現(xiàn)合并多個(gè)word文檔的方法,是C#針對Word文檔操作的一個(gè)非常重要的技巧,需要的朋友可以參考下2014-09-09Qt之調(diào)用C#的動(dòng)態(tài)庫的解決方法
這篇文章給大家介紹了Qt之調(diào)用C#的動(dòng)態(tài)庫的解決方法,環(huán)境使用的是VS2019+Qt5.12,感興趣的朋友一起看看吧2021-10-10windows下C#定時(shí)管理器框架Task.MainForm詳解
這篇文章主要為大家詳細(xì)介紹了windows下C#定時(shí)管理器框架Task.MainForm的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06