首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在asp.net中将数据导出到excel

在asp.net中将数据导出到excel
EN

Stack Overflow用户
提问于 2012-10-12 13:11:03
回答 1查看 2.4K关注 0票数 0

我需要将表中占位符的内容导出到excel中。当我只需将占位符放在div中时,就可以将数据(表)导出到excel。但是,当我将其放入表的td中时,我只能导出标题,而不能导出占位符的内容。

代码语言:javascript
运行
复制
<asp:Panel ID="pnlMilestone" runat="server" CssClass="milestone-wrapper">
    <div id="TableLists" runat="server" class="milestone-chart-wrapper">         
    <table>
        <tr>
            <td style="text-align: center;">
                <asp:Label ID="lblChartTitle" runat="server" Visible="False" Font-Bold="True"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <div runat="server" id="bartable_render">
                    <div id="bar_chart" style="height: 315px; width: 800px; margin-left: 150px;">
                    </div>
                    <div id="bar" style="height: 345px; width: 800px; margin-left: 10px;">
                    </div>
                </div>
        <asp:PlaceHolder ID="phDynamicTabular" runat="server"></asp:PlaceHolder>
            </td>
        </tr>
    </table>
     </div>
</asp:Panel>

导出代码:

代码语言:javascript
运行
复制
 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;filename=ProjectTarget_" + CampaignID + ".xls");
 Response.Charset = "";
 Response.ContentType = "application/vnd.xls";
 StringWriter stringWrite = new StringWriter();
 HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

 TableLists.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
EN

Stack Overflow用户

回答已采纳

发布于 2012-10-12 13:48:05

试试这段代码,

代码语言:javascript
运行
复制
    var htmlContent = GetHtmlContent();
    var excelName="ProjectTarget_" + CampaignID;
    response.Clear();
    response.ClearHeaders();
    response.ClearContent();
    response.ContentEncoding = Encoding.GetEncoding("windows-1254");
    response.Charset = "windows-1254"; //ISO-8859-13 ISO-8859-9  windows-1254
    response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", excelName));
    response.ContentType = "application/ms-excel";
    //response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    const string header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1254\" />\n<style>\n</style>\n</head>\n<body>\n";
    response.Write(header + htmlContent);
    response.End();

另请查看此页面:How to save created excel file to Client pc in ASP.NET

希望能有所帮助。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12852619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档