ListView不能正确地将数据导出到Excel

我在Excel中得到一个错误,涉及到我想从列表视图(ASP&C#)导出的数据。

为了澄清,我正在转换ListView到一个DataTable,因为我得到错误之前。

所有这些都是通过单击button来处理的,Excel文档被创build,但是在打开时出现错误,只显示标题。

任何想法,我在做什么错了? 我怀疑在LoadFromDataTable中添加dt会导致这种情况,但是在debugging时没有任何错误 – 任何指针都会被感激地接收到。

Excel错误: We found a problem with some content in 'Test_List.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes. We found a problem with some content in 'Test_List.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.

C#代码后面

 protected void csv_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Ref", typeof(string))); dt.Columns.Add(new DataColumn("Company", typeof(string))); dt.Columns.Add(new DataColumn("Email", typeof(string))); dt.Columns.Add(new DataColumn("Telephone", typeof(string))); foreach(ListViewDataItem li in ListView1.Items) { if (li.ItemType == ListViewItemType.DataItem) { DataRow dr = dt.NewRow(); dr["Ref"] = ((Label)li.FindControl("lblRef")).Text; dr["Company"] = ((Label)li.FindControl("lblCmp")).Text; dr["Email"] = ((Label)li.FindControl("lblEmail")).Text; dr["Telephone"] = ((Label)li.FindControl("lblTele")).Text; dt.Rows.Add(dr); } } using (ExcelPackage pck = new ExcelPackage()) { // creating worksheet ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Test_List"); // load database to sheet, start from A1, print column names on row 1 ws.Cells["A1"].LoadFromDataTable(dt, true); //loop through rows in datatable to rows in excel Response.ContentType = "application/vnd.openxmlformats-officedocument,spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=Test_List.xlsx"); Response.BinaryWrite(pck.GetAsByteArray()); } } 

在你的例子中,我将要解决的问题是,listview不能正确地转换成数据表,这可能是也可能不是为什么你的spreadsheeet抛出无用的错误。

如果你在dr["ref"]上进行深入的debugging和挖掘,我不认为string包含标签文本。

切掉listview到数据表位,并从SQL数据库到DataTable到CSV。

像这样,虽然你可以使用xlsx,如果你愿意的话:

 protected void Csv_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalConnection"].ConnectionString)) { con.Open(); //stored procs are easier to modify than query strings using (SqlCommand cmd = new SqlCommand("csvProc", con)) { cmd.CommandType = CommandType.StoredProcedure; using (SqlDataAdapter sda = new SqlDataAdapter()) { sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); // build csv as comma sep string string csv = string.Empty; foreach(DataColumn col in dt.Columns) { // header row for csv csv += col.ColumnName + ','; } // new line csv += "\r\n"; foreach(DataRow row in dt.Rows) { foreach(DataColumn col in dt.Columns) { // add the data rows csv += row[col.ColumnName].ToString().Replace(",", ";") + ','; } csv += "\r\n"; } // download the csv file Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=TestList.csv"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(csv); Response.Flush(); Response.End(); } } } } }