/// <summary> /// 添加样式 /// </summary> /// <param name="exporterHeaders"></param> /// <param name="sheet"></param> protected void AddStyle(ExcelExporterAttribute exporter, List <ExporterHeaderInfo> exporterHeaders, ExcelWorksheet sheet) { foreach (var exporterHeader in exporterHeaders) { if (exporterHeader.ExporterHeader != null) { if (exporterHeader.ExporterHeader.IsIgnore) { //TODO:后续直接修改数据导出逻辑(不写忽略列数据) sheet.DeleteColumn(exporterHeader.Index); //删除之后,序号依次-1 foreach (var item in exporterHeaders.Where(p => p.Index > exporterHeader.Index)) { item.Index--; } continue; } var col = sheet.Column(exporterHeader.Index); col.Style.Numberformat.Format = exporterHeader.ExporterHeader.Format; if (exporter.AutoFitAllColumn || exporterHeader.ExporterHeader.IsAutoFit) { col.AutoFit(); } } } }
/// <summary> /// 创建表头 /// </summary> /// <param name="exporterHeaderDtoList"></param> /// <param name="sheet"></param> protected void AddHeader(List <ExporterHeaderInfo> exporterHeaderDtoList, ExcelWorksheet sheet, ExcelExporterAttribute exporter) { foreach (var exporterHeaderDto in exporterHeaderDtoList) { if (exporterHeaderDto != null) { if (exporterHeaderDto.ExporterHeader != null) { var exporterHeaderAttribute = exporterHeaderDto.ExporterHeader; if (exporterHeaderAttribute != null && !exporterHeaderAttribute.IsIgnore) { var name = exporterHeaderAttribute.DisplayName.IsNullOrWhiteSpace() ? exporterHeaderDto.PropertyName : exporterHeaderAttribute.DisplayName; sheet.Cells[1, exporterHeaderDto.Index].Value = ColumnHeaderStringFunc(name); sheet.Cells[1, exporterHeaderDto.Index].Style.Font.Bold = exporterHeaderAttribute.IsBold; var size = exporter?.HeaderFontSize ?? exporterHeaderAttribute.FontSize; if (size.HasValue) { sheet.Cells[1, exporterHeaderDto.Index].Style.Font.Size = size.Value; } } } else { sheet.Cells[1, exporterHeaderDto.Index].Value = ColumnHeaderStringFunc(exporterHeaderDto.PropertyName); } } } }
/// <summary> /// 添加导出数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sheet"></param> /// <param name="startRowIndex"></param> /// <param name="items"></param> protected void AddDataItems <T>(ExcelWorksheet sheet, List <ExporterHeaderInfo> exporterHeaders, IList <T> items, ExcelExporterAttribute exporter) { if (items == null || items.Count == 0) { return; } var tbStyle = TableStyles.Medium10; if (exporter != null && !exporter.TableStyle.IsNullOrWhiteSpace()) { tbStyle = (TableStyles)Enum.Parse(typeof(TableStyles), exporter.TableStyle); } sheet.Cells["A2"].LoadFromCollection(items, false, tbStyle); }