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 = "上海",Sex = 0},
            new DataItem() {Name = "程序猿",Age = 27,Address = "青島",Sex = 1},
        };

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

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, 256 * item.Address.Length * 4);
            }
            try
            {
                //將內存中的數據寫入磁盤
                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("導出完成");
        }

 

代碼下載

見下面文章末

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章