C# ASP.net Gridview导出到Excel.xlsx不工作
更改内容类型后,导出到.xls中的excel正在工作,但导出到.xlsx不工作- 我的代码如下:C# ASP.net Gridview导出到Excel.xlsx不工作,c#,excel,gridview,C#,Excel,Gridview,更改内容类型后,导出到.xls中的excel正在工作,但导出到.xlsx不工作- 我的代码如下: private void ExportToExcel() { try { Response.Clear(); Response.Buffer = true; //Response.AddHeader("content-disposition", "attachment;filename=LoanDataDeletion.xls");
private void ExportToExcel()
{
try
{
Response.Clear();
Response.Buffer = true;
//Response.AddHeader("content-disposition", "attachment;filename=LoanDataDeletion.xls");
//Response.Charset = "";
// Response.ContentType = "application/vnd.ms-excel";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Charset = "";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", "LoanDataDeletion.xlsx"));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdView.AllowPaging = false;
grdView.DataBind();
//Change the Header Row back to white color
grdView.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
for (int i = 0; i < grdView.Columns.Count; i++)
{
grdView.HeaderRow.Cells[i].Style.Add("background-color", "green");
}
for (int i = 0; i < grdView.Rows.Count; i++)
{
GridViewRow row = grdView.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
}
}
grdView.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch (Exception)
{
throw;
}
}
private void ExportToExcel()
{
尝试
{
Response.Clear();
Response.Buffer=true;
//AddHeader(“内容处置”、“附件;文件名=loanadadeletion.xls”);
//响应。Charset=“”;
//Response.ContentType=“application/vnd.ms excel”;
Response.ContentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”;
响应。Charset=“”;
AddHeader(“Content Disposition”,string.Format(“attachment;filename={0}”,“loanadadeletion.xlsx”);
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
grdView.AllowPaging=false;
grdView.DataBind();
//将标题行更改回白色
grdView.HeaderRow.Style.Add(“背景色”和“FFFFFF”);
//将样式应用于单个单元格
对于(int i=0;i
string path=System.IO.path.GetTempPath()+System.DateTime.Now.Ticks+“.xls”;
GridView grdTemp=新的GridView();
grdTemp.DataSource=dt;
grdTemp.Caption=“Products
”+DateTime.Now;
grdTemp.DataBind();
使用(StreamWriter sw=新StreamWriter(路径))
{
使用(HtmlTextWriter hw=新的HtmlTextWriter(sw))
{
grdTemp.RenderControl(硬件);
}
}
字符串save_path=path;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,System.DateTime.Now.Ticks+”.xls”);
Response.ContentType=“应用程序/excel”;
Response.WriteFile(保存路径);
Response.End();
System.IO.File.Delete(保存路径);
string path=System.IO.path.GetTempPath()+System.DateTime.Now.Ticks+“.xls”;
GridView grdTemp=新的GridView();
grdTemp.DataSource=dt;
grdTemp.Caption=“Products
”+DateTime.Now;
grdTemp.DataBind();
使用(StreamWriter sw=新StreamWriter(路径))
{
使用(HtmlTextWriter hw=新的HtmlTextWriter(sw))
{
grdTemp.RenderControl(硬件);
}
}
字符串save_path=path;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,System.DateTime.Now.Ticks+”.xls”);
Response.ContentType=“应用程序/excel”;
Response.WriteFile(保存路径);
Response.End();
System.IO.File.Delete(保存路径);
我想您得到的错误是“您试图打开的文件的格式与文件扩展名指定的格式不同”。这是因为在传统的导出到Excel方法中,GridView首先转换为HTML字符串,然后将该HTML字符串导出到Excel,但Excel 2007/2010无法识别纯HTML格式。有一种方法可以在不使用HtmlTextWriter的情况下使用。它允许您在服务器上创建Excel电子表格。查看以下帖子:
或者这个:我想您得到的错误是“您试图打开的文件的格式与文件扩展名指定的格式不同”。这是因为在传统的导出到Excel方法中,GridView首先转换为HTML字符串,然后将该HTML字符串导出到Excel,但Excel 2007/2010无法识别纯HTML格式。有一种方法可以在不使用HtmlTextWriter的情况下使用。它允许您在服务器上创建Excel电子表格。查看以下帖子:
或者这一条:您必须提供更多关于“不工作”的含义的信息。您是否尝试使用.xlsx保留以前的add header语句?它正在.xls中导出,但没有在.xlsx中导出。您必须提供更多关于“不工作”的信息意思是。您是否尝试使用.xlsx保留以前的添加头语句?它正在.xls中导出,但未在.xlsx中导出。
string path = System.IO.Path.GetTempPath() + System.DateTime.Now.Ticks + ".xls";
GridView grdTemp = new GridView();
grdTemp.DataSource = dt;
grdTemp.Caption = "Products<br> " + DateTime.Now;
grdTemp.DataBind();
using (StreamWriter sw = new StreamWriter(path))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
grdTemp.RenderControl(hw);
}
}
string save_path = path;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", System.DateTime.Now.Ticks + ".xls"));
Response.ContentType = "application/excel";
Response.WriteFile(save_path);
Response.End();
System.IO.File.Delete(save_path);