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 在项目中引入 ...
随机推荐
- Dangerous well
Firsttime to develop games throuth Unity3d, such a great platform! You can build your games more qui ...
- python基础17_列表推导式 vs 生成器表达式
[ ] 列表推导式,是用简单的语法来生成列表, ( ) 生成器表达式,是用简单的语法创建个生成器. 外观上仅括号不一样. 虽然写起来方便,但是读起来稍显费力,另外,不易调试. # 列表推导式 prin ...
- Groovy学习笔记-陷阱
1.def和in是关键字 2.==映射到了equals() 中,如果有Comparable接口实现,则优先compareTo str1 = 'hello' str2 = str1 str3 = new ...
- js 回调函数理解
function A(callback) { console.log('我是主函数'); setTimeout(function () { callback("我是主函数传出的") ...
- <html> ---- position
position 固定的属性,是全局的.和DIV层次无关. <!DOCTYPE html> <html lang="en"> <head> &l ...
- CSS效果:不怎么样的登录表单
HTML: <html lang="en"> <head> <meta charset="UTF-8"> <meta ...
- 如何将新建的项目完整的提交到gitlab上?
描述: 使用idea开发工具新建了一个项目工程,此时此刻工程没有任何的版本控制,代码存放在本地磁盘中,如果需要直接将项目工程代码提交到gitlab上,该如何操作呢? 因为当前没有版本控制,不能像正常的 ...
- C#获取当前日期时间
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- python3学习笔记及常见问题
1,mac自带的python是2.7版本,我们需要按照python3,这样在terminal下可以直接使用,但是编译打包的时候会默认使用python2.7 解决办法:安装virtualenv,一个管理 ...
- Windows gitweb安装
/******************************************************************************* * Windows gitweb安装 ...