我有一个从Excel电子表格创建的C#列表,我想将其导出到Excel。我怎样才能完成这项任务?这只是一个控制台项目。我不打算在.Net应用程序中显示数据。我只需要电子表格。
var fileName = string.Format("C:\\Users\\SGurmu\\Desktop\\Data 091510.xls");
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var fileName2 = string.Format("C:\\Users\\SGurmu\\Desktop\\Copy of Prototype.xls");
var connectionString2 = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "contacts");
var data = ds.Tables["contacts"].AsEnumerable();
List<EmployeeData> query = data.Where(x => x.Field<string>("First_Name") != string.Empty).Select(x =>
new EmployeeData
{
empID = x.Field<double>("EMPLOYEE"),
firstName = x.Field<string>("First_Name"),
lastName = x.Field<string>("Last_Name"),
JobCategory = x.Field<string>("Job Title"),
StartDate = x.Field<Nullable<DateTime>>("Hire Dt"),
EndDate = x.Field<Nullable<DateTime>>("Term Dt"),
TermReason = x.Field<string>("Term Reason"),
PeggedUID = x.Field<Nullable<double>>("Pegged UserID"),
UpdateDate = x.Field<Nullable<DateTime>>("Last Updated")
}).ToList();
答案 0 :(得分:0)
也许您可以尝试使用Nuget中托管的Infodinamica.Framework.Expotable包。
有了它,你可以做这样的事情
List<EmployeeData> query = data.Where(x => x.Field<string>("First_Name") != string.Empty).Select(x =>
new EmployeeData
{
empID = x.Field<double>("EMPLOYEE"),
firstName = x.Field<string>("First_Name"),
lastName = x.Field<string>("Last_Name"),
JobCategory = x.Field<string>("Job Title"),
StartDate = x.Field<Nullable<DateTime>>("Hire Dt"),
EndDate = x.Field<Nullable<DateTime>>("Term Dt"),
TermReason = x.Field<string>("Term Reason"),
PeggedUID = x.Field<Nullable<double>>("Pegged UserID"),
UpdateDate = x.Field<Nullable<DateTime>>("Last Updated")
}).ToList();
IExportEngine engine = new ExcelExportEngine();
engine.AddData(EmployeeData);
MemoryStream memory = engine.Export();
您可以使用nuget命令安装:
Install-Package Infodinamica.Framework.Exportable
唯一的问题是,文档是西班牙语。
项目页面为here
导出示例(西班牙语)为here
它还启用导入文件(西班牙语)here