/// <summary> /// 取得某一列的表头信息 /// </summary> /// <param name="rowMain"></param> /// <param name="rowType"></param> /// <param name="rowDesc"></param> /// <param name="col"></param> /// <returns></returns> ExcelHeader GetSingleExcelHeader(IRow rowMain, IRow rowType, IRow rowDesc, int col) { ExcelHeader header = new ExcelHeader(); header.Name = rowMain.GetCell(col).StringCellValue; header.Type = rowType.GetCell(col).StringCellValue; header.Describe = rowDesc.GetCell(col).StringCellValue; header.ColumnStart = col; header.ColumnEnd = col; return(header); }
void GenaratePropertyWithType(ref StringBuilder sb, ExcelHeader eh, string type, string name) { if (eh.ColumnStart == 0) { sb.Append("\t[ExcelIndex]\r\n"); } sb.Append("\t/// <summary>\r\n\t/// "); sb.Append(eh.Describe.Replace("\n", "").Replace("\r", "")); sb.Append("\r\n\t/// </summary>\r\n"); sb.Append("\tpublic "); sb.Append(type); sb.Append(" "); sb.Append(name); sb.Append(" {get; set;}\r\n"); }
/// <summary> /// 取得结构体的ExcelHeader /// </summary> /// <param name="rowDetail"></param> /// <param name="rowType"></param> /// <param name="rowDesc"></param> /// <param name="nStart"></param> /// <param name="nLen"></param> /// <returns></returns> List <ExcelHeader> GetStructHeader(IRow rowDetail, IRow rowType, IRow rowDesc, int nStart, int nLen) { List <ExcelHeader> headerList = new List <ExcelHeader>(); for (int i = nStart; i < nStart + nLen; i++) { ExcelHeader header = new ExcelHeader(); header.ColumnStart = i; header.ColumnEnd = i; header.Name = rowDetail.GetCell(i).StringCellValue; header.Type = rowType.GetCell(i).StringCellValue; header.Describe = rowDesc.GetCell(i).StringCellValue; headerList.Add(header); } return(headerList); }
void GenarateProperty(ref StringBuilder sb, ExcelHeader eh) { if (eh.ColumnStart == eh.ColumnEnd || eh.SubClassFieldLength == 1) { GenaratePropertyWithType(ref sb, eh, eh.Type, eh.Name); } else if (eh.SubNode.Count == 1) { GenarateSubClass(ref sb, eh.Type, eh.SubNode[0]); GenaratePropertyWithType(ref sb, eh, eh.Type, eh.Name); } else { GenarateSubClass(ref sb, eh.Name, eh.SubNode[0]); GenaratePropertyWithType(ref sb, eh, eh.Type, eh.Name + "List"); } }
/// <summary> /// 取得多列表头信息 /// </summary> /// <param name="rowMain"></param> /// <param name="rowType"></param> /// <param name="rowDesc"></param> /// <param name="cols"></param> /// <returns></returns> ExcelHeader GetMutiExcelHeader(IRow rowMain, IRow rowDetail, IRow rowType, IRow rowDesc, List <int> cols) { ExcelHeader header = new ExcelHeader(); header.ColumnStart = cols.Min(); header.ColumnEnd = cols.Max(); header.Describe = string.Empty; header.Name = rowMain.GetCell(header.ColumnStart).StringCellValue; header.SubNode = new Dictionary <int, List <ExcelHeader> >(); int structLength = GetMutiExcelStructLength(rowDetail, cols); header.SubClassFieldLength = structLength; for (var i = header.ColumnStart; i <= header.ColumnEnd; i += structLength) { header.SubNode[header.SubNode.Count] = GetStructHeader(rowDetail, rowType, rowDesc, i, structLength); } if (header.SubNode.Count == 1) { header.Type = header.Name + "Object"; } else { if (structLength == 1) { header.Type = "List<" + rowType.GetCell(header.ColumnStart).StringCellValue + ">"; } else { header.Type = "List<" + header.Name + ">"; } } return(header); }
public List <XElement> GetMutiHeaderXml(ExcelHeader header, IRow row) { var xEleAll = new List <XElement>(); if (header.SubNode == null) { return(xEleAll); } foreach (var subHeaderDic in header.SubNode) { if (subHeaderDic.Value.Count == 1) { try { for (int i = header.ColumnStart; i <= header.ColumnEnd; i++) { var cell = row.Cells.Find(x => x.ColumnIndex == i); if (cell == null || cell.CellType == CellType.Blank) { continue; } var xSub = new XElement(header.Name + "Element", GetCellValue(cell)); xEleAll.Add(xSub); } break; } catch (Exception ex) { throw new Exception("[" + row.Sheet.SheetName + "][" + header.Name + "] invalid", ex); } } else { try { var xSub = new XElement(header.Name + "Element"); bool bAdd = true; bool bFirst = true; foreach (var sh in subHeaderDic.Value) { var col = sh.ColumnStart; var cell = row.Cells.Find(x => x.ColumnIndex == col); if (bFirst && (cell == null || cell.CellType == CellType.Blank)) { bAdd = false; break; } bFirst = false; var xSubEle = new XElement(sh.Name, GetCellValue(cell)); xSub.Add(xSubEle); } if (bAdd) { xEleAll.Add(xSub); } } catch (Exception ex) { throw new Exception("[" + row.Sheet.SheetName + "][" + header.Name + "] invalid", ex); } } } return(xEleAll); }