如题,下面是我的主要代码:
protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "工程师信息表.xls");
}
private void Export(string FileType,string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileName;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
但是导出的Excel确出现了乱码,打开Excel时出现错误提示:格式与文件扩展名制定的格式不一致,文件已损坏!
这是怎么回事呢!各位大侠帮帮忙!
protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "工程师信息表.xls");
}
private void Export(string FileType,string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileName;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
但是导出的Excel确出现了乱码,打开Excel时出现错误提示:格式与文件扩展名制定的格式不一致,文件已损坏!
这是怎么回事呢!各位大侠帮帮忙!
这句
修改 Response.ContentEncoding = System.Text.Encoding.UTF8;
我刚做过 是因为你那种方法是导出office2003里面 而出现错误是因为你的office版本是2007
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
Response.AddHeader("content-disposition", "attachment;filename=instimestock.xls");
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//写到Excel的数据不用分页
GridViewName.RenderControl(htmlWrite);//GridView Name
Response.Write(stringWrite.ToString());//向客户端写数据
Response.End();
}
//这个方法不写的话可能报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
给你个测试通过的代码。
这里是个导出excel 的源码 我用过的~ 你看看 对你时不时有帮助
不行的,我这儿还是出现那个错误,我装的是Office2007,难道是这个原因吗?
楼主这个没加......
google上一搜一大堆
Response.ContentEncoding = System.Text.GETEncoding.("gb2312");
{
this.GridView1.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel
Export(GridView1, "application/ms-excel", "项目报表.xls");
//Export(GridView5, } public override void VerifyRenderingInServerForm(Control control)
{
//这里什么也不用写
}
public void Export(GridView gv, string FileType, string FileName)
{ string style = @"<style>.text{mso-number-format:@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 . PrepareGridViewForExport(gv);//将模版列显示出来 Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; //this.GridView5.AllowPaging = false;
gv.AllowPaging = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); //this.GridView5.RenderControl(htw);
gv.RenderControl(htw);
Response.Write(style); Response.Write(sw.ToString().Replace("border='0'", "border='1'")); //Response.Write(dt.ToString()); Response.End(); } public void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
{ LinkButton lb = new LinkButton();
Label l = new Label();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text; gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(HtmlAnchor))
{
l.Text = (gv.Controls[i] as HtmlAnchor).InnerText;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(ImageButton))
{
l.Text = "图片";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
} } }
试试看,我昨天还用的