支持多sheet导入导出。导出字段过滤,合并行。特性配置导入验证,非空验证,唯一验证,错误标注等
用于基础配置和普通报表的导入导出,对于复杂需求,比如公式,导出图片等暂不支持
https://github.com/Mike-Zrw/ExcelHelper/tree/master
var students = new List<ExportStudent>();
var grades = new List<ExportGrade>();
var schools = new List<ExportSchool>();
for (int i = 0; i < 100; i++)
{
students.Add(new ExportStudent
{
Name = i % 6 == 1 ? null : ($"name{i}"),
Age = i,
Phone = i % 8 == 1 ? "adsf123" : $"{1}{new Random().Next(100, 999)}{1}{new Random().Next(100, 999)}{2}{new Random().Next(0, 9)}{3}",
Birthday = i % 13 == 1 ? default(DateTime?) : DateTime.Now.AddDays(i),
Money = Math.Round(new Random(i).NextDouble(), 2),
SchoolDate = DateTime.Now.AddDays(i + 1),
});
grades.Add(new ExportGrade { Code = $"编码{i}", GradeName = $"{i}年级" });
schools.Add(new ExportSchool { Name = $"{i}号学校", Address = $"学校地址{i}", Price = Math.Round(new Random().NextDouble(), 2) });
}
var exporter = new DefaultExcelExporter();
var stream = new FileStream("D://export.xlsx", FileMode.Create, FileAccess.Write);
//var stream = new MemoryStream();
exporter.Export(new ExportBook()
{
Ext =ExtEnum.XLSX,
Sheets = new List<ExportSheet> {
new ExportSheet(){ SheetName="测试", Data=students},
new ExportSheet(){ Data=grades},
new ExportSheet(){ Data=schools,Title=new ExportTitle("学校列表",true,18,default,Excel.Enums.HorizontalAlignEnum.Center), FilterColumn=new List<string>(){ "学校名称","price" } },
}
}, stream);
stream.Dispose();
public class ExportStudent : ExportModel
{
[ColumnNameAttribute("名字")]
public string Name { get; set; }
[ColumnNameAttribute("年龄")]
public int Age { get; set; }
[ColumnNameAttribute("生日")]
[StringFormatter("yyyy-MM-dd HH:mm:ss")]
public DateTime? Birthday { get; set; }
[ColumnStyle(FontName = "华文彩云")]
[ColumnNameAttribute("入学时间")]
[StringFormatter("yyyy-MM-dd")]
public DateTime SchoolDate { get; set; }
[ColumnStyle(FontColor = 211, IsBold = true)]
[ColumnNameAttribute("零花钱")]
public double Money { get; set; }
[ColumnNameAttribute("电话")]
public string Phone { get; set; }
}
public void ExportMergeRow()
{
var orders = new List<Order>();
for (int i = 0; i < 100; i++)
{
var index = new Random(i).Next(i + 10, i + 13);
var orderNumber = $"订单{index}";
orders.Add(new Order()
{
Buyer = $"下单人{index}",
Price = Math.Round(new Random(i).NextDouble(), 2),
BuyQty = new Random(i).Next(1, 10),
ProductName = $"商品{i}",
OrderNumber = orderNumber,
OrderNum2 = orderNumber,
ExportPrimaryKey = orderNumber
});
}
var exporter = new DefaultExcelExporter();
var stream = new FileStream("D://exportorder.xlsx", FileMode.Create, FileAccess.Write);
exporter.Export(new ExportBook()
{
Ext = ExtEnum.XLSX,
Sheets = new List<ExportSheet> { new ExportSheet() { SheetName = "订单列表", Data = orders } }
}, stream);
stream.Dispose();
}
var sheet1 = new ImportSheet<ImportStudent>
{
UniqueValidationPrompt = "零花钱不可重复",
HeaderRowIndex = 0,
SheetIndex = 0,
ValidateHandler = (list) =>
{
foreach (ImportStudent model in list)
{
if (model.IsValidated && model.Name == "name0")
model.SetError(nameof(model.Name), "名字不可为0");
if (model.IsValidated && model.Money < 0.5)
model.SetError(nameof(model.Money), "零花钱不可小于0.5");
}
}
};
var sheet2 = new ImportSheet<ImportGrade>
{
HeaderRowIndex = 0,
SheetIndex = 1
};
var sheet3 = new ImportSheet<ImportSchool>
{
HeaderRowIndex = 1,
SheetIndex = 2,
ValidateHandler = (list) =>
{
foreach (var model in list)
{
if (model.Price > 0.5)
model.SetError(nameof(model.Price), "学费不可大于0.5");
}
}
};
var import = new DefaultExcelImporter();
using var inputStrem = new FileStream("D://export.xlsx", FileMode.OpenOrCreate, FileAccess.Read);
using var outStrem = new FileStream("D://error.xlsx", FileMode.Create, FileAccess.Write);
var bookmodel = new ImportBook();
bookmodel.SetSheetModels(sheet1, sheet2, sheet3);
var ret = import.ImportExcel(inputStrem, ExtEnum.XLSX, bookmodel, outStrem);
var success = ret.ImportSuccess;
var summaryErrorMsg = ret.GetSummaryErrorMessage();
var notDisplayMsg = ret.GetNotDisplayErrorMessage();
Output.WriteLine($"success:{success}");
Output.WriteLine("summaryErrorMsg------------");
Output.WriteLine(summaryErrorMsg);
Output.WriteLine("notDisplayMsg------------");
Output.WriteLine(notDisplayMsg);
public class ImportStudent : ImportModel
{
[ColumnRequired("名字必填")]
[ColumnNameAttribute("名字")]
public string Name { get; set; }
[ColumnNameAttribute("年龄")]
public int Age { get; set; }
[ColumnRequired]
[ColumnNameAttribute("生日")]
public DateTime Birthday { get; set; }
[ColumnNameAttribute("入学时间")]
public DateTime SchoolDate { get; set; }
[ColumnUnique]
[ColumnNameAttribute("零花钱")]
public double Money { get; set; }
[ColumnNameAttribute("电话")]
[ColumnRegex(@"^[1]+[1-9]+\d{9}$", "电话格式不对")]
public string Phone { get; set; }
}