我有一個C#列表,我從Excel電子表格創建,我想將其導出到Excel。我怎樣才能完成這項任務?這只是一個控制檯項目。我不打算在.Net應用程序中顯示數據。我只需要電子表格。如何將C#對象列表導出到Excel電子表格中?
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();
你不能導出到您在預先計算的excel文件導入一個簡單的csv文件或XML文件? –
你試過這個傢伙嗎?轉到標題爲「以下是列入方法列表。列表