CrystalReportViewer导出到特定的Excel工作表

时间:2011-05-19 18:13:53

标签: vb.net excel crystal-reports

CrystalReportViewer允许我将报表导出到Excel文件,但它始终将报表“Sheet 1”保存在新文件中。

有没有办法让CrystalReportViewer将报表保存到现有的Excel文件中,并指定将报表保存到报表的名称?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

禁用CrystalReportViewer导出按钮

    <CR:CrystalReportViewer  ....    HasExportButton="false" ... />

添加导出工具栏

  <asp:Panel ID="exportPanel" ClientIDMode="Static" runat="server" CssClass="inLinePanel" >
  <table style="height: 22px; margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: 1px; display: block; cursor: pointer; " id="CrystalReportViewer1_toptoolbar_addedButtons" class="" cellspacing="0" cellpadding="0" border="0"  >
   <tr>
    <td  title="PDF">
     <asp:ImageButton ID="btnPdf" runat="server" OnClick="btnExport_Click" ImageUrl="~/Images/PDF.png" CssClass="AddedButton" />
     </td>
    <td title="word">
     <asp:ImageButton ID="btnDoc" runat="server" OnClick="btnExport_Click" ImageUrl="~/Images/DOC.png"  />   
     </td>
    <td  title="excel">
     <asp:ImageButton ID="btnXls" runat="server" OnClick="btnExport_Click" ImageUrl="~/Images/XLS.png"  />   
     </td>
    <td  title="excel no stile">
     <asp:ImageButton ID="btnCsv" runat="server" OnClick="btnExport_Click" ImageUrl="~/Images/CSV.png" />   
     </td>
   </tr>
 </table>
 </asp:Panel> 

这是代码隐藏的

 protected void btnExport_Click(object sender, EventArgs e)
{
 ReportDocument reportDocument = CrystalReportViewer1.ReportSource ;
    // Stop buffering the response
    Response.Buffer = false;
    // Clear the response content and headers
    Response.ClearContent();
    Response.ClearHeaders();
    try
    {
        string senderID = ((ImageButton)sender).ID;
        if (senderID == "btnPdf")
            reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Page.Title);
        else if (senderID == "btnXls")
            reportDocument.ExportToHttpResponse(ExportFormatType.Excel, Response, true, Page.Title);
        else if (senderID == "btnCsv")
            reportDocument.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, true, Page.Title);
        else if (senderID == "btnDoc")
            reportDocument.ExportToHttpResponse(ExportFormatType.EditableRTF, Response, true, Page.Title);
        // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
    }
    catch (Exception ex)
    {
        Compliance.SmtpHelper.SendErrorMessage("Reportform.Aspx - QueryString: '" + clearQueryString + "' - Dettaglio errore: " + ex.ToString());
        Response.Write(ex.StackTrace);

    }
}