asp.net导出数据生成真正的excel文件
答案:2 悬赏:60 手机版
解决时间 2021-03-19 11:14
- 提问者网友:眉目添风霜
- 2021-03-19 01:20
asp.net导出数据生成excel文件,如果修改了生成的excel文件它会生成其它几个文件,例如怎样防止啊
最佳答案
- 五星知识达人网友:枭雄戏美人
- 2021-03-19 01:59
我一般都是用gridview导出数据,你看看代码吧 对你有没有帮助, ----------------------按钮事件-------------------protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "sssss.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 = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
// turn off paging
GridView1.AllowPaging = false;
this.GridView1.DataBind(); GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); // turn the paging on again
GridView1.AllowPaging = true;
this.GridView1.DataBind(); } 下面这句不加的话回出错误 public override void VerifyRenderingInServerForm(Control control)
{ }页面:EnableEventValidation = "false"
{
Export("application/ms-excel", "sssss.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 = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
// turn off paging
GridView1.AllowPaging = false;
this.GridView1.DataBind(); GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); // turn the paging on again
GridView1.AllowPaging = true;
this.GridView1.DataBind(); } 下面这句不加的话回出错误 public override void VerifyRenderingInServerForm(Control control)
{ }页面:EnableEventValidation = "false"
全部回答
- 1楼网友:逐風
- 2021-03-19 03:29
由dataset生成
public 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);
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")
{
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for(i=0;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 ls_item +=row[i].tostring() + "\t";
ls_item += row[i].tostring() +"\n";
//当前行数据写入http输出流,并且置空ls_item以便下行数据
resp.write(ls_item);
ls_item="";
}
}
else
{
if(typeid=="2")
{
//从dataset中直接导出xml数据并且写到http输出流中
resp.write(ds.getxml());
}
}
//写缓冲区中的数据到http头文件中
resp.end();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯