Excel文件上传,高亮错误的行和列
/// <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文件上传,高亮错误的行和列的更多相关文章
- [SAP ABAP开发技术总结]客户端文本文件、Excel文件上传下载
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]文本文件、Excel文件上传下传
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- poi excel文件上传并解析xls文件
1.jsp页面 <form action="hw/pe_xls_upload" method="post" enctype="multipart ...
- php部分---文件上传:错误处理、 客户端和服务器端的限制
1.客户端页面 <!---客户端的配置 1.表单页面 2.表单发送方式为post 3.表单form中添加enctype="multipart/form-data" ----- ...
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
----------------------------------------------------------------------------------------------[版权申明: ...
- PHP文件上传、错误处理
说明 这篇是针对之前php知识的补充内容 目录 说明 1. PHP目录处理函数 2. PHP文件权限设置 3. PHP文件路径函数 4. PHP实现文件留言本 5.PHP文件上传 1. php文件上传 ...
- ABAP EXCEL 文件上传下载 用SMW0
T-CODE: SMW0 在这里只介绍二进制数据,HTML模板的上传也一样. 另外也可以用CBO TABLE管理文件 可以看我另一个博文:CBO TABLE管理文件上传下载 选择 二进制 写包名: 进 ...
- Spring MVC文件上传出现错误:Required MultipartFile parameter 'file' is not present
1.配置文件上传的解析器 首先需要在spring mvc的配置文件中(注意是spring mvc的配置文件而不是spring的配置文件:applicationContext.xml)配置: sprin ...
- React + js-xlsx构建Excel文件上传预览功能
首先要准备react开发环境以及js-xlsx插件 1. 此处省略安装react安装步骤 2.下载js-xlsx插件 yarn add xlsx 或者 npm install xlsx 在项目中引入 ...
随机推荐
- 使用Pillow库 创建简单验证码
使用Pillow生成简单的验证码 本想做成字体各自按随机角度倾斜, 但没有在Pillow中找到相关的方法 import randomfrom PIL import Image, ImageDraw, ...
- 已经在Git Server服务器上导入了SSH公钥,可用TortoiseGit同步代码时,还是提示输入密码?
GitHub虽好,但毕竟在国内访问不是很稳定,速度也不快,而且推送到上面的源码等资料必须公开,除非你给他交了保护费:所以有条件的话,建议大家搭建自己的Git Server.本地和局域网服务器都好,不信 ...
- pytorch查看模型weight与grad
在用pdb debug的时候,有时候需要看一下特定layer的权重以及相应的梯度信息,如何查看呢? 1. 首先把你的模型打印出来,像这样 2. 然后观察到model下面有module的key,modu ...
- Codeforces1099D.Sum in the tree(贪心)
题目链接:传送门 思路: 一个节点放的数越大,那么以它为根的子树的节点权值之和就越小. 所以我们要在合法的范围内,使偶数层节点的权值尽可能地大.也就是说,令它的权值是子节点的最小值,这样保证了它的子节 ...
- Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么(转)
Web 建站技术中,HTML.HTML5.XHTML.CSS.SQL.JavaScript.PHP.ASP.NET.Web Services 是什么?修改 建站有很多技术,如 HTML.HTML5.X ...
- Python中的常用魔术方法介绍
1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...
- getParameter和getAttribute的区别
1.getAttribute 是取得jsp中 用setAttribute 设定的attribute 2.parameter得到的是String:attribute得到的是object 3.reques ...
- 网络编程三 Socket
1.根据netstat端口的找到进程号---->根据进程号找到进程名称-------->终止进程 1) netstat 最后一列是5432 C:\Users\Administrato ...
- K-means算法应用:图片压缩
plt.imshow(china[:,:,2]) plt.show() from sklearn.datasets import load_sample_image china=load_sample ...
- c# excel xlsx 保存
public XSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle) { X ...