0
下面的代碼現在可以使用了,但是我想在打開Excel後沒有任何損壞消息的XLSX格式。以(XLSX)格式將數據導出到Excel數據
protected void BTNExportExcel_Click(object sender, EventArgs e)
{
DataTable dtexp= ExportTrends(); //const
try
{
Response.Clear();
Response.ClearContent();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Export.xls");
TextWriter tw = new StringWriter();
HtmlTextWriter h = new HtmlTextWriter(tw);
string[] images = TXTImages.Text.Split(new string[]{"<img"},StringSplitOptions.None);
if (images.Length > 0)
{
Response.Write(images[0].ToString());
for (int i = 1; i < images.Length; i++)
{
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
string secondpart = images[i].Substring(images[i].IndexOf(',')+1);
img.ImageUrl = LoadImage(images[i].Substring(images[i].IndexOf(',') + 1, secondpart.LastIndexOf('\'')));
img.RenderControl(new HtmlTextWriter(Response.Output));
Response.Write("<BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/>");
}
}
Response.Write("<BR/>");
////// Create a dynamic control, populate and render it
GridView excel = new GridView();
excel.DataSource = dtexp;
excel.DataBind();
excel.RenderControl(new HtmlTextWriter(Response.Output));
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(string.Empty);
Response.Flush();
Response.End();
}
}
然後,您需要找到一個能夠生成真正的XLSX文件的庫。你有沒有試過谷歌搜索「C#XLSX庫」? – mason
是的,我嘗試過,但他們使用外部庫。我不想將外部庫添加到我的項目中。 – VMS
那對你來說會很艱難。你看,XLSX並不是真正的簡單格式,只需在你自己的代碼中生成即可。這就是圖書館存在的原因。所以你不必重新發明輪子。你有什麼反對使用外部庫? – mason