/// <summary> /// 解析文档 /// </summary> /// <param name="sheet">导出文档</param> /// <returns></returns> private static ExportingDocument ParseDocument(ISheet sheet) { var document = new ExportingDocument(); for (var r = 0; r <= sheet.LastRowNum; r++) { var row = sheet.GetRow(r); if (row == null) { break; } SheetDataSet set = null; for (var c = 0; c < row.LastCellNum; c++) { var cell = row.GetCell(c); if (cell == null) { break; } if (cell.CellType == CellType.String && !string.IsNullOrEmpty(cell.StringCellValue)) { var text = cell.StringCellValue; var si = text.IndexOf('['); var ei = text.IndexOf(']', si + 1); if (si != -1 && ei != -1) { set = ParseDataSet(cell, set, text.Substring(si, ei - si + 1), text, r); } else if (text.IndexOf("`") != -1) { document.Formulas.Add(ParseFormula(cell, text)); } else if (text == "{Extend}") { document.Extend = cell; } } } if (set != null) { document.DataSets.Add(set); } } return(document); }
/// <summary> /// 将数据填充到工作表中。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sheet"></param> /// <param name="data">列表数据。</param> /// <param name="document">解析的文档对象。</param> /// <param name="source">源行,即定义字段标签的行。</param> private static void FillSheet(ISheet sheet, IList data, ExportingDocument document, IRow source) { var index = 0; var mergedCellIndex = -1; foreach (var item in data) { var row = index == 0 ? sheet.GetRow(source.RowNum) : sheet.CopyRow(source.RowNum, source.RowNum + index); foreach (var ckv in document.DataSets[0].Properties) { ICell cell = null; if ((cell = row.GetCell(ckv.Key.ColumnIndex)) == null) { cell = row.CreateCell(ckv.Key.ColumnIndex); } if (index != 0) { var scell = source.GetCell(ckv.Key.ColumnIndex); row.Height = source.Height; cell.CellStyle = scell.CellStyle; } else { if (cell.IsMergedCell) { mergedCellIndex = ckv.Key.ColumnIndex; } } var properties = TypeDescriptor.GetProperties(item); var property = properties[ckv.Value.Key]; if (property == null) { continue; } var value = property == null ? string.Empty : property.GetValue(item); SetCellValue(cell, ckv.Value, value); } index++; } }