基于NPOI操作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();
//创建单元格样式
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中
}
}
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15982401.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。