ASP.NET将数据导入到Excel
2013-02-28 15:22
323 查看
1.直接将当前页面Repeater里的所有数据导出到Excel (在BtnExcel单击事件里写)
一个导出Excel的方法 进行调用!
注意:
如果
类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。
有报这个错误,那么请检查一下form标签里有没有 runat="server"
如果有还是报错 可以加上下面的代码 就可以了
//定义文档类型、字符编码 string title = "**统计表" + "_" + DateTime.Today.ToShortDateString(); Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8) + ".xls"; Response.AppendHeader("Content-Disposition", filename); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档 Response.ContentType = "application/ms-excel"; Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); this.EnableViewState = false; // 定义一个输入流 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.lstPurchaseOrder.RenderControl(oHtmlTextWriter); //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件 Response.Write(oStringWriter.ToString()); Response.End();
一个导出Excel的方法 进行调用!
#region 导出Excel方法 /// <summary> /// 导出Excel方法 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); gdvExcelOrder.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } #endregion
#region 导出Excel /// <summary> /// 导出Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExcel_Click(object sender, EventArgs e) { string year = (DateTime.Now.Year - 2000).ToString("D2"); string month = DateTime.Now.Month.ToString("D2"); string day = DateTime.Now.Day.ToString("D2"); string fileName = year + "-" + month + day; Export("application/ms-excel", fileName+".xls"); } #endregion
注意:
如果
类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。
有报这个错误,那么请检查一下form标签里有没有 runat="server"
如果有还是报错 可以加上下面的代码 就可以了
#region 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件 /// <summary> /// 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件 /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { } #endregion
相关文章推荐
- asp.net实现将Excel中多个sheet数据导入到SQLSERVER中的方法
- ASP.NET中数据库数据导入Excel并打印
- asp.net采用OLEDB方式导入Excel数据时提示:未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 提供程序"
- asp.net mvc 数据导出excel表及自excel表导入数据到相应表
- asp.net系统导入Excel数据时提示:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序"
- ASP.NET中数据库数据导入Excel并打印
- Asp.net中Excel数据导入到SQL Server
- ASP.NET Excel数据导入数据库
- asp.net_Excel数据导入数据库
- ASP.NET Excel数据导入数据库
- ASP.NET Excel用户数据批量导入(三层)
- asp.net 将EXCEL表格数据导入到SQL Server数据库
- Asp.NET MVC 导入Excel数据教程 手把手教你系列!!!
- asp.net如何实现excel导入到sql或者根据需要再将sql中的数据导出到excel表格中
- ASP.NET中数据库数据导入Excel并打印
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
- ASP.NET中数据库数据导入Excel并打印(转载)
- Asp.Net将Excel数据导入Sql Server数据库的的例子
- 转:Asp.Net将Excel数据导入Sql Server数据库
- ASP.NET 导入excel 数据