C#的ReportViewer出口(c# ReportViewer Export)

2019-08-17 01:09发布

ReportView我要导出到以下格式:.DOCX,.PDF,或.xlsx

导出到.PDF:

reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[3]);

导出为.docx:

reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[5]);

出口的.xlsx:

reportViewer.ExportDialog(_reportViewer.LocalReport.ListRenderingExtensions()[1]);

但起初我必须选择一个格式保存。 我想在第一次打开SaveFileDialog,并在其中选择保存的格式

我该怎么做 ?

Answer 1:

这是我想出的代码:

string _sSuggestedName = String.Empty;

byte[] byteViewerPDF = _reportViewer.LocalReport.Render("PDF");  
byte[] byteViewerExcel = _reportViewer.LocalReport.Render("Excel");  
byte[] byteViewerWord = _reportViewer.LocalReport.Render("Word");

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "PDF files (*.pdf)|*.pdf| Doc files
(*.doc)|*.doc| Excel files (*.xls)|*.xls";

if (saveFileDialog1.ShowDialog() == DialogResult.Ok)
{

  FileStream newFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);

  if (saveFileDialog1.FilterIndex == 1)
  {
    newFile.Write(byteViewerPDF, 0, byteViewerPDF.Length);
    newFile.Close();
  }
  else
    if (saveFileDialog1.FilterIndex == 2)
    {
      newFile.Write(byteViewerWord, 0, byteViewerWord.Length);
      newFile.Close();
    }
  else
    if (saveFileDialog1.FilterIndex == 3)
    {
      newFile.Write(byteViewerExcel, 0, byteViewerExcel.Length);
      newFile.Close();
    }

}


文章来源: c# ReportViewer Export