欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于NPOI操作Excel

 更新時(shí)間:2022年04月29日 11:31:58   作者:農(nóng)碼一生  
這篇文章介紹了C#基于NPOI操作Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

NPOI簡(jiǎn)介

NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒(méi)有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作。

NPOI是一個(gè)開(kāi)源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項(xiàng)目。

優(yōu)勢(shì)

(一)傳統(tǒng)操作Excel遇到的問(wèn)題:

1、如果是.NET,需要在服務(wù)器端裝Office,且及時(shí)更新它,以防漏洞,還需要設(shè)定權(quán)限允許.NET訪問(wèn)COM+,如果在導(dǎo)出過(guò)程中出問(wèn)題可能導(dǎo)致服務(wù)器宕機(jī)。

2、Excel會(huì)把只包含數(shù)字的列進(jìn)行類型轉(zhuǎn)換,本來(lái)是文本型的,Excel會(huì)將其轉(zhuǎn)成數(shù)值型的,比如編號(hào)000123會(huì)變成123。

3、導(dǎo)出時(shí),如果字段內(nèi)容以“-”或“=”開(kāi)頭,Excel會(huì)把它當(dāng)成公式進(jìn)行,會(huì)報(bào)錯(cuò)。

4、Excel會(huì)根據(jù)Excel文件前8行分析數(shù)據(jù)類型,如果正好你前8行某一列只是數(shù)字,那它會(huì)認(rèn)為該列為數(shù)值型,自動(dòng)將該列轉(zhuǎn)變成類似1.42702E+17格式,日期列變成包含日期和數(shù)字的。

(二)使用NPOI的優(yōu)勢(shì)

1、您可以完全免費(fèi)使用該框架

2、包含了大部分EXCEL的特性(單元格樣式、數(shù)據(jù)格式、公式等等)

3、專業(yè)的技術(shù)支持服務(wù)(24*7全天候) (非免費(fèi))

4、支持處理的文件格式包括xls, xlsx, docx.

5、采用面向接口的設(shè)計(jì)架構(gòu)( 可以查看 NPOI.SS 的命名空間)

6、同時(shí)支持文件的導(dǎo)入和導(dǎo)出

7、基于.net 2.0 也支持xlsx 和 docx格式(當(dāng)然也支持.net 4.0)

8、來(lái)自全世界大量成功且真實(shí)的測(cè)試Cases

9、大量的實(shí)例代碼

11、你不需要在服務(wù)器上安裝微軟的Office,可以避免版權(quán)問(wèn)題。

12、使用起來(lái)比Office PIA的API更加方便,更人性化。

13、你不用去花大力氣維護(hù)NPOI,NPOI Team會(huì)不斷更新、改善NPOI,絕對(duì)省成本。

14、不僅僅對(duì)與Excel可以進(jìn)行操作,對(duì)于doc、ppt文件也可以做對(duì)應(yīng)的操作

NPOI之所以強(qiáng)大,并不是因?yàn)樗С謱?dǎo)出Excel,而是因?yàn)樗С謱?dǎo)入Excel,并能“理解”OLE2文檔結(jié)構(gòu),這也是其他一些Excel讀寫庫(kù)比較弱的方面。通常,讀入并理解結(jié)構(gòu)遠(yuǎn)比導(dǎo)出來(lái)得復(fù)雜,因?yàn)閷?dǎo)入你必須假設(shè)一切情況都是可能的,而生成你只要保證滿足你自己需求就可以了,如果把導(dǎo)入需求和生成需求比做兩個(gè)集合,那么生成需求通常都是導(dǎo)入需求的子集,這一規(guī)律不僅體現(xiàn)在Excel讀寫庫(kù)中,也體現(xiàn)在pdf讀寫庫(kù)中,目前市面上大部分的pdf庫(kù)僅支持生成,不支持導(dǎo)入。

操作Excel

        /// <summary>
        /// 基于NPOI操作Excel
        /// </summary>
        public void ExportExcel()
        {
            string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Excel\\";
            string fileName = filePath + "TestExcel_" + DateTime.UtcNow.ToString("yyyyMMddHHmmss") + ".xlsx";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                XSSFWorkbook workbook = new XSSFWorkbook();

                //創(chuàng)建單元格樣式
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //設(shè)置為文本格式,也可以為 text,即 dataFormat.GetFormat("text");
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;    //下邊框線
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;      //左邊框線
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;     //右邊框線
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;       //上邊框線

                //設(shè)置頁(yè)簽名
                ISheet sheet = workbook.CreateSheet("導(dǎo)出號(hào)段");

                //設(shè)置列寬
                sheet.SetColumnWidth(4, 10 * 500);//第五列 BTMAC
                sheet.SetColumnWidth(5, 10 * 500);//第六列 WifiMAC1
                sheet.SetColumnWidth(6, 10 * 500);//第七列 WifiMAC2
                sheet.SetColumnWidth(7, 10 * 500);//第八列 HARD_CODE
                sheet.SetColumnWidth(8, 10 * 500);//第九列 QR_CODE

                //Excel表頭欄位
                string[] excelHeader = new string[] { "IMEI1", "IMEI2", "MEID", "MSN編號(hào)", "藍(lán)牙MAC地址", "無(wú)線MAC地址1", "無(wú)線MAC地址2", "HARD_CODE", "QR_CODE", "TOKEN", "KEYMASTER" };
                //設(shè)置表頭字段
                IRow headerRow = sheet.CreateRow(0);
                for (int i = 0; i < excelHeader.Length; i++)
                {
                    headerRow.CreateCell(i).SetCellValue(excelHeader[i]);
                }

                //展開(kāi)的數(shù)量
                int count = int.Parse(LoginInfo.QTY);

                //十六進(jìn)制轉(zhuǎn)為十進(jìn)制
                Int64 StarthardCode = Int64.Parse(LoginInfo.HardCode, NumberStyles.HexNumber);
                Int64 StartbtMAC = Int64.Parse(LoginInfo.BTMAC, NumberStyles.HexNumber);
                Int64 StartWifiMAC1 = Int64.Parse(LoginInfo.WiFiMAC1, NumberStyles.HexNumber);
                Int64 StartWifiMAC2 = 0;
                if (LoginInfo.IsEnableWiFiMAC2)
                {
                    StartWifiMAC2 = Int64.Parse(LoginInfo.WiFiMAC2, NumberStyles.HexNumber);
                }


                //填充Excel
                for (int i = 0; i < count; i++)
                {
                    IRow row = sheet.CreateRow(i + 1);
                    //BTMAC
                    Int64 current_btMAC = StartbtMAC + i;
                    //轉(zhuǎn)回十六進(jìn)制,大寫
                    //string strCurrent_btMAC = Convert.ToString(current_btMAC, 16).ToUpper();
                    string strCurrent_btMAC = current_btMAC.ToString("X").ToUpper();
                    row.CreateCell(4).SetCellValue(strCurrent_btMAC);


                    //WifiMAC1
                    Int64 current_WifiMAC1 = StartWifiMAC1 + i;
                    //轉(zhuǎn)回十六進(jìn)制,大寫
                    string strCurrent_WifiMAC1 = current_WifiMAC1.ToString("X").ToUpper();
                    row.CreateCell(5).SetCellValue(strCurrent_WifiMAC1);


                    //WifiMAC2
                    if (LoginInfo.IsEnableWiFiMAC2)
                    {
                        Int64 current_WifiMAC2 = StartWifiMAC2 + i;
                        //轉(zhuǎn)回十六進(jìn)制,大寫
                        string strCurrent_WifiMAC2 = current_WifiMAC2.ToString("X").ToUpper();
                        row.CreateCell(6).SetCellValue(strCurrent_WifiMAC2);

                    }

                    //HardCode
                    Int64 current_HardCode = StarthardCode + i;
                    //轉(zhuǎn)回十六進(jìn)制,小寫
                    string strCurrent_HardCode = current_HardCode.ToString("X").ToLower();
                    row.CreateCell(7).SetCellValue(strCurrent_HardCode);

                }

                workbook.Write(fs);           //寫入到Excel中          
            }
        }

到此這篇關(guān)于C#基于NPOI操作Excel的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論