/// <summary>
/// Excel模板写入错误信息
/// </summary>
/// <param name="fileName"></param>
/// <param name="errorAllList"></param>
public void ErrorWriteExcel(string fileName, List<RowCellSheetMessage> errorAllList)
{
FileStream fs = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
IWorkbook workbook = WorkbookFactory.Create(fs);
fs.Dispose(); ICellStyle rowCellStyle = workbook.CreateCellStyle();
rowCellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
rowCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
rowCellStyle.FillPattern = FillPattern.SolidForeground;
rowCellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Pink.Index; ICellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
cellStyle.FillPattern = FillPattern.SolidForeground;
cellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Pink.Index; for (int i = 0; i < workbook.NumberOfSheets; i++)
{
ISheet sheet = workbook.GetSheetAt(i);
IRow headerRow = sheet.GetRow(0);
if (!errorAllList.Exists(l => l.SheetName.Contains(sheet.SheetName)))
{
continue;
} List<RowCellSheetMessage> currentErrorList = errorAllList.Where(l => l.SheetName == sheet.SheetName).ToList();
for (int j = 0; j < sheet.PhysicalNumberOfRows; j++)
{
//获取Excel行
IRow row = sheet.GetRow(j);
if (row == null)
{
row = sheet.CreateRow(j); ;
} for (int r = 0; r < headerRow.PhysicalNumberOfCells; r++)
{
ICell cell = row.GetCell(r);
if (cell == null)
{
cell = row.CreateCell(r);
cell.SetCellValue("");
} if (currentErrorList.Select(l => l.RowNum).Contains(j))
{
cell.CellStyle = rowCellStyle;
}
}
} //获取Excel列
foreach (var item in currentErrorList)
{
ICell cell = sheet.GetRow(item.RowNum).GetCell(item.CellNum); IDrawing patr = sheet.CreateDrawingPatriarch();
IComment comment = patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 3, 3, 8, 8));
comment.String = new HSSFRichTextString(item.Message);
cell.CellComment = comment; cell.CellStyle = cellStyle;
}
} // 创建错误Sheet
if (errorAllList.Count > 0)
{
var errorDictionary = errorAllList
.GroupBy(l => new { l.SheetName, l.RowNum })
.Select(l => new { Key = l.Key, Value = l })
.ToDictionary(l => l.Key, v => v.Value);
int maxErrorCount = errorDictionary.Values.Max(l => l.Count()); #region 设置错误Sheet的标题行
ISheet errorSheet = workbook.CreateSheet("错误说明");
IRow errorRow = errorSheet.CreateRow(0);
ICell errorCell1 = errorRow.CreateCell(0, CellType.String);
errorCell1.SetCellValue("Sheet名称");
ICell errorCell2 = errorRow.CreateCell(1, CellType.String);
errorCell2.SetCellValue("Excel行号");
for (int i = 0; i < maxErrorCount; i++)
{
ICell errorCell3 = errorRow.CreateCell(i + 2, CellType.String);
errorCell3.SetCellValue("错误说明" + (i + 1));
errorSheet.SetColumnWidth(i + 2, 20 * 256);
}
errorSheet.SetColumnWidth(0, 20 * 256);
errorSheet.SetColumnWidth(1, 10 * 256);
#endregion #region 设置错误行标题的样式
errorRow.HeightInPoints = 20; ICellStyle errorStyle = workbook.CreateCellStyle();
errorStyle.BorderBottom = BorderStyle.Thin;
errorStyle.BorderTop = BorderStyle.Thin;
errorStyle.BorderLeft = BorderStyle.Thin;
errorStyle.BorderRight = BorderStyle.Thin;
errorStyle.Alignment = HorizontalAlignment.Center;
errorStyle.VerticalAlignment = VerticalAlignment.Center;
errorStyle.FillForegroundColor = HSSFColor.LightYellow.Index;
errorStyle.FillPattern = FillPattern.SolidForeground; IFont errorFont = workbook.CreateFont();
errorFont.Color = HSSFColor.Red.Index;
errorFont.FontHeightInPoints = 9;
errorStyle.SetFont(errorFont); for (int i = 0; i < errorRow.PhysicalNumberOfCells; i++)
{
ICell errorCell = errorRow.GetCell(i);
errorCell.CellStyle = errorStyle;
}
#endregion #region 为错误Sheet赋值
int error_Row_Index = 0;
foreach (var item in errorDictionary)
{
error_Row_Index++;
errorRow = errorSheet.CreateRow(error_Row_Index); errorCell1 = errorRow.CreateCell(0, CellType.String);
errorCell1.SetCellValue(item.Key.SheetName);
errorCell2 = errorRow.CreateCell(1, CellType.String);
errorCell2.SetCellValue(item.Key.RowNum); List<RowCellSheetMessage> currentRowErrorList = item.Value.ToList();
int error_Cell_Index = 1;
foreach (var errorItem in currentRowErrorList)
{
error_Cell_Index++;
ICell errorCell3 = errorRow.CreateCell(error_Cell_Index, CellType.String);
errorCell3.SetCellValue(errorItem.Message);
} errorStyle = workbook.CreateCellStyle();
errorStyle.BorderBottom = BorderStyle.Thin;
errorStyle.BorderTop = BorderStyle.Thin;
errorStyle.BorderLeft = BorderStyle.Thin;
errorStyle.BorderRight = BorderStyle.Thin;
errorStyle.Alignment = HorizontalAlignment.Center;
errorStyle.VerticalAlignment = VerticalAlignment.Center;
errorFont = workbook.CreateFont();
errorFont.FontHeightInPoints = 9;
errorStyle.SetFont(errorFont); for (int i = 0; i < errorRow.PhysicalNumberOfCells; i++)
{
ICell errorCell = errorRow.GetCell(i);
errorCell.CellStyle = errorStyle;
}
}
#endregion
} using (FileStream fsWrite = File.OpenWrite(fileName))
{
workbook.Write(fsWrite);
} workbook = null;
}

  

[Serializable]
public class RowCellSheetMessage
{
/// <summary>
/// Excel行号 从下标0开始
/// </summary>
public int RowNum { get; set; } /// <summary>
/// Excel列号 从下标0开始
/// </summary>
public int CellNum { get; set; } /// <summary>
/// Sheet名称
/// </summary>
public string SheetName { get; set; } /// <summary>
/// 错误信息
/// </summary>
public string Message { get; set; }
}

  效果图:

Excel文件上传,高亮错误的行和列的更多相关文章

  1. [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. [SAP ABAP开发技术总结]文本文件、Excel文件上传下传

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. poi excel文件上传并解析xls文件

    1.jsp页面 <form action="hw/pe_xls_upload" method="post" enctype="multipart ...

  4. php部分---文件上传:错误处理、 客户端和服务器端的限制

    1.客户端页面 <!---客户端的配置 1.表单页面 2.表单发送方式为post 3.表单form中添加enctype="multipart/form-data" ----- ...

  5. 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现

    ----------------------------------------------------------------------------------------------[版权申明: ...

  6. PHP文件上传、错误处理

    说明 这篇是针对之前php知识的补充内容 目录 说明 1. PHP目录处理函数 2. PHP文件权限设置 3. PHP文件路径函数 4. PHP实现文件留言本 5.PHP文件上传 1. php文件上传 ...

  7. ABAP EXCEL 文件上传下载 用SMW0

    T-CODE: SMW0 在这里只介绍二进制数据,HTML模板的上传也一样. 另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载 选择 二进制 写包名: 进 ...

  8. Spring MVC文件上传出现错误:Required MultipartFile parameter 'file' is not present

    1.配置文件上传的解析器 首先需要在spring mvc的配置文件中(注意是spring mvc的配置文件而不是spring的配置文件:applicationContext.xml)配置: sprin ...

  9. React + js-xlsx构建Excel文件上传预览功能

    首先要准备react开发环境以及js-xlsx插件 1. 此处省略安装react安装步骤 2.下载js-xlsx插件 yarn add xlsx 或者 npm install xlsx 在项目中引入 ...

随机推荐

  1. case 函数的简单使用记录下

    Case有2中格式:简单Case函数和Case搜索函数. 简单函数:case sex when '1' then '男' when '2' then ‘女’ else  '其它' end;(sex是列 ...

  2. CSS效果:简单的登录框

    HTML: <html lang="en"> <head> <meta charset="UTF-8"> <meta ...

  3. 剑指Offer 64. 滑动窗口的最大值 (其他)

    题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6 ...

  4. 微信小程序上传文件遇到的坑

    在开发小程序时,使用的花生壳做的内网映射,域名使用花生壳卖的https域名 在做小程序文件上传时,调用接口,老是报错. Caused by: org.apache.commons.fileupload ...

  5. select 下拉选择自动到textarea框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. jQuery-4.动画篇---淡入淡出效果

    jQuery中淡出动画fadeOut 让元素在页面不可见,常用的办法就是通过设置样式的display:none.除此之外还可以一些类似的办法可以达到这个目的.这里要提一个透明度的方法,设置元素透明度为 ...

  7. AC, FVOCI, FVPL

    IFRS9 会计新准则中规定了资产划分依据为合同现金流特征及业务模式. 关键词:资产负债表,利润表,损益,利率风险 AC账户: 为收取合同约定的现金流.持有到期业务. 资产在持有期内不变,使用摊余成本 ...

  8. 上传文件代码报错,java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

    2018-09-11 11:11:08.235 ERROR 14352 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : ...

  9. numpy鸢尾花

    import numpy from sklearn.datasets import load_iris #从sklearn包自带的数据集中读出鸢尾花数据集data iris_data = load_i ...

  10. python 迭代器 一个奇怪的解决方法

    一般我们在类里面写迭代器都是如下写法: class IterableSomthing: def __iter__(self): return self def __next__(self): retu ...