首页 > 开发 > 综合 > 正文

将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)

2024-07-21 02:25:30
字体:
来源:转载
供稿:网友
//把table控件中的数据保存到excel或word
public void save(system.web.ui.control source, documenttype type)

{
response.clear();
response.buffer= true;

//设置http的头信息,编码格式
if (type == documenttype.excel)
{
//excel
response.appendheader("content-disposition","attachment;filename=result.xls");
response.contenttype = "application/ms-excel";
}
else if (type == documenttype.word)
{
//word
response.appendheader("content-disposition","attachment;filename=result.doc");
response.contenttype = "application/ms-word";
}

//设置编码
response.charset="gb2312";
response.contentencoding=system.text.encoding.getencoding("gb2312");

//关闭控件的视图状态
source.enableviewstate =false;

//初始化htmlwriter
system.io.stringwriter writer = new system.io.stringwriter() ;
system.web.ui.htmltextwriter htmlwriter = new system.web.ui.htmltextwriter(writer);
source.rendercontrol(htmlwriter);

//输出
response.write(writer.tostring());

response.end();
}


//以下是保存图片
public void savepic()
{
string path = server.mappath(".") + @"/images/chart.jpeg";
filestream file = file.openread(path);
byte[] content = new byte[file.length];
file.read(content,0,content.length);
file.close();

response.clear();
response.appendheader("content-disposition","attachment;filename=chart.jpeg");
response.contenttype = "image/jpeg";//设置http的头信息
response.binarywrite(content);//输出

response.end();
}

不过图片保存完后,页面上的dropdownlist的select事件不能促发,不晓得是什么缘故,而页面上的button事件却可以激发事件,不知道大家有没有出现过这种问题?可以讨论一下,还是我保存图片的过程有问题?

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表