/// <summary> /// Export to PDF /// </summary> public void ExportToPDF() { // If there are no rows in the grid then we cannot export if (this.applicationsDataSet.Tables[0].Rows.Count == 0) { MessageBox.Show("There is no data to Export", "Export Error"); } else { // We need to temporarily set the grid view to 'Resize all columns' in order to get // the resultant PDF file formatted correctly. AutoFitStyle oldStyle = applicationsGridView.DisplayLayout.AutoFitStyle; applicationsGridView.DisplayLayout.AutoFitStyle = AutoFitStyle.ResizeAllColumns; // First browse for the folder / file that we will save SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.ValidateNames = false; saveFileDialog.FileName = headerLabel.Text + ".pdf"; saveFileDialog.Filter = "Adobe Acrobat Document (*.pdf)|*.pdf"; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { UltraGridDocumentExporter exporter = new UltraGridDocumentExporter(); exporter.Export(applicationsGridView, saveFileDialog.FileName, GridExportFileFormat.PDF); DesktopAlert.ShowDesktopAlert("Data successfully exported to '" + saveFileDialog.FileName + "'"); } // Populate the old autofit style this.applicationsGridView.DisplayLayout.AutoFitStyle = oldStyle; } }
/// <summary> /// Export to XPS /// </summary> public void ExportToXPS() { // If there are no rows in the grid then we cannot export if (this.applicationsDataSet.Tables[0].Rows.Count == 0) { MessageBox.Show("There is no data to Export", "Export Error"); } else { // First browse for the folder / file that we will save SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.ValidateNames = false; saveFileDialog.FileName = headerLabel.Text + ".xps"; saveFileDialog.Filter = "XML Paper Specification (*.xps)|*.xps"; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { UltraGridDocumentExporter exporter = new UltraGridDocumentExporter(); exporter.Export(applicationsGridView, saveFileDialog.FileName, GridExportFileFormat.XPS); DesktopAlert.ShowDesktopAlert("Data successfully exported to '" + saveFileDialog.FileName + "'"); } } }
private void btnExportToSelected_ButtonClick(object sender, EventArgs e) { var item = (ToolStripSplitButton)sender; string selectedFormat = item.Tag.ToString(); if (selectedFormat == ExportFormats.Excel) { var exp = new UltraGridExcelExporter(); SaveFileDialog sfd = DialogFactory.Instance.GetSaveFormatDialog("Excel|*.xls"); if (sfd.ShowDialog() == DialogResult.OK) { exp.Export(_view.grdData, sfd.FileName, WorkbookFormat.Excel97To2003); } } else if (selectedFormat == ExportFormats.Excel2007) { var exp = new UltraGridExcelExporter(); SaveFileDialog sfd = DialogFactory.Instance.GetSaveFormatDialog("Excel 2007|*.xlsx"); if (sfd.ShowDialog() == DialogResult.OK) { exp.Export(_view.grdData, sfd.FileName, WorkbookFormat.Excel2007); } } else if (selectedFormat == ExportFormats.Pdf) { var exp = new UltraGridDocumentExporter(); SaveFileDialog sfd = DialogFactory.Instance.GetSaveFormatDialog("Pdf|*.pdf"); if (sfd.ShowDialog() == DialogResult.OK) { exp.Export(_view.grdData, sfd.FileName, GridExportFileFormat.PDF); } } else if (selectedFormat == ExportFormats.Xps) { var exp = new UltraGridDocumentExporter(); SaveFileDialog sfd = DialogFactory.Instance.GetSaveFormatDialog("Xps|*.xps"); if (sfd.ShowDialog() == DialogResult.OK) { exp.Export(_view.grdData, sfd.FileName, GridExportFileFormat.XPS); } } else if (selectedFormat == ExportFormats.Xml) { // var exp = new UltraGridDocumentExporter(); SaveFileDialog sfd = DialogFactory.Instance.GetSaveFormatDialog("Xml|*.xml"); if (sfd.ShowDialog() == DialogResult.OK) { if (_view.grdData.DataSource is DataSet) { var ds = (DataSet)_view.grdData.DataSource; ds.WriteXml(sfd.FileName, XmlWriteMode.IgnoreSchema); } else if (_view.grdData.DataSource is DataTable) { var dt = (DataTable)_view.grdData.DataSource; dt.WriteXml(sfd.FileName, XmlWriteMode.IgnoreSchema); } } } }
private void esportaGridPdf() { try { ultraGridDocumentExporter1 = new UltraGridDocumentExporter(); var grids = getFormGrid(MainStandardForm_Fill_Panel.Controls); if (grids.Count > 0) { var opzioni = new OpzioniStampa(); if (opzioni.ShowDialog() == DialogResult.OK) { var grid = findCurrentGrid(grids); var pathTempFile = Utility.GetTempFolder(grid.DisplayLayout.Bands[0].Header.Caption + ".pdf", "pdf"); if (opzioni.StampaLogo) ultraGridDocumentExporter1.ExportStarted += ultraGridDocumentExporter1ExportStarted; var style = grid.DisplayLayout.ViewStyle; if (!opzioni.VisualizzaDettagli) grid.DisplayLayout.ViewStyle = ViewStyle.SingleBand; var restoreDeleteColumn = false; if (grid.DisplayLayout.Bands[0].Columns.Exists("DeleteColumn")) { grid.DisplayLayout.Bands[0].Columns["DeleteColumn"].Hidden = true; restoreDeleteColumn = true; } var rowSelectors = grid.DisplayLayout.Bands[0].Override.RowSelectors; grid.DisplayLayout.Bands[0].Override.RowSelectors = DefaultableBoolean.False; ultraGridDocumentExporter1.TargetPaperOrientation = PageOrientation.Portrait; if (opzioni.GetOrientamento() == OrientamentoStampa.Orizzontale) ultraGridDocumentExporter1.TargetPaperOrientation = PageOrientation.Landscape; try { ultraGridDocumentExporter1.Export(grid, pathTempFile, GridExportFileFormat.PDF); } catch (Exception ex) { log.ErrorFormat("Errore l'esportazione della grid in pdf - EXPORT - {0} - grid:{1} - form text:{2} - path:{3} - azienda:{4}", ex, Utility.GetMethodDescription(), grid.Name, Text, pathTempFile, Login.Instance.CurrentLogin().Azienda); throw; } try { System.Diagnostics.Process.Start(pathTempFile); } catch (System.ComponentModel.Win32Exception exWin) { log.DebugFormat("Non è stata trovata nessuna applicazione per visualizzare il file pdf generato - {0} - grid:{1} - form text:{2} - path:{3} - azienda:{4}", exWin, Utility.GetMethodDescription(), grid.Name, Text, pathTempFile, Login.Instance.CurrentLogin().Azienda); MessageBox.Show("Non è stata trovata nessuna applicazione per visualizzare il file pdf generato.", "Apertura Documento", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { log.ErrorFormat("Errore l'esportazione della grid in pdf - APERTURA PDF - {0} - grid:{1} - form text:{2} - path:{3} - azienda:{4}", ex, Utility.GetMethodDescription(), grid.Name, Text, pathTempFile, Login.Instance.CurrentLogin().Azienda); throw; } grid.DisplayLayout.ViewStyle = style; grid.DisplayLayout.Bands[0].Override.RowSelectors = rowSelectors; if (restoreDeleteColumn) grid.DisplayLayout.Bands[0].Columns["DeleteColumn"].Hidden = false; } opzioni.Dispose(); } } catch (Exception ex) { log.Error("Errore durante l'esportazione della grid in pdf - " + Utility.GetMethodDescription() + " - azienda:" + Login.Instance.CurrentLogin().Azienda, ex); } }
/// <summary> /// Constructor /// </summary> /// <param name="toFile">Name of the file to export to</param> /// <param name="headerText">Main Header text</param> /// <param name="subheadingText">Sub-heading text</param> /// <param name="footerText">footer text</param> /// <param name="grid">Grid View to export</param> /// <param name="outputFormat">Output format</param> public static void Export(String toFile , String headerText , String subheadingText , String footerText , UltraGrid grid , FileFormat outputFormat) { // Create the report itself Report report = new Report(); // ...then the sections for header, footer and body ISection mainSection = report.AddSection(); mainSection.PageMargins = new Infragistics.Documents.Reports.Report.Margins(50); ISectionHeader headerSection = mainSection.AddHeader(); headerSection.Height = 50; headerSection.Repeat = true; // Add a footer for page numbering ISectionFooter footerSection = mainSection.AddFooter(); // Create place-holder for header text Infragistics.Documents.Reports.Report.Text.IText headerTextSection = headerSection.AddText(0, 0); // ...and add the header in to this section, centralized headerTextSection.AddContent(headerText); headerTextSection.Alignment.Horizontal = Alignment.Center; headerTextSection.Alignment.Vertical = Alignment.Middle; // Set style for the header Infragistics.Documents.Reports.Report.Text.Style HeaderStyle = new Infragistics.Documents.Reports.Report.Text.Style(new Infragistics.Documents.Reports.Graphics.Font("Verdana", 10, Infragistics.Documents.Reports.Graphics.FontStyle.Underline), Infragistics.Documents.Reports.Graphics.Brushes.DarkBlue); headerTextSection.Style = HeaderStyle; // Add in a sub-heading if required if (subheadingText != null && subheadingText != "") { headerTextSection = headerSection.AddText(0, 20); Infragistics.Documents.Reports.Report.Text.Style subHeaderStyle = new Infragistics.Documents.Reports.Report.Text.Style(new Infragistics.Documents.Reports.Graphics.Font("Verdana", 8, Infragistics.Documents.Reports.Graphics.FontStyle.Underline), Infragistics.Documents.Reports.Graphics.Brushes.DarkBlue); headerTextSection.Style = subHeaderStyle; headerTextSection.AddContent(subheadingText); } // Add the body of the report which is the contents of the grid UltraGridDocumentExporter exporter = new UltraGridDocumentExporter(); exporter.Export(grid, mainSection); //------------------------- // Setup Footer content //------------------------- footerSection.Height = 50; // Do we have a footer to display? if (footerText != null && footerText != "") { // Create place-holder for footer text Infragistics.Documents.Reports.Report.Text.IText footerTextSection = footerSection.AddText(0, 0); // ...and add the footer text in to this section, left aligned footerTextSection.Alignment.Horizontal = Alignment.Left; footerTextSection.Alignment.Vertical = Alignment.Top; // Set style for the text Infragistics.Documents.Reports.Report.Text.Style footerStyle = new Infragistics.Documents.Reports.Report.Text.Style(new Infragistics.Documents.Reports.Graphics.Font("Verdana", 6, Infragistics.Documents.Reports.Graphics.FontStyle.Underline), Infragistics.Documents.Reports.Graphics.Brushes.DarkBlue); footerTextSection.Style = footerStyle; // ...and set the text itself footerTextSection.AddContent(footerText); } PageNumbering pn = mainSection.PageNumbering; // The Template property is the actual string that shows the page numbering. Use the [Page #] place- // holder for the current page and the [TotalPages] place-holder for the total amount of pages in // the entire document. pn.Template = "Page [Page #] of [TotalPages]"; // Setting SkipFirst to true does not place page numbering on the first page of the section. This // is useful if the first page is a Title page. pn.SkipFirst = false; // The page numbering will be aligned with the right side of the page. Valid values off the // Alignment enum include Left, Center, and Right. pn.Alignment.Horizontal = Alignment.Right; // The page numbering will be located at the bottom of the page. Valid values off the // Alignment enum include Top and Bottom. pn.Alignment.Vertical = Alignment.Bottom; // The page numbering is at the extreme bottom of the page, so we need to change the Y Offset // in order to bring it in line with the rest of the page footer text. pn.OffsetY = -18; // Delete the old report if it exists try { if (File.Exists(toFile)) { File.Delete(toFile); } } catch (Exception) { //Ignore any errors } // Generate the report try { report.Generate(); report.Publish(toFile, outputFormat); } catch (Exception ex) { MessageBox.Show("Failed to write the export file " + toFile + ", the error was " + ex.Message); } }
/// <summary> /// Export the /// </summary> /// <param name="ToFile"></param> /// <param name="listSections"></param> /// <param name="outputFormat"></param> public static void Export(string toFile, UltraGrid grid, ExportSectionList listSections, FileFormat outputFormat) { // Create the report itself Report report = new Report(); // ...then the sections for header, footer and body ISection mainSection = report.AddSection(); mainSection.PageMargins = new Infragistics.Documents.Reports.Report.Margins(50); ISectionHeader headerSection = mainSection.AddHeader(); headerSection.Height = 50; headerSection.Repeat = true; ISectionFooter footerSection = mainSection.AddFooter(); footerSection.Height = 50; // Add the body of the report which is the contents of the grid UltraGridDocumentExporter exporter = new UltraGridDocumentExporter(); exporter.Export(grid, mainSection); // Setup the page numbering PageNumbering pn = mainSection.PageNumbering; // The Template property is the actual string that shows the page numbering. Use the [Page #] place- // holder for the current page and the [TotalPages] place-holder for the total amount of pages in // the entire document. pn.Template = "Page [Page #] of [TotalPages]"; // Setting SkipFirst to true does not place page numbering on the first page of the section. This // is useful if the first page is a Title page. pn.SkipFirst = false; // The page numbering will be aligned with the right side of the page. Valid values off the // Alignment enum include Left, Center, and Right. pn.Alignment.Horizontal = Alignment.Right; // The page numbering will be located at the bottom of the page. Valid values off the // Alignment enum include Top and Bottom. pn.Alignment.Vertical = Alignment.Bottom; // The page numbering is at the extreme bottom of the page, so we need to change the Y Offset // in order to bring it in line with the rest of the page footer text. pn.OffsetY = -18; // Delete the old report if it exists try { if (File.Exists(toFile)) { File.Delete(toFile); } } catch (Exception) { //Ignore any errors } // Generate the report try { report.Generate(); report.Publish(toFile, outputFormat); } catch (Exception ex) { MessageBox.Show("Failed to write the export file " + toFile + ", the error was " + ex.Message); } }