1. 程式人生 > 其它 >c#使用NPOI匯出到excel

c#使用NPOI匯出到excel

1、新增程式包。

在專案名右鍵。

選擇管理NuGet程式包,瀏覽處搜尋NPOI並安裝。

2、程式碼引用。

using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

3、匯出Excel方法:

public void ExportDataToExcel(DataTable TableName, string FileName)
        {
            SaveFileDialog saveFileDialog 
= new SaveFileDialog(); //設定檔案標題 saveFileDialog.Title = "匯出Excel檔案"; //設定檔案型別 saveFileDialog.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 97-2003 工作簿(*.xls)|*.xls"; //設定預設檔案型別顯示順序 saveFileDialog.FilterIndex = 1; //是否自動在檔名中新增副檔名 saveFileDialog.AddExtension = true
; //是否記憶上次開啟的目錄 saveFileDialog.RestoreDirectory = true; //設定預設檔名 saveFileDialog.FileName = FileName; //按下確定選擇的按鈕 if (saveFileDialog.ShowDialog() == DialogResult.OK) { //獲得檔案路徑 string localFilePath = saveFileDialog.FileName.ToString();
//資料初始化 int TotalCount; //總行數 int RowRead = 0; //已讀行數 int Percent = 0; //百分比 TotalCount = TableName.Rows.Count; lblStatus.Text = "共有" + TotalCount + "條資料"; lblStatus.Visible = true; barStatus.Visible = true; //NPOI IWorkbook workbook; string FileExt = Path.GetExtension(localFilePath).ToLower(); if (FileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (FileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; } if (workbook == null) { return; } ISheet sheet = string.IsNullOrEmpty(FileName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(FileName); //秒鐘 Stopwatch timer = new Stopwatch(); timer.Start(); try { //讀取標題 IRow rowHeader = sheet.CreateRow(0); for (int i = 0; i < TableName.Columns.Count; i++) { ICell cell = rowHeader.CreateCell(i); cell.SetCellValue(TableName.Columns[i].ColumnName); } //讀取資料 for (int i = 0; i < TableName.Rows.Count; i++) { IRow rowData = sheet.CreateRow(i + 1); for (int j = 0; j < TableName.Columns.Count; j++) { ICell cell = rowData.CreateCell(j); cell.SetCellValue(TableName.Rows[i][j].ToString()); } //狀態列顯示 RowRead++; Percent = (int)(100 * RowRead / TotalCount); barStatus.Maximum = TotalCount; barStatus.Value = RowRead; lblStatus.Text = "共有" + TotalCount + "條資料,已讀取" + Percent.ToString() + "%的資料。"; Application.DoEvents(); } //狀態列更改 lblStatus.Text = "正在生成Excel..."; Application.DoEvents(); //轉為位元組陣列 MemoryStream stream = new MemoryStream(); workbook.Write(stream); var buf = stream.ToArray(); //儲存為Excel檔案 using (FileStream fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); fs.Close(); } //狀態列更改 lblStatus.Text = "生成Excel成功,共耗時" + timer.ElapsedMilliseconds + "毫秒。"; Application.DoEvents(); //關閉秒鐘 timer.Reset(); timer.Stop(); //成功提示 if (MessageBox.Show("匯出成功,是否立即開啟?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { System.Diagnostics.Process.Start(localFilePath); } //賦初始值 lblStatus.Visible = false; barStatus.Visible = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { //關閉秒鐘 timer.Reset(); timer.Stop(); //賦初始值 lblStatus.Visible = false; barStatus.Visible = false; } } }

4、結果演示:

轉 :https://www.cnblogs.com/zxh1314/p/8342066.html

https://www.cnblogs.com/lazyneal/p/6148912.html