基于NPOI操作Excel


   /// 
        /// 基于NPOI操作Excel
        /// 
        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();

                //创建单元格样式
                ICellStyle cellStyle = workbook.CreateCellStyle();

                //设置为文本格式,也可以为 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;       //上边框线

                //设置页签名
                ISheet sheet = workbook.CreateSheet("导出号段");

                //设置列宽
                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编号", "蓝牙MAC地址", "无线MAC地址1", "无线MAC地址2", "HARD_CODE", "QR_CODE", "TOKEN", "KEYMASTER" };
                //设置表头字段
                IRow headerRow = sheet.CreateRow(0);
                for (int i = 0; i < excelHeader.Length; i++)
                {
                    headerRow.CreateCell(i).SetCellValue(excelHeader[i]);
                }

                //展开的数量
                int count = int.Parse(LoginInfo.QTY);

                //十六进制转为十进制
                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;
                    //转回十六进制,大写
                    //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;
                    //转回十六进制,大写
                    string strCurrent_WifiMAC1 = current_WifiMAC1.ToString("X").ToUpper();
                    row.CreateCell(5).SetCellValue(strCurrent_WifiMAC1);


                    //WifiMAC2
                    if (LoginInfo.IsEnableWiFiMAC2)
                    {
                        Int64 current_WifiMAC2 = StartWifiMAC2 + i;
                        //转回十六进制,大写
                        string strCurrent_WifiMAC2 = current_WifiMAC2.ToString("X").ToUpper();
                        row.CreateCell(6).SetCellValue(strCurrent_WifiMAC2);

                    }

                    //HardCode
                    Int64 current_HardCode = StarthardCode + i;
                    //转回十六进制,小写
                    string strCurrent_HardCode = current_HardCode.ToString("X").ToLower();
                    row.CreateCell(7).SetCellValue(strCurrent_HardCode);

                }

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