加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ExportForm.cs 10.60 KB
一键复制 编辑 原始数据 按行查看 历史
LenkaGuo123 提交于 2015-09-17 10:27 . new version
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using GrapeCity.ActiveReports.Export.Html.Page;
using GrapeCity.ActiveReports.Extensibility.Rendering.IO;
using GrapeCity.ActiveReports.Rendering.IO;
using System.IO;
using GrapeCity.ActiveReports.Export.Image.Page;
using GrapeCity.ActiveReports.Export.Pdf.Page;
using GrapeCity.ActiveReports.Export.Rdf;
using GrapeCity.ActiveReports.Export.Word.Page;
using GrapeCity.ActiveReports.Export.Xml.Page;
using GrapeCity.ActiveReports.Export.Excel.Page;
using GrapeCity.ActiveReports.Design;
using GrapeCity.ActiveReports.Export.Excel.Section;
using System.Xml;
using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.Viewer.Win;
namespace ActiveReportsDemo
{
public partial class ExportForm : Form
{
readonly DesignerReportType _reportType;
readonly object _report;
private readonly Viewer _reportViewer;
readonly SectionReport _sectionReport = new SectionReport();
private GrapeCity.ActiveReports.Document.PageDocument _runtime;
public ExportForm(DesignerReportType reportType, object report, Viewer reportViewer)
{
_reportType = reportType;
_report = report;
_reportViewer = reportViewer;
InitializeComponent();
}
public ExportForm(GrapeCity.ActiveReports.Document.PageDocument _runtime)
{
// TODO: Complete member initialization
this._runtime = _runtime;
}
void AddPageReportExportTypes()
{
List<PageExport> pageExports = new List<PageExport>();
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageImageString,
DefaultFileName = Properties.Resources.ImageFileName,
Settings = new GrapeCity.ActiveReports.Export.Image.Page.Settings(),
Filter = Properties.Resources.ImageFilter,
RenderingExtension = new ImageRenderingExtension(),
});
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageWordString,
DefaultFileName = Properties.Resources.WordFileName,
Settings = new GrapeCity.ActiveReports.Export.Word.Page.Settings(),
Filter = Properties.Resources.WordFilter,
RenderingExtension = new WordRenderingExtension(),
});
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageHTMLString,
DefaultFileName = Properties.Resources.HTMLFileName,
Settings = new GrapeCity.ActiveReports.Export.Html.Page.Settings(),
Filter = Properties.Resources.HTMLFilter,
RenderingExtension = new HtmlRenderingExtension(),
});
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PagePDFString,
DefaultFileName = Properties.Resources.PDFFileName,
Settings = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings(),
Filter = Properties.Resources.PDFFilter,
RenderingExtension = new PdfRenderingExtension(),
});
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageRDFString,
DefaultFileName = Properties.Resources.RDFFileName,
Settings = new GrapeCity.ActiveReports.Export.Rdf.Settings(),
Filter = Properties.Resources.RdfFilter,
RenderingExtension = new RdfRenderingExtension(),
});
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageXMLString,
DefaultFileName = Properties.Resources.XMLFileName,
Settings = new GrapeCity.ActiveReports.Export.Xml.Page.Settings(),
Filter = Properties.Resources.XmlFilter,
RenderingExtension = new XmlRenderingExtension(),
});
//Excel Rendering Extension
pageExports.Add(new PageExport
{
DisplayName = (_reportType == DesignerReportType.Rdl) ? Properties.Resources.PageExcelRE : Properties.Resources.PageExcelString,
DefaultFileName = Properties.Resources.ExcelFileName,
Settings = new ExcelRenderingExtensionSettings(),
Filter = Properties.Resources.ExcelFilter,
RenderingExtension = new ExcelRenderingExtension(),
});
if (_reportType == DesignerReportType.Rdl)
{
pageExports.Add(new PageExport
{
DisplayName = Properties.Resources.PageTransformationDeviceString,
DefaultFileName = Properties.Resources.ExcelFileName,
Settings = new GrapeCity.ActiveReports.Export.Excel.Page.Settings(),
Filter = Properties.Resources.ExcelFilter,
RenderingExtension = new ExcelTransformationDevice(),
});
}
cmbExportFormat.DataSource = pageExports;
cmbExportFormat.DisplayMember = "DisplayName";
cmbExportFormat.SelectedIndex = 0;
}
void AddSectionReportExportTypes()
{
List<SectionExport> sectionExports = new List<SectionExport>();
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionHTMLString,
Filter = Properties.Resources.HTMLFilter,
DefaultFileName = Properties.Resources.HTMLFileName,
Settings = new GrapeCity.ActiveReports.Export.Html.Section.HtmlExport()
});
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionPDFString,
Filter = Properties.Resources.PDFFilter,
DefaultFileName = Properties.Resources.PDFFileName,
Settings = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport()
});
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionRTFString,
Filter = Properties.Resources.RtfFilter,
DefaultFileName = Properties.Resources.RtfFileName,
Settings = new GrapeCity.ActiveReports.Export.Word.Section.RtfExport()
});
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionTIFFString,
Filter = Properties.Resources.TiffFilter,
DefaultFileName = Properties.Resources.TiffFileName,
Settings = new GrapeCity.ActiveReports.Export.Image.Tiff.Section.TiffExport()
});
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionPlainTextString,
Filter = Properties.Resources.TextFilter,
DefaultFileName = Properties.Resources.TextFileName,
Settings = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport()
});
sectionExports.Add(new SectionExport
{
DisplayName = Properties.Resources.SectionExcelString,
Filter = Properties.Resources.ExcelFilter,
DefaultFileName = Properties.Resources.ExcelFileName,
Settings = new XlsExport()
});
cmbExportFormat.DataSource = sectionExports;
cmbExportFormat.DisplayMember = "DisplayName";
cmbExportFormat.SelectedIndex = 0;
}
private void ExportForm_Load(object sender, EventArgs e)
{
if (_reportType == DesignerReportType.Section)
{
AddSectionReportExportTypes();
}
if ((_reportType == DesignerReportType.Page) || (_reportType == DesignerReportType.Rdl))
{
AddPageReportExportTypes();
}
}
private void cmbExportFormat_SelectedIndexChanged(object sender, EventArgs e)
{
if ((_reportType == DesignerReportType.Page) || (_reportType == DesignerReportType.Rdl))
{
exportPropertyGrid.SelectedObject = ((PageExport)cmbExportFormat.SelectedItem).Settings;
}
if (_reportType == DesignerReportType.Section)
{
exportPropertyGrid.SelectedObject = ((SectionExport)cmbExportFormat.SelectedItem).Settings;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Hide();
}
private void btnOk_Click(object sender, EventArgs e)
{
switch (_reportType)
{
case DesignerReportType.Section:
exportSaveFileDialog.Filter = ((SectionExport)cmbExportFormat.SelectedItem).Filter;
exportSaveFileDialog.FileName = ((SectionExport)cmbExportFormat.SelectedItem).DefaultFileName;
if (exportPropertyGrid.SelectedObject is XlsExport)
{
switch (((XlsExport)exportPropertyGrid.SelectedObject).FileFormat)
{
case GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx:
exportSaveFileDialog.FileName = Properties.Resources.Excel2007FileName;
exportSaveFileDialog.Filter = Properties.Resources.Excel2007Filter;
break;
}
}
if (exportSaveFileDialog.ShowDialog() == DialogResult.OK)
{
RunSectionReport();
if (File.Exists(exportSaveFileDialog.FileName))
{
File.Delete(exportSaveFileDialog.FileName);
}
((SectionExport)cmbExportFormat.SelectedItem).Export(_sectionReport.Document, exportSaveFileDialog.FileName);
MessageBox.Show(((SectionExport)cmbExportFormat.SelectedItem).DisplayName + " " + Properties.Resources.ExportCompleteString);
Hide();
}
break;
case DesignerReportType.Page:
case DesignerReportType.Rdl:
exportSaveFileDialog.AddExtension = false;
exportSaveFileDialog.Filter = ((PageExport)cmbExportFormat.SelectedItem).Filter;
exportSaveFileDialog.FileName = ((PageExport)cmbExportFormat.SelectedItem).DefaultFileName;
if (exportPropertyGrid.SelectedObject is GrapeCity.ActiveReports.Export.Image.Page.Settings)
{
GrapeCity.ActiveReports.Export.Image.Page.Settings _settings = (GrapeCity.ActiveReports.Export.Image.Page.Settings)exportPropertyGrid.SelectedObject;
exportSaveFileDialog.FileName = ((PageExport)cmbExportFormat.SelectedItem).DefaultFileName + "." + _settings.ImageType.ToString().ToLower();
}
if (exportPropertyGrid.SelectedObject is ExcelRenderingExtensionSettings &&
((ExcelRenderingExtensionSettings) exportPropertyGrid.SelectedObject).FileFormat ==
GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx)
{
exportSaveFileDialog.FileName = Properties.Resources.Excel2007FileName;
exportSaveFileDialog.Filter = Properties.Resources.Excel2007Filter;
}
if (exportSaveFileDialog.ShowDialog() == DialogResult.OK)
{
if (File.Exists(exportSaveFileDialog.FileName))
{
File.Delete(exportSaveFileDialog.FileName);
}
PageExport tempQualifier = ((PageExport)cmbExportFormat.SelectedItem);
string filePath = exportSaveFileDialog.FileName;
StreamProvider StreamProvider = new FileStreamProvider(new DirectoryInfo(Path.GetDirectoryName(filePath)), Path.GetFileNameWithoutExtension(filePath));
_reportViewer.Render(tempQualifier.RenderingExtension, StreamProvider, tempQualifier.Settings.GetSettings());
MessageBox.Show(((PageExport)cmbExportFormat.SelectedItem).DisplayName + " " + Properties.Resources.ExportCompleteString);
Hide();
}
break;
}
}
void RunSectionReport()
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Position = 0;
using (XmlWriter writer = XmlWriter.Create(memoryStream))
{
((SectionReport)_report).SaveLayout(writer);
}
memoryStream.Position = 0;
using (XmlReader reader = XmlReader.Create(memoryStream))
{
_sectionReport.LoadLayout(reader);
}
_sectionReport.Document.Printer.PrinterName = "";
_sectionReport.Run();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化