询问者
如何在客户端将动态生成的aspx页面另存为到本地

问题
-
我在页面a.aspx通过windows.open打开页面b.aspx?more
b.aspx页面通过more参数在后台获取数据,并以table的格式展示在前台(其中前台有已经写好的内容)
客户端当前看到的是b.aspx页面,并希望将当前显示的页面内容以excel(.xls)形式存储到本地(页面中表格用table形成)
目前想到的方法有两种,一种是将当前页面存储到服务端,用户点击另存为的时候下载;或者将当前页面以stream的形式导出;
但是在实现的时候却碰到几个问题:
1、存储到服务端的方法
string excel = HttpRuntime.AppDomainAppPath;
WebClient client = new WebClient();
string local = Request.ServerVariables["HTTP_HOST"];
string url ="http://"+ local + Request.ServerVariables["URL"];//or string url = local + Request.ServerVariables["URL"];
client.DownloadFile(url, excel + "test.xls");找到的当前页面所采用的方法不理想,不知道有否刚好的方法
2、客户端用户可以自由选择文件存储地址:我找到的都是需要<object>才能打开一个窗体,如:<object classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id="ObjWB" width=0 height=0>
</object>
<a href="javascript:void(0);" onClick="document.all.ObjWB.ExecWB(4,1);">Save As </a>这样需要用户点击允许才能使用的方法,求更完美的解决方案
- 已编辑 麒霖 2012年2月20日 9:49 完善问题
全部回复
-
你好,
你可以参考下面的方法:
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//指定输出的文件名及类型
Response.AppendHeader("Content-Disposition", "attachment;filename=TestBook1.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);//也可以使用其他数据控件
this.Repeater1.RenderControl(oHtmlTextWriter);Response.Write(oStringWriter.ToString());
Response.End();或者参考OneCode里面的示例:
ASP.NET app exports ReportViewer data (CSASPNETReportViewerExport)
http://code.msdn.microsoft.com/CSASPNETReportViewerExport-bda5617dImport/export Excel worksheet in ASP.NET (CSASPNETExcelImportExport)
http://code.msdn.microsoft.com/CSASPNETExcelImportExport-71cf1101补充说明一下,官方是不支持服务端的Office自动化的。
http://support.microsoft.com/kb/257757
谢谢!
Leo Tang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- 已标记为答案 LeoTangModerator 2012年2月27日 8:28
- 取消答案标记 麒霖 2012年3月1日 5:58
-
最开始是考虑你提供的方案,但是在修改Response.Charset = "utf-8";时报错:服务器无法在发送 HTTP 标头之后设置内容类型
这个原因主要是因为用户需要的表比较繁琐,见下图,
最开始为了能够快速实现内容展示,我直接在html页面中建立的<table>标签去创建展示,然后我在<table>标签中添加后台字符串<%=str%> .cs:str="<tr><td>xx</td></td>",从数据库获取对应数据并显示在页面中,目前用户希望将这些显示在页面中的内容另存为excel文件保存到本地硬盘,如果不能直接将本页面所有内容进行提取,其实还有一个可行方案是在服务端生成一个excel文件并进行存储,然后用户点击另存为文件,只要将那个另存为文件windows.open就行了,但是同时又碰到两个问题:activeX安全性和文件另存为报错HRESULT:0x800A03EC代码如下:
Microsoft.Office.Interop.Excel.Application app=new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks wbs = app.Workbooks;
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);
Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
s.Name = "test";
s.Cells[1, 1] = "1";
s.Cells[1, 2] = "1";
string spath = Server.MapPath("./")+"a.xls";//如c:\\a.xls
//---------------HRESULT:0x800A03EC SaveAs
wb.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);在网上查了很多,试了很多种参数都不行,我在工作用机上进行的编写,win7,office 2010;或者,能否在给我提供一个对应的解决方案呢?
-
最开始是考虑你提供的方案,但是在修改Response.Charset = "utf-8";时报错:服务器无法在发送 HTTP 标头之后设置内容类型
这个原因主要是因为用户需要的表比较繁琐,见下图,
最开始为了能够快速实现内容展示,我直接在html页面中建立的<table>标签去创建展示,然后我在<table>标签中添加后台字符串<%=str%> .cs:str="<tr><td>xx</td></td>",从数据库获取对应数据并显示在页面中,目前用户希望将这些显示在页面中的内容另存为excel文件保存到本地硬盘,如果不能直接将本页面所有内容进行提取,其实还有一个可行方案是在服务端生成一个excel文件并进行存储,然后用户点击另存为文件,只要将那个另存为文件windows.open就行了,但是同时又碰到两个问题:activeX安全性和文件另存为报错HRESULT:0x800A03EC代码如下:
Microsoft.Office.Interop.Excel.Application app=new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks wbs = app.Workbooks;
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);
Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
s.Name = "test";
s.Cells[1, 1] = "1";
s.Cells[1, 2] = "1";
string spath = Server.MapPath("./")+"a.xls";//如c:\\a.xls
//---------------HRESULT:0x800A03EC SaveAs
wb.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);在网上查了很多,试了很多种参数都不行,我在工作用机上进行的编写,win7,office 2010;或者,能否在给我提供一个对应的解决方案呢?
你好,
你可以参考下面的方法:
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//指定输出的文件名及类型
Response.AppendHeader("Content-Disposition", "attachment;filename=TestBook1.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);//也可以使用其他数据控件
this.Repeater1.RenderControl(oHtmlTextWriter);Response.Write(oStringWriter.ToString());
Response.End();或者参考OneCode里面的示例:
ASP.NET app exports ReportViewer data (CSASPNETReportViewerExport)
http://code.msdn.microsoft.com/CSASPNETReportViewerExport-bda5617dImport/export Excel worksheet in ASP.NET (CSASPNETExcelImportExport)
http://code.msdn.microsoft.com/CSASPNETExcelImportExport-71cf1101补充说明一下,官方是不支持服务端的Office自动化的。
http://support.microsoft.com/kb/257757
谢谢!
Leo Tang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
你好,
你是否直接将页面输出?复杂结构的表是可以放到Repeater控件中的。
Leo Tang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.