asp.net导出Excel
来源:互联网 发布:设计效果图用什么软件 编辑:程序博客网 时间:2024/05/17 04:06
protected void CreateExcel(DataSet ds, string typeid, string FileName) { HttpResponse resp; resp = Page.Response; resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName); resp.Charset = "UTF-8";
string colHeaders = "", ls_item = ""; int i = 0;
//定义表对象与行对像,同时用DataSet对其值进行初始化 DataTable dt = ds.Tables[0]; DataRow[] myRow = dt.Select(""); // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件 if (typeid == "1") {
resp.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 for (i = 0; i < dt.Columns.Count - 1; i++) colHeaders += dt.Columns[i].Caption.ToString() + "\t"; colHeaders += dt.Columns[i].Caption.ToString() + "\n"; //向HTTP输出流中写入取得的数据信息 resp.Write(colHeaders); //逐行处理数据 foreach (DataRow row in myRow) { //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n for (i = 0; i < dt.Columns.Count - 1; i++) ls_item += row[i].ToString().Replace("\t", "") + "\t"; ls_item += row[i].ToString().Replace("\t", "") + "\n"; //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据 resp.Write(ls_item); ls_item = ""; } } else { if (typeid == "2") { //从DataSet中直接导出XML数据并且写到HTTP输出流中 resp.Write(ds.GetXml()); } } //写缓冲区中的数据到HTTP头文件中 resp.End(); }
//绑定事件
protected void ExcelBind() { string sel = "select OrderName as '姓名',OrderTel as '电话' ,convert(nvarchar(10),OrderDate,120 ) as '日期' ,OrderType_id as '产品' ,OrderMark as '备注' from BuyOrder "; conn.Open(); cmd = new SqlCommand(sel, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); conn.Close();
CreateExcel(ds, "1", "Statement.xls");//引用上面的方法进行另存为 }
- asp.net导出excel
- asp.net导出Excel
- asp.net导出excel
- asp.net导出Excel
- Asp.net 导出Excel
- asp.net导出excel
- asp.net 导出excel
- asp.net 导出Excel
- asp.net 导出 excel
- asp.net导出Excel
- ASP.NET Excel导出
- asp.net导出EXCEL
- ASP.NET Excel导出
- asp.net 导出Excel
- asp.net 导出Excel
- Asp.net导出Excel
- asp.net导出excel
- Asp.net 导出Excel
- let finger cross
- 跨平台开发慎用中文注释
- 物联网是未来发展重点
- 环球科学:大脑暗能量(图)
- 实训第4天的小备忘
- asp.net导出Excel
- Android源码下载——用git clone实现单个目录下载
- 配置实现-适易快速开发架构实现功能总结
- H-JTAG与计算机并口的连接
- 快速获得强大外链的10大方法
- <Right here waiting> 想起了很多
- web开发的一些资料
- 责任
- V4L2 编程