
前言
前面介紹了NPOI單元格樣式的使用、單元格合并,以及NPOI提供的顏色?,F(xiàn)在用上前面的一些知識點,做一個測試結(jié)果表格。
1、 介紹NPOI 的顏色卡、名稱以及索引 https://editor.csdn.net/md/?articleId=130265415
2、 NPOI的CellStyle單元格樣式 https://editor.csdn.net/md/?articleId=130245869
3、 將DataTable中的數(shù)據(jù)保存到Excel (二) 使用NPOI https://editor.csdn.net/md/?articleId=130225379
初版表格,單元格的合并
先上一個簡單版本的,主要內(nèi)容在于跨行和跨列單元格的合并。涉及的主要函數(shù)是Sheet的AddMergedRegion
以及NPOI.SS.Util.CellRangeAddress
這兩個函數(shù)。
這里使用的NPOI版本是 2.0.6.0, 框架 .NET Framework 4.0。
using NPOI.HSSF.Util;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
/// <summary>
/// 合并單元格
/// </summary>
void MergeCell()
{
//創(chuàng)建工作簿對象
HSSFWorkbook workBook = new HSSFWorkbook();
//創(chuàng)建一個sheet
ISheet sheet = workBook.CreateSheet("MergeTable");
IRow row;
ICell cell;
//行索引
int rowIndex = 0;
//標(biāo)題
row = sheet.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("測試報告");
for (int i=1; i<8; i++)
{
cell = row.CreateCell(i);
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
rowIndex++;
row = sheet.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("機型:單相 功率:2.2kw 電流:1.8A 測試時間:2023/04/20 耗時:3分鐘");
for (int i = 1; i < 8; i++)
{
cell = row.CreateCell(i);
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
rowIndex++;
//記錄起始項
int startIndex = rowIndex;
//第一個小表格的標(biāo)題
row = sheet.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("品質(zhì)測試");
List<string> valueList = new List<string>()
{
"序號","測試項", "產(chǎn)品", "合格數(shù)據(jù)", "測試結(jié)果"
};
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i+1);
cell.SetCellValue (valueList[i]);
}
rowIndex++;
//第一條測試結(jié)果
row = sheet.CreateRow(rowIndex);
valueList.Clear();
valueList.AddRange(new string[]
{
"", "1", "通訊測試","成功", "", "/", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
rowIndex++;
//第二條測試結(jié)果
row = sheet.CreateRow(rowIndex);
valueList.Clear();
valueList.AddRange(new string[]
{
"", "2", "溫度測試","25℃", "", "23-30℃", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
rowIndex++;
//第三條測試結(jié)果
row = sheet.CreateRow(rowIndex);
valueList.Clear();
valueList.AddRange(new string[]
{
"", "3", "故障測試","E001", "", "/", "", "不合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
//品質(zhì)測試 要跨行
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
rowIndex++;
startIndex = rowIndex;
//第二個 小表格
row = sheet.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("性能測試");
valueList.Clear();
valueList.AddRange(new string[]
{
"序號","測試項", "產(chǎn)品", "對比項", "最小值" ,"最大值", "測試結(jié)果"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i+1);
cell.SetCellValue(valueList[i]);
}
rowIndex++;
//第一條測試結(jié)果
row = sheet.CreateRow(rowIndex);
valueList.Clear();
valueList.AddRange(new string[]
{
"", "1", "版本檢測","112", "112", "", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
}
rowIndex++;
//第二條測試結(jié)果
row = sheet.CreateRow(rowIndex);
valueList.Clear();
valueList.AddRange(new string[]
{
"","2", "帶載檢測","10.4/10.2", "", "10.1", "10.6", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
}
//品質(zhì)測試 要跨行
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
rowIndex++;
row = sheet.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("測試總結(jié)果:");
for (int i = 1; i < valueList.Count; i++)
{
cell = row.CreateCell(i+1);
if( i == 3)
{
cell.SetCellValue("不合格");
}
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 7));
using (FileStream file = new FileStream(@"測試結(jié)果.xls", FileMode.Create))
{
workBook.Write(file);
file.Close();
}
}
效果圖:
進階表格,單元格美化
設(shè)置行高列寬,對單元格的對齊方式、邊框、字體大小IFont
、顏色進行設(shè)置美化, 使用的是ICellStyle
.
先看一下最終的效果圖,由結(jié)果分析向上分析。
測試報告
行高30,居中顯示,字體: 黑體,16號,加粗, 設(shè)置單元格邊框。
//大標(biāo)題 黑體 16號 加邊框 居中顯示 行高30
string fontName = "黑體";
ICellStyle totalHeader = workBook.CreateCellStyle();
//垂直居中對齊
totalHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
totalHeader.VerticalAlignment = VerticalAlignment.Center;
//邊框
totalHeader.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體
IFont totalFont = workBook.CreateFont();
totalFont.FontName = fontName;
totalFont.FontHeightInPoints = 16;
totalFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
totalHeader.SetFont(totalFont);
第二行 機型
行高20, 居中顯示, 字體 :微軟雅黑12號 設(shè)置單元格邊框。
//中間行樣式 機型 行高20 居中顯示 字體 微軟雅黑12號 加邊框
ICellStyle lineStyle = workBook.CreateCellStyle();
//垂直居中對齊
lineStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
lineStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
lineStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體大小
IFont midFont = workBook.CreateFont();
midFont.FontHeightInPoints = 12;
midFont.FontName = "微軟雅黑";
lineStyle.SetFont(midFont);
左側(cè)跨單元格 品質(zhì)(性能)測試 總結(jié)果
居中顯示, 字體微軟雅黑12號加粗 ,設(shè)置單元格邊框。在單元格合并后再設(shè)置樣式。
//左側(cè)跨行單元格的樣式 居中顯示 字體微軟雅黑12號加粗 設(shè)置單元格邊框
ICellStyle leftCellStyle = workBook.CreateCellStyle();
//對齊
leftCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
leftCellStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
leftCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體大小
IFont leftFont = workBook.CreateFont();
leftFont.FontHeightInPoints = 12;
leftFont.FontName = "微軟雅黑";
leftFont.Boldweight = (short)(short)NPOI.SS.UserModel.FontBoldWeight.Bold; ;
leftCellStyle.SetFont(leftFont);
帶背景小標(biāo)題
行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色。
要想設(shè)置的背景色起作用,必須要設(shè)置 midTitleStyle.FillPattern = FillPattern.SolidForeground;
這句
//小標(biāo)題的樣式 行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色
ICellStyle midTitleStyle = workBook.CreateCellStyle();
//對齊
midTitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
midTitleStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
midTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//需要設(shè)置圖例,顏色才會起作用
midTitleStyle.FillPattern = FillPattern.SolidForeground;
//背景顏色
midTitleStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
midTitleStyle.SetFont(midFont);
中間的測試內(nèi)容
行高15,左對齊,設(shè)置單元格邊框。
//內(nèi)容的樣式 行高15 左對齊 設(shè)置單元格邊框
ICellStyle contentStyle = workBook.CreateCellStyle();
//對齊
contentStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
contentStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
contentStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
全部代碼文章來源:http://www.zghlxwxcb.cn/news/detail-421737.html
/// <summary>
/// 合并單元格
/// </summary>
void MergeCell()
{
//創(chuàng)建工作簿對象
HSSFWorkbook workBook = new HSSFWorkbook();
//創(chuàng)建一個sheet
ISheet sheet = workBook.CreateSheet("MergeTable");
//設(shè)置列寬
sheet.SetColumnWidth(0, 15 * 256);
sheet.SetColumnWidth(1, 10 * 256);
sheet.SetColumnWidth(2, 10 * 256);
sheet.SetColumnWidth(3, 10 * 256);
sheet.SetColumnWidth(4, 10 * 256);
sheet.SetColumnWidth(5, 10 * 256);
sheet.SetColumnWidth(7, 12 * 256);
IRow row;
ICell cell;
//大標(biāo)題 黑體 16號 加邊框 居中顯示 行高30
string fontName = "黑體";
ICellStyle totalHeader = workBook.CreateCellStyle();
//垂直居中對齊
totalHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
totalHeader.VerticalAlignment = VerticalAlignment.Center;
//邊框
totalHeader.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
totalHeader.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體
IFont totalFont = workBook.CreateFont();
totalFont.FontName = fontName;
totalFont.FontHeightInPoints = 16;
totalFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
totalHeader.SetFont(totalFont);
//行索引
int rowIndex = 0;
//標(biāo)題
row = sheet.CreateRow(rowIndex);
//設(shè)置行高
row.HeightInPoints = 30;
cell = row.CreateCell(0);
cell.SetCellValue("測試報告");
//設(shè)置單元格樣式
cell.CellStyle = totalHeader;
for (int i=1; i<8; i++)
{
cell = row.CreateCell(i);
//設(shè)置單元格樣式
cell.CellStyle = totalHeader;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
rowIndex++;
//中間行樣式 機型 行高20 居中顯示 字體 微軟雅黑12號 加邊框
ICellStyle lineStyle = workBook.CreateCellStyle();
//垂直居中對齊
lineStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
lineStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
lineStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
lineStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體大小
IFont midFont = workBook.CreateFont();
midFont.FontHeightInPoints = 12;
midFont.FontName = "微軟雅黑";
lineStyle.SetFont(midFont);
row = sheet.CreateRow(rowIndex);
//行高20
row.HeightInPoints = 20;
cell = row.CreateCell(0);
cell.SetCellValue("機型:單相 功率:2.2kw 電流:1.8A 測試時間:2023/04/20 耗時:3分鐘");
//設(shè)置樣式
cell.CellStyle = lineStyle;
for (int i = 1; i < 8; i++)
{
cell = row.CreateCell(i);
cell.CellStyle = lineStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
rowIndex++;
//小標(biāo)題的樣式 行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色
ICellStyle midTitleStyle = workBook.CreateCellStyle();
//對齊
midTitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
midTitleStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
midTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
midTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//需要設(shè)置圖例,顏色才會起作用
midTitleStyle.FillPattern = FillPattern.SolidForeground;
//背景顏色
midTitleStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
midTitleStyle.SetFont(midFont);
//內(nèi)容的樣式 行高15 左對齊 設(shè)置單元格邊框
ICellStyle contentStyle = workBook.CreateCellStyle();
//對齊
contentStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
contentStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
contentStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
contentStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//左側(cè)跨行單元格的樣式 居中顯示 字體微軟雅黑12號加粗 設(shè)置單元格邊框
ICellStyle leftCellStyle = workBook.CreateCellStyle();
//對齊
leftCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
leftCellStyle.VerticalAlignment = VerticalAlignment.Center;
//邊框
leftCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
leftCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
//字體大小
IFont leftFont = workBook.CreateFont();
leftFont.FontHeightInPoints = 12;
leftFont.FontName = "微軟雅黑";
leftFont.Boldweight = (short)(short)NPOI.SS.UserModel.FontBoldWeight.Bold; ;
leftCellStyle.SetFont(leftFont);
//記錄起始項
int startIndex = rowIndex;
//第一個小表格的標(biāo)題
row = sheet.CreateRow(rowIndex);
//行高
row.HeightInPoints = 20;
cell = row.CreateCell(0);
cell.SetCellValue("品質(zhì)測試");
List<string> valueList = new List<string>()
{
"序號","測試項", "產(chǎn)品", "", "合格數(shù)據(jù)","", "測試結(jié)果"
};
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i+1);
cell.SetCellValue (valueList[i]);
//樣式
cell.CellStyle = midTitleStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
rowIndex++;
//第一條測試結(jié)果
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 15;
valueList.Clear();
valueList.AddRange(new string[]
{
"", "1", "通訊測試","成功", "", "/", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
//內(nèi)容樣式
cell.CellStyle = contentStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
rowIndex++;
//第二條測試結(jié)果
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 15;
valueList.Clear();
valueList.AddRange(new string[]
{
"", "2", "溫度測試","25℃", "", "23-30℃", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
//內(nèi)容樣式
cell.CellStyle = contentStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
rowIndex++;
//第三條測試結(jié)果
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 15;
valueList.Clear();
valueList.AddRange(new string[]
{
"", "3", "故障測試","E001", "", "/", "", "不合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
//內(nèi)容樣式
cell.CellStyle = contentStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
//品質(zhì)測試 要跨行
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
sheet.GetRow(startIndex).GetCell(0).CellStyle = leftCellStyle;
rowIndex++;
startIndex = rowIndex;
//第二個 小表格
row = sheet.CreateRow(rowIndex);
//行高
row.HeightInPoints = 20;
cell = row.CreateCell(0);
cell.SetCellValue("性能測試");
valueList.Clear();
valueList.AddRange(new string[]
{
"序號","測試項", "產(chǎn)品", "對比項", "最小值" ,"最大值", "測試結(jié)果"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i+1);
cell.SetCellValue(valueList[i]);
cell.CellStyle = midTitleStyle;
}
rowIndex++;
//第一條測試結(jié)果
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 15;
valueList.Clear();
valueList.AddRange(new string[]
{
"", "1", "版本檢測","112", "112", "", "", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
//內(nèi)容樣式
cell.CellStyle = contentStyle;
}
rowIndex++;
//第二條測試結(jié)果
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 15;
valueList.Clear();
valueList.AddRange(new string[]
{
"","2", "帶載檢測","10.4/10.2", "", "10.1", "10.6", "合格"
});
for (int i = 0; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(valueList[i]);
//內(nèi)容樣式
cell.CellStyle = contentStyle;
}
//性能測試 要跨行
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
sheet.GetRow(startIndex).GetCell(0).CellStyle = leftCellStyle;
rowIndex++;
row = sheet.CreateRow(rowIndex);
row.HeightInPoints = 20;
cell = row.CreateCell(0);
cell.SetCellValue("測試總結(jié)果:");
cell.CellStyle = leftCellStyle;
for (int i = 1; i < valueList.Count; i++)
{
cell = row.CreateCell(i);
if( i == 3)
{
cell.SetCellValue("不合格");
}
cell.CellStyle = leftCellStyle;
}
//單元格合并 合并列
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 7));
sheet.GetRow(rowIndex).GetCell(0).CellStyle = leftCellStyle;
sheet.GetRow(rowIndex).GetCell(1).CellStyle = leftCellStyle;
using (FileStream file = new FileStream(@"測試結(jié)果.xls", FileMode.Create))
{
workBook.Write(file);
file.Close();
}
}
小結(jié)
1 單元格合并 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
2 設(shè)置行高 row.HeightInPoints = 20;
3 設(shè)置列寬 sheet.SetColumnWidth(0, 15 * 256);
4 樣式 ICellStyle leftCellStyle = workBook.CreateCellStyle();
5 字體 IFont font = workBook.CreateFont();文章來源地址http://www.zghlxwxcb.cn/news/detail-421737.html
到了這里,關(guān)于使用NPOI做Excel簡單報表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!