日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载

發(fā)布時間:2025/3/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景

HSSFworkbook,XSSFworkbook,SXSSFworkbook區(qū)別

HSSFWorkbook:

是操作Excel2003以前(包括2003)的版本,擴展名是.xls;導(dǎo)出excel最常用的方式;但是此種方式的局限就是導(dǎo)出的行數(shù)至多為65535行,超出65536條后系統(tǒng)就會報錯。

XSSFWorkbook:

是操作Excel2007后的版本,擴展名是.xlsx;為了突破HSSFWorkbook的65535行局限。其對應(yīng)的是excel2007(1048576行,16384列)擴展名為“.xlsx”,最多可以導(dǎo)出104萬行,不過這樣就伴隨著一個問題---OOM內(nèi)存溢出,原因是你所創(chuàng)建的book sheet row cell等此時是存在內(nèi)存的并沒有持久化。

SXSSFWorkbook:

也是操作Excel2007后的版本,擴展名是.xlsx;SXSSFWorkbook是streaming版本的XSSFWorkbook,它只會保存最新的excel rows在內(nèi)存里供查看,在此之前的excel rows都會被寫入到硬盤里(Windows電腦的話,是寫入到C盤根目錄下的temp文件夾)。被寫入到硬盤里的rows是不可見的/不可訪問的。只有還保存在內(nèi)存里的才可以被訪問到。

所以會在默認的C盤目錄下生成一些臨時文件,默認路徑是:

C:\Users\HAOHAO\AppData\Local\Temp\poifiles

?

如果忘記了位置要清理的話,可以借助垃圾清理軟件

?

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。

實現(xiàn)

通過以上三種方式在導(dǎo)出Eecel時新建sheet和Row和Cell時是一樣的,只不過在一開始新建wookbook對象時以及文件擴展名不同。

在實現(xiàn)導(dǎo)出Excel之前首先要進行npoi的dll的引用,NPOI需要引用的dll如下:

?

可以看到除了NPOI開頭的引用,還有另外的兩個引用,為什么要有這兩個引用,我們可以通過其github下載源碼

https://github.com/svn2github/npoi

然后可以看到其源碼中引用了這兩個dll,所以這里也需要引用這兩個dll

?

dll下載鏈接:

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12473431

HSSFWorkbook導(dǎo)出Excel

新建一個窗體頁面然后進入其代碼,為例構(gòu)建導(dǎo)出的數(shù)據(jù),首先新建一個對象類DataItem

??? public class DataItem{public int Age { get; set; }public string Name { get; set; }public string Address { get; set; }public int Sex { get; set; }}

然后進入此窗體的代碼中,首先構(gòu)建導(dǎo)出的數(shù)據(jù)

??????? //數(shù)據(jù)List<DataItem> ItemList = new List<DataItem>(){new DataItem() {Name = "霸道",Age = 24,Address = "中國",Sex = 1},new DataItem() {Name = "流氓",Age = 25,Address = "北京",Sex = 0},new DataItem() {Name = "氣質(zhì)",Age = 26,Address = "上海",Sex = 0},new DataItem() {Name = "程序猿",Age = 27,Address = "青島",Sex = 1},};

拖拽一個按鈕,然后在其點擊事件中

private void button2_Click(object sender, EventArgs e){//創(chuàng)建HSSFWorkbook對象HSSFWorkbook wb = new HSSFWorkbook();//創(chuàng)建sheet 并指定sheet的名字ISheet sheet1 = wb.CreateSheet("詳細數(shù)據(jù)");//新建style對象并設(shè)置樣式屬性ICellStyle style1 = wb.CreateCellStyle();//樣式IFont font1 = wb.CreateFont();//字體font1.FontName = "宋體";font1.FontHeightInPoints = 11;font1.Boldweight = (short)FontBoldWeight.Bold;style1.SetFont(font1);//樣式里的字體設(shè)置具體的字體樣式//創(chuàng)建第一行IRow row0 = sheet1.CreateRow(0);//創(chuàng)建第一行第一列并設(shè)置值row0.CreateCell(0).SetCellValue("姓名");//獲取第一行第一列并設(shè)置樣式row0.GetCell(0).CellStyle = style1;row0.CreateCell(1).SetCellValue("年齡");row0.GetCell(1).CellStyle = style1;row0.CreateCell(2).SetCellValue("地址");row0.GetCell(2).CellStyle = style1;row0.CreateCell(3).SetCellValue("性別");row0.GetCell(3).CellStyle = style1;//循環(huán)添加數(shù)據(jù)foreach (DataItem item in ItemList){int item_index = ItemList.IndexOf(item);//從第二行開始IRow rowi = sheet1.CreateRow(item_index+1);rowi.CreateCell(0).SetCellValue(item.Name);rowi.CreateCell(1).SetCellValue(item.Age);rowi.CreateCell(2).SetCellValue(item.Address);rowi.CreateCell(3).SetCellValue(item.Sex);//設(shè)置列寬度,256*字符數(shù),因為單位是1/256個字符sheet1.SetColumnWidth(item_index, 256 * item.Address.Length *4);}try{//將內(nèi)存中的數(shù)據(jù)寫入磁盤using (FileStream filestream = new FileStream(System.IO.Path.Combine(@"D:\", "badao.xls"), FileMode.Create)){wb.Write(filestream);filestream.Close();}}catch (Exception ex){Console.Write(ex);}MessageBox.Show("導(dǎo)出完成");}

運行項目點擊按鈕,然后會在D盤下生成xls的excel

?

XSSFWorkbook導(dǎo)出Excel

然后再拖拽一個按鈕并設(shè)置按鈕的點擊事件

private void button3_Click(object sender, EventArgs e){//創(chuàng)建XSSFWorkbook對象XSSFWorkbook wb = new XSSFWorkbook();//創(chuàng)建sheet 并指定sheet的名字gISheet sheet1 = wb.CreateSheet("詳細數(shù)據(jù)");//新建style對象并設(shè)置樣式屬性ICellStyle style1 = wb.CreateCellStyle();//樣式IFont font1 = wb.CreateFont();//字體font1.FontName = "宋體";font1.FontHeightInPoints = 11;font1.Boldweight = (short)FontBoldWeight.Bold;style1.SetFont(font1);//樣式里的字體設(shè)置具體的字體樣式//創(chuàng)建第一行IRow row0 = sheet1.CreateRow(0);//創(chuàng)建第一行第一列并設(shè)置值row0.CreateCell(0).SetCellValue("姓名");//獲取第一行第一列并設(shè)置樣式row0.GetCell(0).CellStyle = style1;row0.CreateCell(1).SetCellValue("年齡");row0.GetCell(1).CellStyle = style1;row0.CreateCell(2).SetCellValue("地址");row0.GetCell(2).CellStyle = style1;row0.CreateCell(3).SetCellValue("性別");row0.GetCell(3).CellStyle = style1;//循環(huán)添加數(shù)據(jù)foreach (DataItem item in ItemList){int item_index = ItemList.IndexOf(item);//從第二行開始IRow rowi = sheet1.CreateRow(item_index + 1);rowi.CreateCell(0).SetCellValue(item.Name);rowi.CreateCell(1).SetCellValue(item.Age);rowi.CreateCell(2).SetCellValue(item.Address);rowi.CreateCell(3).SetCellValue(item.Sex);//設(shè)置列寬度,256*字符數(shù),因為單位是1/256個字符sheet1.SetColumnWidth(item_index, 256 * item.Address.Length * 4);}try{//將內(nèi)存中的數(shù)據(jù)寫入磁盤using (FileStream filestream = new FileStream(System.IO.Path.Combine(@"D:\", "liumang.xlsx"), FileMode.Create)){wb.Write(filestream);filestream.Close();}}catch (Exception ex){Console.Write(ex);}MessageBox.Show("導(dǎo)出完成");}

然后運行項目,點擊按鈕

?

SXSSFWorkbook導(dǎo)出Excel

同理再拖拽一個按鈕,在按鈕的點擊事件中,不同的是需要提前構(gòu)建一個大數(shù)據(jù)量的數(shù)據(jù),然后導(dǎo)出時等待一段時間

private void button4_Click(object sender, EventArgs e){//創(chuàng)建SXSSFWorkbook對象SXSSFWorkbook wb = new SXSSFWorkbook();//創(chuàng)建sheet 并指定sheet的名字ISheet sheet1 = wb.CreateSheet("詳細數(shù)據(jù)");//新建style對象并設(shè)置樣式屬性ICellStyle style1 = wb.CreateCellStyle();//樣式IFont font1 = wb.CreateFont();//字體font1.FontName = "宋體";font1.FontHeightInPoints = 11;font1.Boldweight = (short)FontBoldWeight.Bold;style1.SetFont(font1);//樣式里的字體設(shè)置具體的字體樣式//創(chuàng)建第一行IRow row0 = sheet1.CreateRow(0);//創(chuàng)建第一行第一列并設(shè)置值row0.CreateCell(0).SetCellValue("姓名");//獲取第一行第一列并設(shè)置樣式row0.GetCell(0).CellStyle = style1;row0.CreateCell(1).SetCellValue("年齡");row0.GetCell(1).CellStyle = style1;row0.CreateCell(2).SetCellValue("地址");row0.GetCell(2).CellStyle = style1;row0.CreateCell(3).SetCellValue("性別");row0.GetCell(3).CellStyle = style1;//構(gòu)建大數(shù)據(jù)量List<DataItem> bigData = new List<DataItem>();for (int i = 0; i < 50000;i++ ){DataItem data = new DataItem();data.Name = "霸道" + i;data.Age = i;data.Address = "青島" + i;data.Sex = i;bigData.Add(data);}//循環(huán)添加數(shù)據(jù)foreach (DataItem item in bigData){int item_index = bigData.IndexOf(item);//從第二行開始IRow rowi = sheet1.CreateRow(item_index + 1);rowi.CreateCell(0).SetCellValue(item.Name);rowi.CreateCell(1).SetCellValue(item.Age);rowi.CreateCell(2).SetCellValue(item.Address);rowi.CreateCell(3).SetCellValue(item.Sex);//設(shè)置列寬度,256*字符數(shù),因為單位是1/256個字符sheet1.SetColumnWidth(item_index, 256 * item.Address.Length * 4);}try{//將內(nèi)存中的數(shù)據(jù)寫入磁盤using (FileStream filestream = new FileStream(System.IO.Path.Combine(@"D:\", "qizhi.xlsx"), FileMode.Create)){wb.Write(filestream);filestream.Close();}}catch (Exception ex){Console.Write(ex);}MessageBox.Show("導(dǎo)出完成");}

?

代碼下載

見下面文章末

WinformNPOI導(dǎo)出Excel三種(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代碼

?

?

總結(jié)

以上是生活随笔為你收集整理的Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。