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 在项目中引入 ...
随机推荐
- 函数QFileSystemModelPrivate::_q_fileSystemChanged
由于Qt的体系过于庞大,即使是某个模块,分析起来也十分困难.对于QFileSystemModel,我们在这里对单个重要函数慢慢分析 1 /*! \internal The thread has rec ...
- centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡
# 用了nginx for win很久,安装也是超级简单.# 还是用一下linux版的吧.环境是centos 6.5 x64 # 安装开始: # 先安装依赖 yum install gcc-c++ y ...
- 【javascript小案例】从0开始实现一个俄罗斯方块
写在前面得话: 这篇文章主要记录了我是怎么一步一步写出俄罗斯方块,整个代码用的函数编程,主要是为了让一些不熟悉es6, 面向对象写法得 新手能更容易看明白,全部得代码中都是一些js的基础知识,很容易理 ...
- 左侧 随着页面滚动固定 fixed. scroll .scrollTop
1.图片. 要求:随着页面滚动 . 左侧应该顶着 浏览器顶部, 向上回滚, 就恢复原状. 2. 代码: html <div class="all "> <!-- ...
- datafactory5.6向mysql5.7添加大量测试数据
1.下载安装datafactory5.6 2.下载安装mysql5.7,并创建数据库guest_test和表sign_event 3.下载安装odbc5.3 4.打开datafactory配置数据源, ...
- SQL 姓名,联系方式-脱敏
SELECT ORDER_PROJECT.project_type AS attribute, ORDER_PROJECT.order_num, ,), "*") AS pv, C ...
- 开发Canvas 绘画应用(一):搭好框架
毕业汪今年要毕业啦,毕设做的是三维模型草图检索,年前将算法移植到移动端做了一个小应用(利用nodejs搭的服务),正好也趁此机会可以将前端的 Canvas 好好学一下~~毕设差不多做完了,现将思路和代 ...
- 爬虫基础之urllib库(代码演示)
# 自定义opener from urllib.request import ProxyHandler,build_opener from urllib.error import URLError ...
- 在c#中 RemoveAt、 Remove、delete用法区别
有三种方法可以删除 DataTable 中的 DataRow: Delete 方法和 Remove 方法和 RemoveAt 方法 其区别是: Delete 方法实际上不是从 DataTable 中删 ...
- 测试那些事儿-Jmeter介绍及使用
Jmeter与LR有啥区别? Jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码.(LR中的VuGen) 2.用户运行器:通常是一个脚本运行引擎,根据脚本要求模拟指 ...