public static IWorkbookFormatProvider GetFormatProvider(string extension) { IWorkbookFormatProvider formatProvider; switch (extension) { case XlsxFormat: formatProvider = new XlsxFormatProvider(); break; case CsvFormat: formatProvider = new CsvFormatProvider(); ((CsvFormatProvider)formatProvider).Settings.HasHeaderRow = true; break; case TxtFormat: formatProvider = new TxtFormatProvider(); break; case PdfFormat: formatProvider = new PdfFormatProvider(); break; default: formatProvider = null; break; } return(formatProvider); }
public static void ExportPivotToExcel(RadPivotGrid pivot) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "xlsx"; // dialog.Filter = "CSV files (*.csv)|*.csv |All Files (*.*) | *.*"; var result = dialog.ShowDialog(); if ((bool)result) { try { using (var stream = dialog.OpenFile()) { var workbook = GenerateWorkbook(pivot); // IWorkbookFormatProvider formatProvider = new CsvFormatProvider(); //formatProvider.Export(workbook, stream); XlsxFormatProvider provider = new XlsxFormatProvider(); provider.Export(workbook, stream); } } catch (IOException ex) { MessageBox.Show(ex.Message); } } }
private void ExportToExcel() { this.BusyIndicator.IsBusy = true; SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "xlsx"; dialog.Filter = "Excel Workbook (xlsx) | *.xlsx |All Files (*.*) | *.*"; var result = dialog.ShowDialog(); if ((bool)result) { try { using (var stream = dialog.OpenFile()) { var workbook = GenerateWorkbook(); XlsxFormatProvider provider = new XlsxFormatProvider(); provider.Export(workbook, stream); } } catch (IOException ex) { MessageBox.Show(ex.Message); } } this.BusyIndicator.IsBusy = false; }
public static void ExportPivotToExcel(RadPivotGrid pivot) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "xlsx"; // dialog.Filter = "CSV files (*.csv)|*.csv |All Files (*.*) | *.*"; var result = dialog.ShowDialog(); if ((bool)result) { try { using (var stream = dialog.OpenFile()) { var workbook = GenerateWorkbook(pivot); // IWorkbookFormatProvider formatProvider = new CsvFormatProvider(); //formatProvider.Export(workbook, stream); XlsxFormatProvider provider = new XlsxFormatProvider(); provider.Export(workbook, stream); } } catch (IOException ex) { MessageBox.Show(ex.Message); } } }
static void Main(string[] args) { Console.WriteLine("Hello, would you like to create a workbook? [Y/y][N/n]:"); var result = Console.ReadLine(); if (result?.ToLower() == "y") { Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets.Add(); CellSelection selection = worksheet.Cells[1, 1]; //B2 cell selection.SetValue("Hello RadSpreadProcessing"); string fileName = "SampleFile.xlsx"; IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); using Stream output = new FileStream(fileName, FileMode.Create); formatProvider.Export(workbook, output); Console.WriteLine("Done!"); } }
public async void ViewFormattedDocument(object parameter) { IWorkbookFormatProvider provider = new XlsxFormatProvider(); Workbook workbook; try { Assembly assembly = typeof(ConditionalFormattingViewModel).Assembly; string fileName = assembly.GetManifestResourceNames().First(n => n.Contains("SpreadProcessingDocument2.xlsx")); using (Stream stream = assembly.GetManifestResourceStream(fileName)) { workbook = provider.Import(stream); } this.AddRules(workbook); Stream mystream = await OpenStreamAsync(workbook); using (mystream) { IFileViewerService fileViewerService = DependencyService.Get <IFileViewerService>(); await fileViewerService.View(mystream, fileName); } } catch { IMessageService messageService = DependencyService.Get <IMessageService>(); await messageService.ShowMessage("An error occured", "An error occured, please try again."); } }
public void Export(string format) { var workbook = this.GenerateWorkBook(); var extension = format; SaveFileDialog dialog = new SaveFileDialog() { DefaultExt = extension, FileName = "File", Filter = extension + "files (*." + extension + ")|*." + extension + "|All files (*.*)|*.*", FilterIndex = 1 }; if (dialog.ShowDialog() == true) { BinaryWorkbookFormatProviderBase provider; if (format == "xlsx") { provider = new XlsxFormatProvider(); } else { provider = new PdfFormatProvider(); } using (Stream output = dialog.OpenFile()) { provider.Export(workbook, output); } this.subItemsCount = 0; this.parentItemsDictionary.Clear(); } }
public static IWorkbookFormatProvider GetFormatProvider(string extension) { IWorkbookFormatProvider formatProvider; switch (extension) { case XlsxFormat: formatProvider = new XlsxFormatProvider(); break; case CsvFormat: formatProvider = new CsvFormatProvider(); ((CsvFormatProvider)formatProvider).Settings.HasHeaderRow = true; break; case TxtFormat: formatProvider = new TxtFormatProvider(); break; case PdfFormat: formatProvider = new PdfFormatProvider(); break; default: formatProvider = null; break; } return formatProvider; }
void LoadResourceFile(string filePath) { XlsxFormatProvider formatProvider = new XlsxFormatProvider(); using (var stream = ResourceHelper.GetResourceStream(filePath)) { this.radSpreadsheet.Workbook = formatProvider.Import(stream); } }
private static Stream OpenStream(Workbook workbook) { IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); MemoryStream stream = new MemoryStream(); formatProvider.Export(workbook, stream); return(stream); }
private void ExportToXlsxOnFileSystem(object sender, RoutedEventArgs e) { Workbook target = null; string fileName = null; //export the data from RadGridView Workbook playersDoc = this.playersGrid.ExportToWorkbook(); OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "Workbooks|*.xlsx"; openDialog.Multiselect = false; //load XLSX document from the file system bool?dialogResult = openDialog.ShowDialog(); if (dialogResult == true) { using (Stream stream = openDialog.OpenFile()) { target = new XlsxFormatProvider().Import(stream); fileName = openDialog.FileName; } bool containsSameName = false; //check if target file contains sheet with the same name foreach (var worksheet in target.Worksheets) { if (worksheet.Name == playersDoc.Sheets[0].Name) { containsSameName = true; break; } } if (containsSameName) { //replate the content of the sheet with the exported data from RadGridView target.Worksheets[playersDoc.Sheets[0].Name].CopyFrom(playersDoc.Sheets[0] as Worksheet); } else { //create new sheet and add the exported data from RadGridView to it Worksheet clonedSheet = target.Worksheets.Add(); clonedSheet.CopyFrom(playersDoc.Sheets[0] as Worksheet); clonedSheet.Name = playersDoc.Sheets[0].Name; } //export the combined document back at the same path using (FileStream output = new FileStream(fileName, FileMode.Create)) { new XlsxFormatProvider().Export(target, output); } MessageBox.Show("Check updated file: " + fileName); } }
public JsonResult Export(string model, string data, string format, string title) { Workbook workbook = new Workbook(); workbook.History.IsEnabled = false; workbook.SuspendLayoutUpdate(); workbook.Sheets.Add(SheetType.Worksheet); Worksheet worksheet = workbook.ActiveWorksheet; worksheet.Name = title; var modelObject = JsonConvert.DeserializeObject <dynamic>(model); var dataObject = JsonConvert.DeserializeObject <dynamic>(data); var columnCount = modelObject.Count; for (int idx = 0; idx < columnCount; idx++) { var modelCol = modelObject[idx]; string columnName = modelCol.title ?? modelCol.field; worksheet.Cells[0, idx].SetValue(columnName); } for (int rowIdx = 1; rowIdx < dataObject.Count; rowIdx++) { for (int colIdx = 0; colIdx < modelObject.Count; colIdx++) { worksheet.Cells[rowIdx, colIdx].SetValue(dataObject[rowIdx - 1][modelObject[colIdx].field.ToString()].ToString()); } } if (format == "XLSX") { ColumnSelection columnSelection = worksheet.Columns[0, columnCount]; columnSelection.AutoFitWidth(); } workbook.ResumeLayoutUpdate(); IWorkbookFormatProvider formatProvider = null; if (format == "XLSX") { formatProvider = new XlsxFormatProvider(); } else { formatProvider = new CsvFormatProvider(); } using (MemoryStream output = new MemoryStream()) { formatProvider.Export(workbook, output); Session[title] = output.ToArray(); } return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
private static void SaveWorkbook(Workbook workbook, Stream stream) { XlsxFormatProvider provider = new XlsxFormatProvider(); using (stream) { provider.Export(workbook, stream); } }
void LoadResourceFile(string filePath) { XlsxFormatProvider formatProvider = new XlsxFormatProvider(); using (var stream = ResourceHelper.GetResourceStream(filePath)) { this.radSpreadsheet.Workbook = formatProvider.Import(stream); } }
public JsonResult Export(string model, string data, string format, string title) { Workbook workbook = new Workbook(); workbook.History.IsEnabled = false; workbook.SuspendLayoutUpdate(); workbook.Sheets.Add(SheetType.Worksheet); Worksheet worksheet = workbook.ActiveWorksheet; worksheet.Name = title; var modelObject = JsonConvert.DeserializeObject<IList<ColumnSettings>>(model); var dataObject = JsonConvert.DeserializeObject<dynamic>(data); var columnCount = modelObject.Count; for (int idx = 0; idx < columnCount; idx++) { var modelCol = modelObject[idx]; string columnName = modelCol.Title ?? modelCol.Field; worksheet.Cells[0, idx].SetValue(columnName); } for (int rowIdx = 1; rowIdx < dataObject.Count; rowIdx++) { for (int colIdx = 0; colIdx < modelObject.Count; colIdx++) { worksheet.Cells[rowIdx, colIdx].SetValue(dataObject[rowIdx - 1][modelObject[colIdx].Field].Value); } } if (format == "XLSX") { ColumnSelection columnSelection = worksheet.Columns[0, columnCount]; columnSelection.AutoFitWidth(); } workbook.ResumeLayoutUpdate(); IWorkbookFormatProvider formatProvider = null; if (format == "XLSX") { formatProvider = new XlsxFormatProvider(); } else { formatProvider = new CsvFormatProvider(); } using (MemoryStream output = new MemoryStream()) { formatProvider.Export(workbook, output); Session[title] = output.ToArray(); } return Json(new { success = true }, JsonRequestBehavior.AllowGet); }
private static Workbook ImportWorkbook() { Assembly assembly = typeof(FindAndReplaceViewModel).Assembly; string fileName = assembly.GetManifestResourceNames().First(n => n.Contains("SpreadProcessingDocument1.xlsx")); using (Stream stream = assembly.GetManifestResourceStream(fileName)) { IWorkbookFormatProvider provider = new XlsxFormatProvider(); Workbook workbook = provider.Import(stream); return(workbook); } }
public void GetText(Stream doc, Stream text) { ExecuteDocumentAction(() => { byte[] data; using (var reader = new BinaryReader(doc)) data = reader.ReadBytes((int)doc.Length); Workbook intermediateDoc = new XlsxFormatProvider().Import(data); new TxtFormatProvider().Export(intermediateDoc, text); }); }
private void SaveFile_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); XlsxFormatProvider formatProvider = new XlsxFormatProvider(); sfd.Filter = "Excel Workbook (*xlsx)|*.xlsx|All Files (*.*)|*.*"; if (sfd.ShowDialog() == true) { using (Stream output = sfd.OpenFile()) { formatProvider.Export(this.radSpreadsheet.Workbook, output); } } }
private void OpenFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); XlsxFormatProvider formatProvider = new XlsxFormatProvider(); ofd.Filter = "Excel Workbook (*xlsx)|*.xlsx|All Files (*.*)|*.*"; if (ofd.ShowDialog() == true) { using (Stream input = ofd.OpenRead()) { this.radSpreadsheet.Workbook = formatProvider.Import(input); } } }
private void ExportXlsx() { #region radspreadsheet-model-import-export-xlsxformatprovider-wpf_1 Workbook workbook = new Workbook(); workbook.Worksheets.Add(); string fileName = "SampleFile.xlsx"; IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); using (FileStream output = new FileStream(fileName, FileMode.Create)) { formatProvider.Export(workbook, output); } #endregion }
public void ImportWorkbookFromXlsx() { #region radspreadprocessing-working-with-workbooks-create-open-and-save-workbooks_1 const string FilePath = @"http://localhost:54352/Resourses/SampleFile.xlsx"; WebClient webClient = new WebClient(); webClient.OpenReadCompleted += (sender, eventArgs) => { XlsxFormatProvider formatProvider = new XlsxFormatProvider(); Workbook workbook = formatProvider.Import(eventArgs.Result); }; webClient.OpenReadAsync(new Uri(FilePath)); #endregion }
private void ExportSpreadsheet() { #region radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider_1 Workbook workbook = new Workbook(); workbook.Worksheets.Add(); string fileName = "SampleFile.xlsx"; IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); using (FileStream output = new FileStream(fileName, FileMode.Create)) { formatProvider.Export(workbook, output); } #endregion }
protected void btnExportXLSX_Click(object sender, EventArgs e) { IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); Workbook workbook = createWorkbook(); byte[] renderedBytes = null; using (MemoryStream ms = new MemoryStream()) { formatProvider.Export(workbook, ms); renderedBytes = ms.ToArray(); } Response.ClearHeaders(); Response.ClearContent(); Response.AppendHeader("content-disposition", "attachment; filename=DYEP-RT-Report-" + DateTime.Now.ToString("dd-MM-yyyy") + ".xlsx"); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.BinaryWrite(renderedBytes); Response.End(); }
private void gridFacturas_CellDoubleClick(object sender, GridViewCellEventArgs e) { try { Limpiar(); btnActualizar.Visible = true; btnInsertar.Visible = false; txtIdFac.Text = gridFacturas.CurrentRow.Cells["FacturaId"].Value?.ToString(); txtEstado.Text = gridFacturas.CurrentRow.Cells["Estado"].Value?.ToString(); txtNumFactura.Text = gridFacturas.CurrentRow.Cells["NumeroFactura"].Value?.ToString(); numFacAnt = gridFacturas.CurrentRow.Cells["NumeroFactura"].Value?.ToString(); dateFactura.Value = Convert.ToDateTime(gridFacturas.CurrentRow.Cells["FechaEmision"].Value?.ToString()); txtRutaFac.Text = gridFacturas.CurrentRow.Cells["ArchivoFactura"].Value?.ToString(); rutaFacAnt = gridFacturas.CurrentRow.Cells["ArchivoFactura"].Value?.ToString(); var rutaFile = Path.Combine(directorio, txtRutaFac.Text); if (Path.GetExtension(rutaFile) == ".xml") { radSpreadsheet1.Visible = false; webBrowser1.Visible = true; webBrowser1.Navigate(rutaFile); } else if (Path.GetExtension(rutaFile) == ".xlsx") { radSpreadsheet1.Visible = true; webBrowser1.Visible = false; var formatProvider = new XlsxFormatProvider(); using (FileStream input = new FileStream(rutaFile, FileMode.Open)) { radSpreadsheet1.Workbook = formatProvider.Import(input); } } tabForm.SelectedTab = tabManteniminento; } catch { MessageBox.Show("No hay ningún registro que selecionar"); } }
private void ImportWorkbookFromXlsxUsingOpenFileDialog() { #region radspreadsheet-model-import-export-xlsxformatprovider-silverlight_1 Workbook workbook; OpenFileDialog openFileDialog = new OpenFileDialog(); XlsxFormatProvider formatProvider = new XlsxFormatProvider(); openFileDialog.Filter = "Excel Workbook (*.xlsx)|*.xlsx|All Files (*.*)|*.*"; if (openFileDialog.ShowDialog() == true) { using (Stream input = openFileDialog.File.OpenRead()) { workbook = formatProvider.Import(input); } } #endregion }
private async Task <Workbook> OpenExcelFile() { IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); Workbook wb; try { using (FileStream input = new FileStream(fileInfo.FullName, FileMode.Open)) { wb = formatProvider.Import(input); return(wb); } } catch (Exception) { throw new Exception($"Die Datei {fileInfo.Name} kann nicht geöffnet werden. Sie wird möglicherweise von einem anderen Prozess benutzt. "); } }
private void ImportSpreadsheet() { #region radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider_0 string fileName = "SampleFile.xlsx"; if (!File.Exists(fileName)) { throw new FileNotFoundException(String.Format("File {0} was not found!", fileName)); } Workbook workbook; IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); using (FileStream input = new FileStream(fileName, FileMode.Open)) { workbook = formatProvider.Import(input); } #endregion }
private void btnCargarFac_Click(object sender, EventArgs e) { try { openFileDialog1.FileName = ""; openFileDialog1.Filter = "Archivos Excel o Xml (*.xlsx, *.xls, *.xml)|*.xlsx;*.xls;*.xml"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (Path.GetExtension(openFileDialog1.FileName) == ".xlsx" || Path.GetExtension(openFileDialog1.FileName) == ".xls") { radSpreadsheet1.Visible = true; webBrowser1.Visible = false; var formatProvider = new XlsxFormatProvider(); using (FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open)) { radSpreadsheet1.Workbook = formatProvider.Import(input); } txtRutaFac.Text = Path.GetFileName(openFileDialog1.FileName); this.RutaOrigen = openFileDialog1.FileName; } else if (Path.GetExtension(openFileDialog1.FileName) == ".xml") { radSpreadsheet1.Visible = false; webBrowser1.Visible = true; webBrowser1.Navigate(openFileDialog1.FileName); txtRutaFac.Text = Path.GetFileName(openFileDialog1.FileName); this.RutaOrigen = openFileDialog1.FileName; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void ExportToExcelButton_Click(object sender, RoutedEventArgs e) { dataTables = new List <DataTable>(); DataTable Datos; sheetNames = new List <string>(); foreach (InputGroupViewModel group in inputGroups) { foreach (InputEntityViewModel entity in group.Entities) { if (entity.IsChecked) { var nnn = ((DataControl)entity.DataGridView).ItemsSource; Datos = nnn as DataTable; dataTables.Add(nnn as DataTable); //dataTables.Add(entity.DataGridView.ItemsSource as DataTable); //sheetNames.Add(entity.ShortName); sheetNames.Add(entity.Name); //ExcelExporter.ExportDataTableToExcelSheet("archivo", Datos, sheetNames); } } } if (dataTables.Any()) { SaveFileDialog dialog = new SaveFileDialog(); IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); dialog.FileName = "ResultadosDHOG"; dialog.Filter = String.Format("{0} files|*{1}|All files (*.*)|*.*", "xlsx", formatProvider.SupportedExtensions.First()); if (dialog.ShowDialog() == true) { ExportBusyIndicator.IsBusy = true; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += ExportToExcel; worker.RunWorkerCompleted += ExportCompleted; worker.RunWorkerAsync(dialog.FileName); } } else { MessageBox.Show("No se seleccionó la información a exportar.", MessageUtil.FormatMessage("LABEL.ExportToExcel"), MessageBoxButton.OK, MessageBoxImage.Error); } }
public void ExportWorkbookToXlsx() { #region radspreadsheet-model-import-export-xlsxformatprovider-silverlight_2 Workbook workbook = new Workbook(); workbook.Worksheets.Add(); SaveFileDialog saveFileDialog = new SaveFileDialog(); IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); saveFileDialog.Filter = "Excel Workbook (*.xlsx)|*.xlsx|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == true) { using (Stream output = saveFileDialog.OpenFile()) { formatProvider.Export(workbook, output); } } #endregion }
private Workbook LoadWorkbook() { Workbook result = null; try { XlsxFormatProvider formatProvider = new XlsxFormatProvider(); using (Stream stream = ResourceHelper.GetResourceStream(ResourceFilePath)) { result = formatProvider.Import(stream); } } catch { return(null); } return(result); }
private Workbook LoadWorkbook() { Workbook result = null; try { XlsxFormatProvider formatProvider = new XlsxFormatProvider(); using (Stream stream = ResourceHelper.GetResourceStream(ResourceFilePath)) { result = formatProvider.Import(stream); } } catch { return null; } return result; }
public void GettingStarted() { #region radspreadprocessing-getting-started_0 Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets.Add(); #endregion #region radspreadprocessing-getting-started_1 CellSelection selection = worksheet.Cells[1, 1]; //B2 cell selection.SetValue("Hello RadSpreadProcessing"); #endregion #region radspreadprocessing-getting-started_2 string fileName = "Hello.xlsx"; IWorkbookFormatProvider formatProvider = new XlsxFormatProvider(); using (FileStream input = new FileStream(fileName, FileMode.Open)) { workbook = formatProvider.Import(input); } #endregion }
protected virtual void ExportButton_Click(object sender, EventArgs e) { if (this.gridControl.MasterViewInfo.IsWaiting) { RadMessageBox.Show("The Data is not loaded! Please wait!"); return; } SaveFileDialog savefile = new SaveFileDialog(); savefile.FileName = "Document.xlsx"; savefile.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; if (savefile.ShowDialog() == DialogResult.OK) { var formatProvider = new XlsxFormatProvider(); using (Stream output = new FileStream(savefile.FileName, FileMode.Create)) { formatProvider.Export(CreateWorkbook(), output); } } }
private static Workbook CreateWorkBook(RadGridView grid) { Workbook book = null; using (var stream = new MemoryStream()) { if (grid != null) { grid.ExportToXlsx(stream, new GridViewDocumentExportOptions() { ShowColumnFooters = grid.ShowColumnFooters, ShowColumnHeaders = grid.ShowColumnHeaders, ShowGroupFooters = grid.ShowGroupFooters, ExportDefaultStyles = true }); } stream.Position = 0; book = new XlsxFormatProvider().Import(stream); } return(book); }
private static Workbook CreateWorkBook(RadGridView grid) { Workbook book = null; using (var stream = new MemoryStream()) { if (grid != null) { grid.ExportToXlsx(stream, new GridViewDocumentExportOptions() { ShowColumnFooters = grid.ShowColumnFooters, ShowColumnHeaders = grid.ShowColumnHeaders, ShowGroupFooters = grid.ShowGroupFooters, ExportDefaultStyles = true }); } stream.Position = 0; book = new XlsxFormatProvider().Import(stream); } return book; }
public static void Export(this RadGridView grid, ExportSettings settings) { var dialog = new SaveFileDialog(); switch (settings.Format) { case ExportFormat.Pdf: dialog.DefaultExt = "*.pdf"; dialog.Filter = "Adobe PDF 文档 (*.pdf)|*.pdf"; break; case ExportFormat.Excel: dialog.DefaultExt = "*.xlsx"; dialog.Filter = "Excel 工作簿 (*.xlsx)|*.xlsx"; break; case ExportFormat.Word: dialog.DefaultExt = "*.docx"; dialog.Filter = "Word 文档 (*.docx)|*.docx"; break; case ExportFormat.Csv: dialog.DefaultExt = "*.csv"; dialog.Filter = "CSV (逗号分隔) (*.csv)|*.csv"; break; } if (settings.FileName != null) { dialog.DefaultFileName = settings.FileName; } if (dialog.ShowDialog() == true) { switch (settings.Format) { case ExportFormat.Csv: using (var output = dialog.OpenFile()) { grid.Export(output, new GridViewExportOptions { Format = Telerik.Windows.Controls.ExportFormat.Csv, ShowColumnFooters = grid.ShowColumnFooters, ShowColumnHeaders = grid.ShowColumnHeaders, ShowGroupFooters = grid.ShowGroupFooters }); } break; case ExportFormat.Excel: { var workbook = CreateWorkBook(grid); if (workbook != null) { var provider = new XlsxFormatProvider(); using (var output = dialog.OpenFile()) { provider.Export(workbook, output); } } } break; default: { var document = CreateDocument(grid, settings); if (document != null) { document.LayoutMode = DocumentLayoutMode.Paged; document.Measure(RadDocument.MAX_DOCUMENT_SIZE); document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); IDocumentFormatProvider provider = null; switch (settings.Format) { case ExportFormat.Pdf: provider = new PdfFormatProvider(); break; case ExportFormat.Word: provider = new DocxFormatProvider(); break; } using (var output = dialog.OpenFile()) { if (provider != null) provider.Export(document, output); } } } break; } } }
private void ExportToXlsxOnFileSystem(object sender, RoutedEventArgs e) { Workbook target = null; string fileName = null; //export the data from RadGridView Workbook playersDoc = this.playersGrid.ExportToWorkbook(); OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "Workbooks|*.xlsx"; openDialog.Multiselect = false; //load XLSX document from the file system bool? dialogResult = openDialog.ShowDialog(); if (dialogResult == true) { using (Stream stream = openDialog.OpenFile()) { target = new XlsxFormatProvider().Import(stream); fileName = openDialog.FileName; } bool containsSameName = false; //check if target file contains sheet with the same name foreach (var worksheet in target.Worksheets) { if (worksheet.Name == playersDoc.Sheets[0].Name) { containsSameName = true; break; } } if (containsSameName) { //replate the content of the sheet with the exported data from RadGridView target.Worksheets[playersDoc.Sheets[0].Name].CopyFrom(playersDoc.Sheets[0] as Worksheet); } else { //create new sheet and add the exported data from RadGridView to it Worksheet clonedSheet = target.Worksheets.Add(); clonedSheet.CopyFrom(playersDoc.Sheets[0] as Worksheet); clonedSheet.Name = playersDoc.Sheets[0].Name; } //export the combined document back at the same path using (FileStream output = new FileStream(fileName, FileMode.Create)) { new XlsxFormatProvider().Export(target, output); } MessageBox.Show("Check updated file: " + fileName); } }
/// <summary> /// 展示从本地添加的文档 /// <remarks> /// 需要将文档相关内容赋给本地的_currentDocument。 /// </remarks> /// </summary> /// <param name="fi">文件</param> private void ViewDocument(FileInfo fi) { var extension = fi.Extension.ToLower(); var length = (int) fi.Length; var fs = fi.OpenRead(); _content = new byte[length]; using (fs) { fs.Read(_content, 0, length); } switch (extension.ToLower()) { case ".xlsx": PDFVisibility = Visibility.Collapsed; WordVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Visible; IWorkbookFormatProvider provider = new XlsxFormatProvider(); ExcelContent = provider.Import(new MemoryStream(_content)); break; case ".docx": PDFVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Collapsed; WordVisibility = Visibility.Visible; WordContent = _content; break; case ".pdf": WordVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Collapsed; PDFVisibility = Visibility.Visible; PDFContent = new MemoryStream(_content); break; default: MessageAlert("不是合法文档!"); break; } Title = fi.Name; _addedDocument = new DocumentDTO { DocumentId = Guid.NewGuid(), Name = fi.Name, Extension = extension, FileStorage = _content }; }
/// <summary> /// 展示从服务端获取的文档 /// </summary> /// <param name="doc">获取的文档</param> private void ViewDocument(DocumentDTO doc) { var extension = doc.Extension; _content = doc.FileStorage; switch (extension) { case ".xlsx": PDFVisibility = Visibility.Collapsed; WordVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Visible; IWorkbookFormatProvider provider = new XlsxFormatProvider(); ExcelContent = provider.Import(new MemoryStream(_content)); break; case ".docx": PDFVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Collapsed; WordVisibility = Visibility.Visible; WordContent = _content; break; case ".pdf": WordVisibility = Visibility.Collapsed; ExcelVisibility = Visibility.Collapsed; PDFVisibility = Visibility.Visible; PDFContent = new MemoryStream(_content); break; default: MessageAlert("不是合法的文档!"); break; } Title = doc.Name; }
/// <summary> /// Lancement de l'export Excel /// </summary> /// <param name="parameter"></param> public void ExportExcel(object parameter) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "*.xlsx"; dialog.Filter = string.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "xlsx", "Excel"); if (dialog.ShowDialog() == true) { //Récupération du DataPager pour imprimer toute la grid sans la pagination bool paged = ExportAndPrintHelper.GetParameterAtIndex(parameter, ExportAndPrintHelper.RadDataPagerIndex) != null && ExportAndPrintHelper.GetParameterAtIndex(parameter, ExportAndPrintHelper.RadDataPagerIndex) is RadDataPager; RadDataPager pager = paged ? (RadDataPager)ExportAndPrintHelper.GetParameterAtIndex(parameter, ExportAndPrintHelper.RadDataPagerIndex) : null; int originalPageSize = 0; int originalPageIndex = 0; if (paged) { originalPageSize = pager.PageSize; originalPageIndex = pager.PageIndex; pager.PageSize = 0; } try { RadGridView grid = ExportAndPrintHelper.InverseVisibilityColumnsToExport(parameter, ExportAndPrintHelper.ColumnsToExportIndex, ExportAndPrintHelper.RadGridViewIndex); var workbook = ExportAndPrintHelper.CreateWorkBook(grid); if (workbook != null) { XlsxFormatProvider provider = new XlsxFormatProvider(); using (Stream output = dialog.OpenFile()) { provider.Export(workbook, output); } } } catch (OutOfMemoryException ex) { throw ex; } catch (IOException ex) { throw ex; } catch (Exception ex) { throw ex; } finally { if (paged) { pager.PageSize = originalPageSize; pager.PageIndex = originalPageIndex; } ExportAndPrintHelper.InverseVisibilityColumnsToExport(parameter, ExportAndPrintHelper.ColumnsToExportIndex, ExportAndPrintHelper.RadGridViewIndex); } } }
private void ExportToExcel() { this.BusyIndicator.IsBusy = true; SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "xlsx"; dialog.Filter = "Excel Workbook (xlsx) | *.xlsx |All Files (*.*) | *.*"; var result = dialog.ShowDialog(); if ((bool)result) { try { using (var stream = dialog.OpenFile()) { var workbook = GenerateWorkbook(); XlsxFormatProvider provider = new XlsxFormatProvider(); provider.Export(workbook, stream); } } catch (IOException ex) { MessageBox.Show(ex.Message); } } this.BusyIndicator.IsBusy = false; }
private static void SaveWorkbook(Workbook workbook, Stream stream) { XlsxFormatProvider provider = new XlsxFormatProvider(); using (stream) { provider.Export(workbook, stream); } }