NPOI导入每行带图片的EXCEL
来源:互联网 发布:nginx添加模块 编辑:程序博客网 时间:2024/05/16 09:56
最近做一个EXCEL 导入的功能,导入一些人员的基本信息,其中一项是人员的照片,经查资料发现在EXCEL中照片和单元格之间并无对应的关系,即使照片在某个单元格内,所以在读取的时候照片那一列读出来的全是空,经研究查找后找到了一种解决的方式,通过计算得出照片在EXCEL 中的单元格,大概过程如下:
一、首先引用NPOI相关的dll
二、在程序中添加NPOI相关的命名空间
三、定义照片信息类
public class PicturesInfo { public int MinRow { get; set; } public int MaxRow { get; set; } public int MinCol { get; set; } public int MaxCol { get; set; } public Byte[] PictureData { get; private set; } public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData) { this.MinRow = minRow; this.MaxRow = maxRow; this.MinCol = minCol; this.MaxCol = maxCol; this.PictureData = pictureData; } }
四、计算照片的位置类
public static class NpoiExtend { public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet) { return sheet.GetAllPictureInfos(null, null, null, null); } public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true) { if (sheet is HSSFSheet) { return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal); } else if (sheet is XSSFSheet) { return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal); } else { throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!"); } } private static List<PicturesInfo> GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal) { List<PicturesInfo> picturesInfoList = new List<PicturesInfo>(); var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer; if (null != shapeContainer) { var shapeList = shapeContainer.Children; foreach (var shape in shapeList) { if (shape is HSSFPicture) { var picture = (HSSFPicture)shape; // var anchor = (HSSFClientAnchor)shape; var anchor = (HSSFClientAnchor)picture.Anchor; if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal)) { picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data)); } } } } return picturesInfoList; } private static List<PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal) { List<PicturesInfo> picturesInfoList = new List<PicturesInfo>(); var documentPartList = sheet.GetRelations(); foreach (var documentPart in documentPartList) { if (documentPart is XSSFDrawing) { var drawing = (XSSFDrawing)documentPart; var shapeList = drawing.GetShapes(); foreach (var shape in shapeList) { if (shape is XSSFPicture) { var picture = (XSSFPicture)shape; var anchor = picture.GetPreferredSize(); if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal)) { picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data)); } } } } } return picturesInfoList; } private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol, int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal) { int _rangeMinRow = rangeMinRow ?? pictureMinRow; int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow; int _rangeMinCol = rangeMinCol ?? pictureMinCol; int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol; if (onlyInternal) { return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow && _rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol); } else { return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) && (Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol))); } } }
五、应用
public ActionResult GetTableFromExcel() { try { HttpFileCollectionBase file = Request.Files; HttpPostedFileBase fileData = file[0]; if (fileData != null) { if (fileData.ContentLength == 0) { return Content("{'success':'false','msg':'并无上传的文件'}"); } } Stream streamfile = fileData.InputStream; XSSFWorkbook hssfworkbook = new XSSFWorkbook(streamfile);//2007以上版本 // HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile); NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(0);//第一行为标题行 int cellCount = headerRow.LastCellNum;//LastCellNum = PhysicalNumberOfCells int rowCount = sheet.LastRowNum;//LastRowNum = PhysicalNumberOfRows - 1 //handling header. for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); if (row != null) { for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) dataRow[j] = GetCellValue(row.GetCell(j)); } } table.Rows.Add(dataRow); } List<BS_Identify> identify_list = new List<BS_Identify>(); for (int i = 0; i < table.Rows.Count; i++) { BS_Identify identify = new BS_Identify(); identify.XM = table.Rows[i][0].ToString(); //读取除了照片列以外的数据 identify_list.Add(identify); //myUpLoadBLL.ForDownLoad(table.Rows[i][1].ToString(), table.Rows[i][2].ToString(),Convert.ToBoolean( table.Rows[i][3])); } List<PicturesInfo> picture_list = new List<PicturesInfo>(); picture_list = NpoiExtend.GetAllPictureInfos(sheet, 1, rowCount, 0, cellCount, false); foreach (var a in picture_list) { identify_list[a.MinRow-1].Img = a.PictureData; } //操作读取的数据 return Content("{'success':'true','msg':''}"); } catch(Exception ex) { return Content("{'success':'false','msg':"+ex.Message); } } /// <summary> /// 根据Excel列类型获取列的值 /// </summary> /// <param name="cell">Excel列</param> /// <returns></returns> private static string GetCellValue(ICell cell) { if (cell == null) return string.Empty; switch (cell.CellType) { case CellType.Blank: return string.Empty; case CellType.Boolean: return cell.BooleanCellValue.ToString(); case CellType.Error: return cell.ErrorCellValue.ToString(); case CellType.Numeric: case CellType.Unknown: default: return cell.ToString(); case CellType.String: return cell.StringCellValue; case CellType.Formula: try { HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook); e.EvaluateInCell(cell); return cell.ToString(); } catch { return cell.NumericCellValue.ToString(); } } }
1 0
- NPOI导入每行带图片的EXCEL
- NPOI 导入EXECEL数据包括每行有图片
- 使用NPOI 导入导出Excel的类
- Excel导入导出NPOI的使用
- NPOI Excel导入导出
- C# NPOI导入Excel
- 获取excel中每行的图片
- Npoi导入导出到Excel
- EXCEL导入数据(NPOI)
- NPOI导入EXCEL数据类
- 关于用NPOI导入Excel
- 利用NPOI操作excel导入
- .NET调用NPOI组件导入导出Excel的操作类
- NPOI实践: .NET导入Excel文件的另一种选择
- 使用NPOI实现word和excel的导入导出
- 基于NPOI导入导出excel的工具,方便灵活
- npoi常用方法,excel表的导入导出
- 使用NPOI将Excel文件的数据导入数据库
- CSS中的变量使用,var()语法
- 美丽夜色
- iOS UILabel自适应高度
- 学期总结
- 算法学习一(基础)
- NPOI导入每行带图片的EXCEL
- Matlab与线性代数 -- 对角矩阵
- squid 访问控制
- 第5章 脚本控制------------------------(作业控制、查看作业jobs命令、重新启动停止的作业)
- AngularJS 脏检查机制
- 【第16周项目1-基数排序】
- 如何让Embed Tomcat和Netty支持HTTPS
- 第十六周项目2-大数据集上排序算法性能的体验
- sqlalchemy(一)基本操作