目录
本次Excel数据导出优点:
- 可以自由编辑 合并Excel单元格。
- 导出的Excel文件可以是.xlsx后缀,不比必须是.xls(原来的那个如果导出的文件是.xlsx后缀会报数据格式错误,导致无法正常打开文件,必须改为.xls才可以)。
- 可以自由添加多个标签页。
- 可以自由设计导出数据的字体样式单元格大小和边框等。
- 其余优点咱们使用者自行发觉。
代码:
/// <summary>
/// 导出成绩
/// </summary>
/// <returns></returns>
public async Task DeriveExcel(long PlanId)
{
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);
// 创建一个合并单元格的区域
// 行和列的索引从0开始 0,0,0,1分别表示单元格合并 从0行开始到0行结束,从0列开始到1列结束
CellRangeAddress mergedRegion = new CellRangeAddress(0, 0, 0, 3);
// 合并单元格
sheet1.AddMergedRegion(mergedRegion);
// 设置合并后的单元格样式
ICell mergedCell0 = row0.CreateCell(0);
mergedCell0.CellStyle = style1;
// 设置合并后的单元格的值
mergedCell0.SetCellValue("基本信息");
// 设置合并后的单元格的边框样式(可选)
RegionUtil.SetBorderBottom((int)BorderStyle.Thin, mergedRegion, sheet1);
RegionUtil.SetBorderLeft((int)BorderStyle.Thin, mergedRegion, sheet1);
RegionUtil.SetBorderRight((int)BorderStyle.Thin, mergedRegion, sheet1);
RegionUtil.SetBorderTop((int)BorderStyle.Thin, mergedRegion, sheet1);
CellRangeAddress mergedRegion1 = new CellRangeAddress(0, 0, 4, 9);
sheet1.AddMergedRegion(mergedRegion1);
ICell mergedCell1 = row0.CreateCell(4);
mergedCell1.CellStyle = style1;
mergedCell1.SetCellValue("考核细项");
RegionUtil.SetBorderBottom((int)BorderStyle.Thin, mergedRegion1, sheet1);
RegionUtil.SetBorderLeft((int)BorderStyle.Thin, mergedRegion1, sheet1);
RegionUtil.SetBorderRight((int)BorderStyle.Thin, mergedRegion1, sheet1);
RegionUtil.SetBorderTop((int)BorderStyle.Thin, mergedRegion1, sheet1);
CellRangeAddress mergedRegion2 = new CellRangeAddress(0, 0, 10, 12);
sheet1.AddMergedRegion(mergedRegion2);
ICell mergedCell2 = row0.CreateCell(10);
mergedCell2.CellStyle = style1;
mergedCell2.SetCellValue("组织信息");
RegionUtil.SetBorderBottom((int)BorderStyle.Thin, mergedRegion2, sheet1);
RegionUtil.SetBorderLeft((int)BorderStyle.Thin, mergedRegion2, sheet1);
RegionUtil.SetBorderRight((int)BorderStyle.Thin, mergedRegion2, sheet1);
RegionUtil.SetBorderTop((int)BorderStyle.Thin, mergedRegion2, sheet1);
IRow rowi = sheet1.CreateRow(1);
rowi.CreateCell(0).SetCellValue("身份证号");
//获取第一行第一列并设置样式
rowi.GetCell(0).CellStyle = style1;
rowi.CreateCell(1).SetCellValue("姓名");
rowi.GetCell(1).CellStyle = style1;
rowi.CreateCell(2).SetCellValue("满分");
rowi.GetCell(2).CellStyle = style1;
rowi.CreateCell(3).SetCellValue("得分");
rowi.GetCell(3).CellStyle = style1;
rowi.CreateCell(4).SetCellValue("风口结焦");
rowi.GetCell(4).CellStyle = style1;
rowi.CreateCell(5).SetCellValue("风口挂渣");
rowi.GetCell(5).CellStyle = style1;
rowi.CreateCell(6).SetCellValue("吹管法兰处发红");
rowi.GetCell(6).CellStyle = style1;
rowi.CreateCell(7).SetCellValue("煤枪软管烧穿");
rowi.GetCell(7).CellStyle = style1;
rowi.CreateCell(8).SetCellValue("风口烧出");
rowi.GetCell(8).CellStyle = style1;
rowi.CreateCell(9).SetCellValue("吹管烧出");
rowi.GetCell(9).CellStyle = style1;
rowi.CreateCell(10).SetCellValue("部门");
rowi.GetCell(10).CellStyle = style1;
rowi.CreateCell(11).SetCellValue("岗位");
rowi.GetCell(11).CellStyle = style1;
rowi.CreateCell(12).SetCellValue("工种");
rowi.GetCell(12).CellStyle = style1;
var csore = _userScoreRep.Where(u => u.PlanId == PlanId).ToList();
var groupedData = (from item in _userScoreRep.AsQueryable().Where(u=> u.PlanId == PlanId)
group item by new { item.PlanId, item.UserId } into grouped
select new
{
PlanId = grouped.Key.PlanId,
UserId = grouped.Key.UserId
}).ToList();
int sum = 2;
foreach (var item in groupedData)
{
IRow rowi1 = sheet1.CreateRow(sum);
var fen = _userScoreRep.Where(u => u.PlanId == item.PlanId && u.UserId == item.UserId).ToList();
var user = _sysUserRep.FirstOrDefault(u => u.Id == item.UserId);
double fs = 0;
double zf = 0;
rowi1.CreateCell(0).SetCellValue(user.Account);
rowi1.CreateCell(1).SetCellValue(user.NickName);
foreach (var item1 in fen)
{
var pra = _practicalSetRep.FirstOrDefault(u => u.Id == item1.ApracticalSetId);
if (pra.ApracticalName == "风口结焦异常处理")
{
rowi1.CreateCell(4).SetCellValue((double)item1.Total);
fs = fs+(double)item1.Total;
zf = zf+(double)pra.Proportion;
}
else if (pra.ApracticalName == "风口挂渣异常处理")
{
rowi1.CreateCell(5).SetCellValue((double)item1.Total);
fs = fs + (double)item1.Total;
zf = zf + (double)pra.Proportion;
}
else if (pra.ApracticalName == "吹管平口法兰处发红")
{
rowi1.CreateCell(6).SetCellValue((double)item1.Total);
fs = fs + (double)item1.Total;
zf = zf + (double)pra.Proportion;
}
else if (pra.ApracticalName == "严重烧穿")
{
if (item1.ziYC_Type == 0)
{
rowi1.CreateCell(7).SetCellValue(0);
rowi1.CreateCell(8).SetCellValue(0);
rowi1.CreateCell(9).SetCellValue(0);
}
else if (item1.ziYC_Type == 1)
{
rowi1.CreateCell(7).SetCellValue((double)item1.Total);
fs = fs + (double)item1.Total;
zf = zf + (double)pra.Proportion;
}
else if (item1.ziYC_Type == 2)
{
rowi1.CreateCell(8).SetCellValue((double)item1.Total);
fs = fs + (double)item1.Total;
zf = zf + (double)pra.Proportion;
}
else if (item1.ziYC_Type == 3)
{
rowi1.CreateCell(9).SetCellValue((double)item1.Total);
fs = fs + (double)item1.Total;
zf = zf + (double)pra.Proportion;
}
}
}
rowi1.CreateCell(2).SetCellValue(zf);
rowi1.CreateCell(3).SetCellValue(fs);
rowi1.CreateCell(10).SetCellValue(user.Department);
rowi1.CreateCell(11).SetCellValue(user.Position);
rowi1.CreateCell(12).SetCellValue(user.JobType);
sum++;
}
var cas = groupedData;
string sFileName = Path.Combine($"{Directory.GetCurrentDirectory()}/wwwroot/Upload/TheTemplate/成绩单.xlsx");
using (FileStream filestream = new FileStream(Path.Combine(sFileName), FileMode.Create))
{
wb.Write(filestream);
filestream.Close();
}
}
代码解释:
CellRangeAddress mergedRegion = new CellRangeAddress(0, 0, 0, 3);
表示:0003意思是说从Excel第1行开始到第1行结束,合并单元格列从第1列开始到第4列结束。之所以是是0003是因为下标是从0开始的(整体表示:第1行1列开始到第1行4列合并单元格)
运行结果:

本次成绩导出功能是根据之前发布的那个的补充。
这段代码展示了如何使用C#的ApachePOI库创建一个包含多个标签页、可自定义编辑的Excel文件,包括合并单元格、设置字体样式、边框以及内容。程序能导出.xlsx格式的文件,无需转换即可打开,且支持组织信息、考核细项等多类型数据的展示。
3235





