1. 程式人生 > WINDOWS開發 >Winform中通過NPOI匯出Excel的三種方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附程式碼下載

Winform中通過NPOI匯出Excel的三種方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附程式碼下載

場景

HSSFworkbook,XSSFworkbook,SXSSFworkbook區別

HSSFWorkbook:

是操作Excel2003以前(包括2003)的版本,副檔名是.xls;匯出excel最常用的方式;但是此種方式的侷限就是匯出的行數至多為65535行,超出65536條後系統就會報錯。

XSSFWorkbook:

是操作Excel2007後的版本,副檔名是.xlsx;為了突破HSSFWorkbook的65535行侷限。其對應的是excel2007(1048576行,16384列)副檔名為“.xlsx”,最多可以匯出104萬行,不過這樣就伴隨著一個問題---OOM記憶體溢位,原因是你所建立的book sheet row cell等此時是存在記憶體的並沒有持久化。

SXSSFWorkbook:

也是操作Excel2007後的版本,副檔名是.xlsx;SXSSFWorkbook是streaming版本的XSSFWorkbook,它只會儲存最新的excel rows在記憶體裡供檢視,在此之前的excel rows都會被寫入到硬盤裡(Windows電腦的話,是寫入到C盤根目錄下的temp資料夾)。被寫入到硬盤裡的rows是不可見的/不可訪問的。只有還儲存在記憶體裡的才可以被訪問到。

所以會在預設的C盤目錄下生成一些臨時檔案,預設路徑是:

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

技術分享圖片

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

技術分享圖片

技術分享圖片

注:

部落格主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取程式設計相關電子書、教程推送與免費下載。

實現

通過以上三種方式在匯出Eecel時新建sheet和Row和Cell時是一樣的,只不過在一開始新建wookbook物件時以及副檔名不同。

在實現匯出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匯出Excel

新建一個窗體頁面然後進入其程式碼,為例構建匯出的資料,首先新建一個物件類DataItem

    public class DataItem
    {

        public int Age { get; set; }


        public string Name { get; set; }


        public string Address { get; set; }

        public int Sex { get; set; }

    }

然後進入此窗體的程式碼中,首先構建匯出的資料

        //資料
        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 = "氣質",Age = 26,Address = "上海",new DataItem() {Name = "程式猿",Age = 27,Address = "青島",};

拖拽一個按鈕,然後在其點選事件中

private void button2_Click(object sender,EventArgs e)
        {
            //建立HSSFWorkbook物件
            HSSFWorkbook wb = new HSSFWorkbook();
            //建立sheet 並指定sheet的名字
            ISheet sheet1 = wb.CreateSheet("詳細資料");
            //新建style物件並設定樣式屬性
            ICellStyle style1 = wb.CreateCellStyle();//樣式
            IFont font1 = wb.CreateFont();//字型
            font1.FontName = "宋體";
            font1.FontHeightInPoints = 11;
            font1.Boldweight = (short)FontBoldWeight.Bold;
            style1.SetFont(font1);//樣式裡的字型設定具體的字型樣式

            //建立第一行
            IRow row0 = sheet1.CreateRow(0);
            //建立第一行第一列並設定值
            row0.CreateCell(0).SetCellValue("姓名");
            //獲取第一行第一列並設定樣式
            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;

            //迴圈新增資料
            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);
                
                //設定列寬度,256*字元數,因為單位是1/256個字元
                sheet1.SetColumnWidth(item_index,256 * item.Address.Length *4);
            }
            try
            {
                //將記憶體中的資料寫入磁碟
                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盤下生成xls的excel

技術分享圖片

技術分享圖片

XSSFWorkbook匯出Excel

然後再拖拽一個按鈕並設定按鈕的點選事件

private void button3_Click(object sender,EventArgs e)
        {
           
            //建立XSSFWorkbook物件
            XSSFWorkbook wb = new XSSFWorkbook();
            //建立sheet 並指定sheet的名字g
            ISheet sheet1 = wb.CreateSheet("詳細資料");
            //新建style物件並設定樣式屬性
            ICellStyle style1 = wb.CreateCellStyle();//樣式
            IFont font1 = wb.CreateFont();//字型
            font1.FontName = "宋體";
            font1.FontHeightInPoints = 11;
            font1.Boldweight = (short)FontBoldWeight.Bold;
            style1.SetFont(font1);//樣式裡的字型設定具體的字型樣式

            //建立第一行
            IRow row0 = sheet1.CreateRow(0);
            //建立第一行第一列並設定值
            row0.CreateCell(0).SetCellValue("姓名");
            //獲取第一行第一列並設定樣式
            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;

            //迴圈新增資料
            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);

                //設定列寬度,256*字元數,因為單位是1/256個字元
                sheet1.SetColumnWidth(item_index,256 * item.Address.Length * 4);
            }
            try
            {
                //將記憶體中的資料寫入磁碟
                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("匯出完成");
        }

然後執行專案,點選按鈕

技術分享圖片

技術分享圖片

SXSSFWorkbook匯出Excel

同理再拖拽一個按鈕,在按鈕的點選事件中,不同的是需要提前構建一個大資料量的資料,然後匯出時等待一段時間

private void button4_Click(object sender,EventArgs e)
        {
            
            //建立SXSSFWorkbook物件
            SXSSFWorkbook wb = new SXSSFWorkbook();
            //建立sheet 並指定sheet的名字
            ISheet sheet1 = wb.CreateSheet("詳細資料");
            //新建style物件並設定樣式屬性
            ICellStyle style1 = wb.CreateCellStyle();//樣式
            IFont font1 = wb.CreateFont();//字型
            font1.FontName = "宋體";
            font1.FontHeightInPoints = 11;
            font1.Boldweight = (short)FontBoldWeight.Bold;
            style1.SetFont(font1);//樣式裡的字型設定具體的字型樣式

            //建立第一行
            IRow row0 = sheet1.CreateRow(0);
            //建立第一行第一列並設定值
            row0.CreateCell(0).SetCellValue("姓名");
            //獲取第一行第一列並設定樣式
            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;

            //構建大資料量
            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);

            }
            //迴圈新增資料
            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);

                //設定列寬度,256*字元數,因為單位是1/256個字元
                sheet1.SetColumnWidth(item_index,"qizhi.xlsx"),FileMode.Create))
                {
                    wb.Write(filestream);
                    filestream.Close();
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            MessageBox.Show("匯出完成");
        }

技術分享圖片

技術分享圖片

程式碼下載

見下面文章末尾

https://mp.weixin.qq.com/s/GX-y9xxcufcMeFzegxySeg