private IEnumerable <CellMarkEntity> ProcessMarkCell <T>(ISheet sheet, ref string errorMessage) where T : class, new() { IWorkbook workBook = sheet.Workbook; int NumberOfNames = workBook.NumberOfNames; List <CellMarkEntity> list = new List <CellMarkEntity>(); CellMarkEntity markEntity = null; T entity = new T(); PropertyInfo[] infos = entity.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); for (int index = 0; index < NumberOfNames; index++) { markEntity = new CellMarkEntity(); IName iname = workBook.GetNameAt(index); string region = iname.RefersToFormula.Replace(iname.SheetName, "").Replace("!", "").Replace("$", ""); string[] regions = region.Split(':'); int rowIndex = 0; int colIndex = 0; // 获取行位置和列位置 DealPostion(regions[0], ref rowIndex, ref colIndex); if (iname.NameName.StartsWith("&")) { markEntity.TransGain = true; } markEntity.CellIndex = colIndex; markEntity.RowIndex = rowIndex; markEntity.IName = iname; markEntity.TitleName = sheet.GetRow(rowIndex).GetCell(colIndex).ToString(); string name = iname.NameName.Replace("$", "").Replace("&", ""); IEnumerable <PropertyInfo> tempInfo = infos.Where(t => t.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (tempInfo != null && tempInfo.Any()) { PropertyInfo info = tempInfo.FirstOrDefault(); markEntity.ColumnName = info.Name; markEntity.PropertyInfo = info; IEnumerable <Attribute> attributes = info.GetCustomAttributes(); if (attributes != null && attributes.Any()) { markEntity.Attribute = attributes.FirstOrDefault(); } list.Add(markEntity); } else { errorMessage = "第【{0}】行第【{1}】列配置不正确,请联系管理员"; errorMessage = string.Format(errorMessage, rowIndex + 1, ExportExcelUtil.IndexToColName(colIndex)); break; } } return(list.AsEnumerable()); }
private CellDataEntity ProcessCell(ISheet sheet, CellMarkEntity item, IEnumerable <CellMergeEntity> cellMerges, IRow row = null) { CellDataEntity cellData = new CellDataEntity(); cellData.TitleName = item.TitleName; cellData.ColumnName = item.ColumnName; cellData.ColIndex = item.CellIndex; ICell cell = null; int rowIndex = 0; if (row == null) { rowIndex = item.RowIndex; cell = sheet.GetRow(item.RowIndex).GetCell(item.CellIndex + 1); } else { rowIndex = row.RowNum; cell = row.GetCell(item.CellIndex); } if (cell == null) { return(null); } cellData.RowIndex = rowIndex; if (cell.IsMergedCell == false) { cellData.CellPostion = string.Concat(ExportExcelUtil.IndexToColName(item.CellIndex), rowIndex + 1); Type originalType = item.PropertyInfo.PropertyType.GetGenericArguments()[0]; string cellValue = ""; switch (cell.CellType) { case CellType.Numeric: if (HSSFDateUtil.IsCellDateFormatted(cell)) { cellValue = cell.DateCellValue.ToString("yyyy-MM-dd HH:mm:ss.fff"); } else { cellValue = cell.NumericCellValue.ToString(); } break; case CellType.String: cellValue = cell.StringCellValue; break; case CellType.Boolean: cellValue = cell.BooleanCellValue.ToString(); break; case CellType.Formula: if (originalType == typeof(DateTime)) { cellValue = cell.DateCellValue.ToString("yyyy-MM-dd HH:mm:ss.fff"); } else { cell.SetCellType(CellType.String); cellValue = cell.ToString(); } break; } cellData.IsEmpty = string.IsNullOrEmpty(cellValue); cellData.CellValue = cellValue; //if (originalType == typeof(DateTime)) //{ // cellData.CellValue = cell.DateCellValue.ToString(); //} //else //{ // if (cell.CellType == CellType.Formula) // cell.SetCellType(CellType.String); // cellData.CellValue = cell.ToString(); //} } else { CellDimension dimension = ProcessCellDimension(cell, cellMerges); var merges = cellMerges.Where(t => t.FirstRow == dimension.FirstRowIndex && t.FirstColumn == dimension.FirstColIndex); cellData.CellPostion = string.Concat(ExportExcelUtil.IndexToColName(dimension.FirstColIndex), dimension.FirstRowIndex + 1); if (merges != null && merges.Any()) { string cellValue = merges.FirstOrDefault().DataCell.ToString(); cellData.CellValue = cellValue; } } cellData.Length = StringUtil.StrLength(cellData.CellValue); return(cellData); }