protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (this.GridView1.Rows.Count > 0)
{
ToExcel(GridView1, "aaa.xls");
}
else
{
MessageBox.Show(this, "暂无需要导出的数据!");
}
}
public override void VerifyRenderingInServerForm(Control control)
{ } private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}这是我从GridView中导出数据到Excel表格中,
然后我又在另一个页面把这个Excel表格导入系统,则报错:外部表不是预期的格式。。请各位看清楚我的问题在发言,谢谢!
解决方案 »
- 用JQuery tablesorter排序的问题
- detailsview使用方法
- asp.net为什么后退时排版会乱
- 向Access里插入值的问题
- ACCESS数据库。如何用like'%2005-3%'搜索到2005年3月所有的记录? 库中记录格式是“2005-4-6 17:30:07”
- asp.net(c#)静态调用c写的dll更新的问题?
- btnsearch 为什么?
- 现在软件公司的软件做的...................
- 高分求 ASP.NET 电子商务入门经典(VB.NET 编程篇) 源代码。
- 为啥我添加的数据时重复的?
- 模态弹出窗口之自定义控件问题
- 关于DateTime 格式精度问题..
{
Export("application/ms-excel","学生成绩报表.xls"); } //GridView控件与Excel的转换
private void Export(string FileType,string FileName)
{
Response.Charset = "GB2312";//输出是采用的gb2312(简体中文)。
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(FileName,System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
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);
}
}
{
/// <summary>
/// 导出Exel
/// </summary>
/// <param name="ctl">导出数据的控件载体</param>
/// <param name="FileName">导出Exel的默认名字</param>
public static void ToExcel(System.Web.UI.Control ctl, string FileName)
{ HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
} public static void ToExcel1(DataTable Dt, string FileName)
{
System.Web.UI.WebControls.GridView dgExport = null;
//当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (Dt != null)
{
//设置编码和附件格式
//System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application nd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "GB2312"; //导出Excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.DataSource = Dt.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind(); //下载到客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}