/// <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. Shell 使用 expr 进行数学运算

    1.语法格式: 第一种:expr $num1 operator $num2 第二种:$(($num1 operator $num2)) 2.expr 操作符: 注意:这里比较为true,返回 1.只支 ...

  2. top和nvidia-smi无法显示占用GPU的PID问题

    通过nvidia-smi查看显卡使用情况,发现显卡在被占用,但是却没有提示占用显卡的进程id, 这时可以输入 fuser -v /dev/nvidia* 可以查看到, 再利用sudo kill -9 ...

  3. c语言——鞍点

    描述 找出具有m行n列二维数组Array的“鞍点”,即该位置上的元素在该行上最大,在该列上最小,其中1<=m,n<=10. 输入 输入数据有多行,第一行有两个数m和n,下面有m行,每行有n ...

  4. Centos7_64环境搭建

    smb搭建参考 https://www.cnblogs.com/areyouready/p/10369917.html activeMq搭建参考 https://blog.csdn.net/u0122 ...

  5. c++单链表冒泡排序(交换结点),链表增删改查,运算符重载

    #include <iostream> #include <stdlib.h> #include <time.h> #include <fstream> ...

  6. amazeUI的confirm控件记录缓存问题的解决办法

    场景:列表行每行都有删除按钮,点击删除按钮将行记录的id传给js方法,js方法中调用amazeui的confirm控件,确认删除function通过ajax执行删除行为. 问题现象:每次删除列表第一行 ...

  7. Sql 查询当天、本周、本月记录、上周、上月记录

    查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 查询24小时内: select * from info where D ...

  8. Including R code in perl

    #example: use Statistics::R;#use R in perlmy $R = Statistics::R->new() ;$R->startR ;$R->sen ...

  9. js中Array数组的属性和方法

    这是我自己整理出来的一些关于Array数组的属性和方法,即查即用. 1.Array.length属性:数组的项数组,始终返回0或者更大的值. 2.instanceof操作符:value instanc ...

  10. sublime text 3搭建python

    1.ST3下载地址: http://www.sublimetext.com/3 2.安装Sublime Text Build 3114 Setup.exe应用程序. 3.ST3的工具优点就是轻量级,简 ...