asp.net怎样导出excel2007
时间:2022-12-26 14:16:44
asp.net如何导出到excel2007?对于新手来说还是有一定难度,怎么办?下面让小编为你带来asp.net导出excel2007的方法。
asp.net导出excel2007的方法步骤
在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上;一种是将文件直接将文件输出流写给浏览器。在Response输出时,t分隔的数据,导出execl时,等价于分列,n等价于换行。
1、将整个html全部输出execl
此法将html中所有的内容,如按钮,表格,图片等全部输出到Execl中。
Response.Clear();
Response.Buffer= true;
Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出)图片、word文档等。下面的方法,也均用了这个属性。
2、将DataGrid控件中的数据导出Execl
上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。
System.Web.UI.Control ctl=this.DataGrid1;
//DataGrid1是你在窗体中拖放的控件
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
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();
如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。
为方便使用,写成方法如下:
public void DGToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
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();
}
用法:DGToExcel(datagrid1);
3、将DataSet中的数据导出Execl
有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如execl2006.xls
public void CreateExcel(DataSet ds,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
string colHeaders= "", ls_item="";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i=0;
int cl=dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加n
{
colHeaders +=dt.Columns[i].Caption.ToString() +"n";
}
else
{
colHeaders+=dt.Columns[i].Caption.ToString()+"t";
}
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach(DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加n
{
ls_item +=row[i].ToString()+"n";
}
else
{
ls_item+=row[i].ToString()+"t";
}
}
resp.Write(ls_item);
ls_item="";
}
resp.End();
}
猜你感兴趣:
1.asp.net如何导出excel2007
2.excel2003打开excel2007怎么办
3.怎么将ASP.NET导出Excel表格
4.如何用C#导出Excel表格
5.将asp图片导出Excel的方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel2007基础教程:选择特殊类型的单元格
Excel怎么使用And函数检查数据是否满足条件?
![](https://img.aspxhome.com/file/2023/4/41134_0s.jpg)
Word文档增强插件,不坑盒子软件体验
![](https://img.aspxhome.com/file/2023/5/16965_0s.jpg)
excel2010中定位的快捷键是哪个?
word个人简历表格制作
打印Word文档时页边框线不显示
![](https://img.aspxhome.com/file/2023/3/19453_0s.jpg)
怎样用Excel中的RAND函数产生随机数
如何在Word中插入具体的时间与日期
怎么在WPS里找出相同颜色的字
解读Excel2013页边距的设置
![](https://img.aspxhome.com/file/2023/6/38436_0s.jpg)
如何打开EXCEL表格中输入法自动切换功能?
![](https://img.aspxhome.com/file/2023/4/37824_0s.jpg)
Word中度量单位怎么设置英镑为单位?
![](https://img.aspxhome.com/file/2023/5/24755_0s.jpg)
所有页面中均显示Word文档中的表格标题的方法
Win10电脑怎么打开regedit注册表编辑窗口?
![](https://img.aspxhome.com/file/2023/9/50759_0s.png)
excel表格怎样设置手动换行
如何计算WORD表格中的数据?
![](https://img.aspxhome.com/file/2023/6/19486_0s.png)
Word中wps改变纸张方向的操作方法
Win10系统浏览器EDG浏览器打开是其他网页怎么设置?
![](https://img.aspxhome.com/file/2023/3/49113_0s.jpg)
Word 2007技巧:设置段落底纹
![](https://img.aspxhome.com/file/2023/6/20506_0s.jpg)