作为笔记记着,以免以后再到处找资料
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 {