首页 > 代码库 > ReportViewer 不预览,直接导出 PDF文件
ReportViewer 不预览,直接导出 PDF文件
作为笔记记着,以免以后再到处找资料
1. 在不预览的情况下导出文件
先看一个方法说明,想知道ReportViewer支持导出哪些文件类型,在Render方法说明中就有描述
//
// Summary:
// Processes the report and renders it in the specified format using a stream
// provided by a callback function.
//
// Parameters:
// format:
// The format in which to render the report. This argument maps to a rendering
// extension. Supported formats include Excel, PDF, Word, and Image. To access
// the list of available rendering extensions, use the Microsoft.Reporting.WinForms.LocalReport.ListRenderingExtensions()
// method.
//
// deviceInfo:
// An XML string that contains the device-specific content that is required
// by the rendering extension specified in the format parameter. For more information
// about device information settings for specific output formats, see fe718939-7efe-4c7f-87cb-5f5b09caeff4
// Device Information Settings in SQL Server Books Online.
//
// createStream:
// A Microsoft.Reporting.WinForms.CreateStreamCallback delegate function that
// will be used to provide a System.IO.Stream object for rendering.
//
// warnings:
// [out] An array of Microsoft.Reporting.WinForms.Warning objects that describes
// any warnings that occurred during report processing and rendering.
public void Render(string format, string deviceInfo, CreateStreamCallback createStream, out Warning[] warnings);
调用方法Microsoft.Reporting.WinForms.LocalReport.ListRenderingExtensions()就知道了,如果要把支持导出的文件类型列表显示出来,这个方法就是必须的,但是我没试过。
下面的代码主要是参数msdn的例子:https://msdn.microsoft.com/zh-cn/library/ms252091(v=vs.110).aspx
测试例子时,没用他的数据源
抄下代码运行,发现没有像他说的那样导出文件,然后看了下源码,发现他只把文件写进MemoryStream对象,并没看到将其输出到文件的代码,于是补充了点代码,把内存里的
输出到文件中,然后定义了个ExportFileType枚举,又添加了一个ExportToFile方法,其他代码与示例相同
打印的代码我不关心,所以没理他
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Data;using Microsoft.Reporting.WinForms;using System.Drawing.Printing;using System.Drawing.Imaging;using System.Drawing;namespace PrintLocalReport{ public enum ExportFileType { PDF } public class ReportViewerPrinter:IDisposable { private int m_currentPageIndex = 0; private IList<Stream> m_streams;
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new MemoryStream(); m_streams.Add(stream); return stream; } public void ExportToFile(LocalReport report,string targetPath, ExportFileType fileType) { Export(report, targetPath, fileType); } private void Export(LocalReport report, string targetPath, ExportFileType fileType) { string deviceInfo = @"<DeviceInfo> <OutputFormat>EMF</OutputFormat> </DeviceInfo>";
//根据我的测试,这里的参数会覆盖rdlc中定义的参数,所以我尝试删除了,发现删除了行为是可预期的,是正确的
//如果需要,这里可以实现定制化了
//<PageWidth>8.5in</PageWidth> //<PageHeight>11in</PageHeight> //<MarginTop>0.25in</MarginTop> //<MarginLeft>0.25in</MarginLeft> //<MarginRight>0.25in</MarginRight> //<MarginBottom>0.25in</MarginBottom> Warning[] warnings; m_streams = new List<Stream>(); report.Render(fileType.ToString(), deviceInfo, CreateStream, out warnings); int bufferLength = 1024; byte[] buffer = new byte[bufferLength]; using (FileStream fs = new FileStream(targetPath, FileMode.CreateNew, FileAccess.Write)) { foreach (Stream stream in m_streams) { stream.Position = 0; MemoryStream ms = stream as MemoryStream; int readLength = 0; while (true) { readLength = ms.Read(buffer, 0, bufferLength); if (readLength == 0) break; fs.Write(buffer, 0, readLength); } } } } private void PrintPage(object sender, PrintPageEventArgs ev) { Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); Rectangle adjustedRect = new Rectangle( ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, ev.PageBounds.Width, ev.PageBounds.Height); ev.Graphics.FillRectangle(Brushes.Wheat, adjustedRect); ev.Graphics.DrawImage(pageImage, adjustedRect); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } private void Print() { if (m_streams == null || m_streams.Count == 0) { throw new Exception("Error:no stream to print"); } PrintDocument printDoc = new PrintDocument(); if (!printDoc.PrinterSettings.IsValid) { throw new Exception("Error: cannot find the default printer."); } else { printDoc.PrintPage += new PrintPageEventHandler(PrintPage); m_currentPageIndex = 0; printDoc.Print(); } } public void Dispose() { if (m_streams != null) { foreach (Stream stream in m_streams) { stream.Close(); } m_streams = null; } } }}
调用代码如下
static void Main(string[] args) { LocalReport report = new LocalReport(); report.ReportPath = @"..\..\report.rdlc";
//这个tablename要和rdlc指定的数据源名字一致,有多少个数据源挨着挨着添加 report.DataSources.Add(new ReportDataSource("tablename", LoadSalesData()));
//这里是定义的参数,也挨着挨着添加 IList<ReportParameter> list = new List<ReportParameter>() { new ReportParameter("paramName","ParamVale") }; report.SetParameters(list.ToArray()); try { new ReportViewerPrinter().ExportToFile(report, "test.pdf", ExportFileType.PDF); } catch (Exception ex) { } }
2. 后台导出的代码很简单,主要纠结的地方是在绘制报表的时候
主要是两个问题:
1. 导出到pdf文件后,本来是一页的内容,他纵向劈开,在pdf中分成两页显示了
2.把导出的pdf文件拿去打印,在打印的时候最后一页除了页眉页脚,中间区域是空白的,而pdf中没有这一页(真他妈奇怪)
这两个问题的原因都是报表的尺寸问题,不超宽,他就不会纵向劈开,不超高,他就不会有空白页,我设置如下解决了这两个问题
我的报表设置的Paper size是 A4 21cm 29.7cm ,Margins 都为0 (右键报表设计界面的黑色区域(也就是非报表绘制区域),选择Report Properties 属性)
鼠标左键点击报表绘制区域,打开Properties窗口,属性窗口中也有一个Size属性,这个是报表绘制的宽度
这里的宽度和高度加上Paper size的Margins值 要小于等于Paper size属性的高度宽度
我的设置是Width:21cm,Height:10cm
即时上面设置没问题,如果打印机的设置有问题,打印的最终效果也会有差异,但是我不需要打印,所以没纠结他。
3.推荐个工具,做过ui的可能或者基本都知道,FSCapture,很好用,美工的mark图出的不好时,要做出1:1的ui,就得靠自己了,这次做报表也多亏他。1:1真的很烦。
ReportViewer 不预览,直接导出 PDF文件