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

在asp.net中将数据导出到excel,asp.net,Asp.net,我需要将表格中占位符的内容导出到excel。当我简单地将占位符放在div中时,我可以将数据(表)导出到excel。但当我把它放在表的td中时,我只能导出标题,而不能导出PlaceHolder的内容 <asp:Panel ID="pnlMilestone" runat="server" CssClass="milestone-wrapper"> <div id="TableLists" runat="server" class="milestone-chart-wrapp

我需要将表格中占位符的内容导出到excel。当我简单地将占位符放在div中时,我可以将数据(表)导出到excel。但当我把它放在表的td中时,我只能导出标题,而不能导出PlaceHolder的内容

<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>
试试这个代码

    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();
var htmlContent=GetHtmlContent();
var excelName=“ProjectTarget”+活动ID;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentEncoding=Encoding.GetEncoding(“windows-1254”);
response.Charset=“windows-1254”//ISO-8859-13 ISO-8859-9 windows-1254
AppendHeader(“内容处置”,String.Format(“附件;文件名={0}.xls”,excelName));
response.ContentType=“应用程序/ms excel”;
//response.ContentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”;
常量字符串头=“\n\n\n\n\n\n\n\n\n\n”;
响应.写入(标题+HTML内容);
response.End();
另请参阅本页:


希望对您有所帮助。

在您的代码中,如何从GetHtmlContent()获取内容;功能
    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();