亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#使用Npoi導(dǎo)出Excel并合并行列

 更新時間:2022年02月21日 14:17:27   作者:曹悠然  
這篇文章主要為大家詳細介紹了C#使用Npoi導(dǎo)出Excel并合并行列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在工作開發(fā)中,客戶經(jīng)常要求數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)出到Excel表格。以前方法是引用office相關(guān)組件,如果客戶沒有安裝office,功能就會遇到問題。

現(xiàn)在用Npoi導(dǎo)出Excel,導(dǎo)出表格是合并行列,如圖:

導(dǎo)出的要求:合計列要進行合并,序號一致的要合并。最后一行要合并列。
因為相同序號數(shù)量不是固定的,要動態(tài)算合并的行數(shù)。

合并行列接口:XXX.AddMergedRegion(new CellRangeAddress(開始行, 最后一行, 開始列, 最后一列));

隱藏指定:sheet.SetColumnHidden(cellIndex, true);

引用組件:

NPOI.dll;
NPOI.OOXML.dll;
NPOI.OpenXml4Net.dll;
NPOI.OpenXmlFormats.dll;
ICSharpCode.SharpZipLib.dll;

代碼如下:

/// <summary>
///?
/// </summary>
/// <param name="dtSource">數(shù)據(jù)源</param>
/// <param name="strFileName">保存路徑</param>
/// <param name="dvXH">序號</param>
public void Export(DataTable dtSource,string strFileName,DataView dvXH=null)
? ? ? ? {
? ? ? ? ? ? //創(chuàng)建工作簿 office2007以上
? ? ? ? ? ? XSSFWorkbook workbook = new XSSFWorkbook();
? ? ? ? ? ? //為工作簿創(chuàng)建工作表并命名
? ? ? ? ? ? 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);//設(shè)置列寬
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (dtSource.Columns[i].ColumnName.EndsWith("Num"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ColumnsName = "數(shù)量";
? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex, 5000);
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else if (dtSource.Columns[i].ColumnName.EndsWith("Summation"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ColumnsName = "合計(元)";
? ? ? ? ? ? ? ? ? ? sheet.SetColumnWidth(cellIndex, 5000);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? #endregion

? ? ? ? ? ? ? ? //設(shè)置行高
? ? ? ? ? ? ? ? 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;//行數(shù)一定要從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 填充內(nèi)容
? ? ? ? ? ? ? ? ? ? ? ? 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());

? ? ? ? ? ? ? ? ? ? ? ? //標的數(shù)量
? ? ? ? ? ? ? ? ? ? ? ? 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)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //合并行數(shù)
? ? ? ? ? ? ? ? ? ? ? ? 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 填充內(nèi)容
? ? ? ? ? ? ? ? ? ? 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());

? ? ? ? ? ? ? ? ? ? //數(shù)量
? ? ? ? ? ? ? ? ? ? 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();
?           }
} 

實際運用中,涉及到數(shù)據(jù),方法中有很多校驗等操作,方法直觀可讀性不是太好,下面附上簡單導(dǎo)出的方法:

實際上導(dǎo)出Excel,總結(jié)有幾點:

1、引用相關(guān)組件
2、創(chuàng)建一個工作簿,創(chuàng)建工作表并命名;
3、設(shè)置表頭及樣式;
4、填充數(shù)據(jù);
5、保存數(shù)據(jù)到指定位置;

/// <summary>
/// 簡單導(dǎo)出數(shù)據(jù)
/// </summary>
/// <param name="dtSource">數(shù)據(jù)源</param>
/// <param name="strFileName">保存路徑</param>
/// <param name="dvXH">序號</param>
? ? ? ? public void Export1(DataTable dtSource, string strFileName)
? ? ? ? {
? ? ? ? ? ? //創(chuàng)建工作簿
? ? ? ? ? ? XSSFWorkbook workbook = new XSSFWorkbook();
? ? ? ? ? ? //為工作簿創(chuàng)建工作表并命名
? ? ? ? ? ? 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++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //設(shè)置行高
? ? ? ? ? ? ? ? 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 數(shù)據(jù)填充
? ? ? ? ? ? int rowIndex = 1;//行數(shù)一定要從1行開始,因為上面已經(jīng)創(chuàng)建了表頭為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");//數(shù)據(jù)類型
? ? ? ? ? ? ? ? ? ? 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

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c#學(xué)習(xí)教程之JSON文件及解析實例

    c#學(xué)習(xí)教程之JSON文件及解析實例

    json作為互聯(lián)網(wǎng)上輕量便捷的數(shù)據(jù)傳輸格式,越來越受到重視,下面這篇文章主要給大家介紹了關(guān)于c#學(xué)習(xí)教程之JSON文件及解析的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • C#讀寫txt文件多種方法實例代碼

    C#讀寫txt文件多種方法實例代碼

    這篇文章主要介紹了C#讀寫txt文件的小例子,大家可以參考使用
    2013-11-11
  • C#命令模式用法實例

    C#命令模式用法實例

    這篇文章主要介紹了C#命令模式用法,以實例形式較為詳細的分析了C#命令模式的功能、定義及使用技巧,需要的朋友可以參考下
    2015-07-07
  • C# FTP操作類分享

    C# FTP操作類分享

    這篇文章主要為大家分享了C# FTP操作類的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#?HttpClient超時重試機制詳解

    C#?HttpClient超時重試機制詳解

    超時重試的實現(xiàn)方式可以使用循環(huán)結(jié)構(gòu),在請求發(fā)起后等待一定時間,若超時未收到響應(yīng),則再次發(fā)起請求,循環(huán)次數(shù)可以根據(jù)實際情況進行設(shè)置,一般建議不超過三次,這篇文章主要介紹了C#?HttpClient超時重試,需要的朋友可以參考下
    2023-06-06
  • C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺的操作方法

    C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺的操作方法

    這篇文章主要介紹了C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點播平臺,上傳視頻功能,主要要解決兩個問題,一是在服務(wù)端通過C#生成簽名和SDKID,二是在客戶端通過JavaScript上傳視頻到騰訊云點播服務(wù)器,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • C#寫入XML文檔

    C#寫入XML文檔

    這篇文章介紹了C#寫入XML文檔的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#基于Modbus三種CRC16校驗方法的性能對比

    C#基于Modbus三種CRC16校驗方法的性能對比

    這篇文章主要介紹了C#基于Modbus三種CRC16校驗方法的性能對比,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Unity實現(xiàn)俄羅斯方塊(三)

    Unity實現(xiàn)俄羅斯方塊(三)

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)俄羅斯方塊的第一部分代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 實例講解C#中的職責(zé)鏈模式

    實例講解C#中的職責(zé)鏈模式

    這篇文章主要介紹了C#中的職責(zé)鏈模式的相關(guān)資料,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論