private async void GenerateSpreadsheet(object obj) { using (MemoryStream stream = new MemoryStream()) { using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { using (IWorksheetExporter worksheetExporter = workbookExporter.CreateWorksheetExporter("Courses")) { ExportColumnWidths(worksheetExporter); ExportDocumentTitleRow(worksheetExporter); ExportDocumentHeaderRow(worksheetExporter); ExportData(worksheetExporter); worksheetExporter.MergeCells(0, 0, 0, 2); } } bool success = await DependencyService.Get <IFileViewerService>().View(stream, "my_courses.xlsx"); if (!success) { MessagingCenter.Send(this, Messages.CreatingFileFailed); } } }
private static void AddWorksheetToExistingDocument(string filePath) { using (FileStream stream = File.Open(filePath, FileMode.Open)) { // Pass SpreadExportMode.Append parameter, and the created workbook exporter will preserve all of the existing worksheets. using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream, SpreadExportMode.Append)) { string sheetName = "Sheet name here"; var importedSheetsNames = workbook.GetSheetInfos().Select(sheetInfo => sheetInfo.Name); if (importedSheetsNames.Contains(sheetName)) { Console.WriteLine("Sheet with that name already exists in the workbook."); return; } using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter(sheetName)) { using (IRowExporter row = worksheet.CreateRowExporter()) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("value 1"); cell.SetValue("value 2"); cell.SetValue("value 3"); } } } } } Console.WriteLine("Document modified."); Process.Start(filePath); }
private static void GenerateDocument(string filePath, List <CsvGlucoseMeasure> output) { using (FileStream stream = File.Open(filePath, FileMode.OpenOrCreate)) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Csv, stream)) { // Creating a style which would be used later in the code. //SpreadCellStyle style = workbook.CellStyles.Add("MyStyle"); //style.Underline = SpreadUnderlineType.None; //style.VerticalAlignment = SpreadVerticalAlignment.Center; using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Data")) { //Entête de colonne using (IRowExporter row = worksheet.CreateRowExporter()) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Date"); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Taux de glucose"); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Dans de la zone de confort"); } } //Contenu foreach (var data in output) { using (IRowExporter row = worksheet.CreateRowExporter()) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(data.RealDateTimeOffset); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(data.GlucoseLevelMGDL); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(data.InTheMedicalZone == true ? "Oui" : "Non"); } } } } } } App.Current.MainPage.DisplayAlert("Export réussi !", "Fichier exporté avec succès vers le répertoire de téléchargements.", "OK"); }
private static void GenerateDocument(string filePath) { using (FileStream stream = File.OpenWrite(filePath)) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("My sheet")) { worksheet.SkipColumns(1); using (IColumnExporter column = worksheet.CreateColumnExporter()) { column.SetWidthInPixels(80); } worksheet.SkipRows(3); using (IRowExporter row = worksheet.CreateRowExporter()) { row.SkipCells(3); using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Merged cell."); cell.SetFormat(new SpreadCellFormat() { HorizontalAlignment = SpreadHorizontalAlignment.Center, VerticalAlignment = SpreadVerticalAlignment.Center }); } } using (IRowExporter row = worksheet.CreateRowExporter()) { row.SetHeightInPixels(200); using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(123.456); } using (ICellExporter cell = row.CreateCellExporter()) { SpreadCellFormat format = new SpreadCellFormat() { NumberFormat = "dd/mm/yyyy", IsBold = true }; cell.SetFormat(format); cell.SetValue(42370); } } worksheet.MergeCells(3, 3, 6, 6); } } } }
private async void GenerateDocument() { SpreadCellFormat format = new SpreadCellFormat(); format.FontFamily = new SpreadThemableFontFamily(this.FontFamiliesItemsSource[this.SelectedFontFamilyIndex]); format.FontSize = int.Parse(this.FontSizesItemsSource[this.SelectedFontSizeIndex]); format.IsBold = this.IsBold; format.IsItalic = this.IsItalic; PredefinedColors selectedFillColor = (PredefinedColors)this.SelectedFillColorIndex; PredefinedColors selectedTextColor = (PredefinedColors)this.SelectedTextColorIndex; if (selectedFillColor != PredefinedColors.NoColor) { format.Fill = SpreadPatternFill.CreateSolidFill(ToSpreadColor(selectedFillColor)); } if (selectedTextColor != PredefinedColors.NoColor) { format.ForeColor = ToSpreadColor(selectedTextColor); } format.Underline = (SpreadUnderlineType)this.SelectedUnderlineIndex; using (MemoryStream stream = new MemoryStream()) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet1")) { using (IRowExporter row = worksheet.CreateRowExporter()) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(this.FirstCellValue ?? "Sample text"); cell.SetFormat(format); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetFormula(this.SecondCellValue ?? "=1+2"); cell.SetFormat(format); } } } } await DependencyService.Get <IXlsxFileViewer>().View(stream, "GettingStarted.xlsx"); } }
private byte[] GenerateDocument() { MemoryStream documentStream = new MemoryStream(); var documentFormat = _format is FileFormats.Formats.XLSX ? SpreadDocumentFormat.Xlsx : SpreadDocumentFormat.Csv; using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(documentFormat, documentStream)) { using IWorksheetExporter worksheetExporter = workbookExporter.CreateWorksheetExporter("Sheet Name"); //Export data to excel ExportGrid(worksheetExporter, _data, false); } return(documentStream.ToArray()); }
public byte[] Export(IEnumerable <Attendee> source) { MemoryStream stream = new MemoryStream(); double[] columnWidths = new double[] { 4.22, 28.56, 16.11, 17.22, 39.11, 11.67, 9.56, 14.22 }; using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { SpreadCellStyle titleStyle = workbookExporter.CellStyles["Heading 1"]; SpreadCellFormat titleFormat = new SpreadCellFormat() { CellStyle = titleStyle }; SpreadCellStyle rowStyle = workbookExporter.CellStyles.Where(p => p.Name.Contains("Accent1")).FirstOrDefault(); SpreadCellFormat evenRowFormat = new SpreadCellFormat() { CellStyle = rowStyle }; using (IWorksheetExporter worksheetExporter = workbookExporter.CreateWorksheetExporter("Attendee information")) { for (int i = 0; i < columnWidths.Length; i++) { using (IColumnExporter columnExporter = worksheetExporter.CreateColumnExporter()) { columnExporter.SetWidthInCharacters(columnWidths[i]); } } ExportRow(worksheetExporter, "Id", "CompanyName", "ContactName", "ContactTitle", "Address", "Country", "Phone", "City", titleFormat); int rowIndex = 1; foreach (Attendee attendee in source.Take(10000)) { SpreadCellFormat currentRowFormat = null; if (rowIndex++ % 2 == 0) { currentRowFormat = evenRowFormat; } ExportRow(worksheetExporter, attendee.Id.ToString(), attendee.CompanyName, attendee.ContactName, attendee.ContactTitle, attendee.Address, attendee.Country, attendee.Phone, attendee.City, currentRowFormat); } } } return(stream.ToArray()); }
public JsonResult Export(string model, string data, string format, string title) { var modelObject = JsonConvert.DeserializeObject <IList <ColumnSettings> >(model); var dataObject = JsonConvert.DeserializeObject <dynamic>(data); SpreadDocumentFormat exportFormat = format == "CSV" ? SpreadDocumentFormat.Csv : SpreadDocumentFormat.Xlsx; using (MemoryStream stream = new MemoryStream()) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(exportFormat, stream)) { using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter(title)) { using (IRowExporter row = worksheet.CreateRowExporter()) { for (int idx = 0; idx < modelObject.Count; idx++) { var modelCol = modelObject[idx]; string columnName = modelCol.Title ?? modelCol.Field; using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(columnName); } } } for (int rowIdx = 0; rowIdx < dataObject.Count; rowIdx++) { using (IRowExporter row = worksheet.CreateRowExporter()) { for (int colIdx = 0; colIdx < modelObject.Count; colIdx++) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(dataObject[rowIdx][modelObject[colIdx].Field].Value); } } } } } } Session[title] = stream.ToArray(); } return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
private void ExportWorkbook(RadGridView grid, Stream stream) { IList <GridViewBoundColumnBase> columns = (from c in grid.Columns.OfType <GridViewBoundColumnBase>() orderby c.DisplayIndex select c).ToList(); using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(this.selectedExportFormat, stream)) { using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet1")) { this.SetWidthOfColumns(worksheet, grid.GroupDescriptors.Count, columns); int rowIndex = 0; if (grid.ShowColumnHeaders) { this.AddHeaderRow(worksheet, grid.GroupDescriptors.Count, columns); rowIndex = 1; } if (grid.Items.Groups != null) { for (int i = 0; i < grid.Items.Groups.Count; i++) { QueryableCollectionViewGroup group = (QueryableCollectionViewGroup)grid.Items.Groups[i]; rowIndex = this.AddGroupRow(worksheet, 1, rowIndex, grid.GroupDescriptors.Count, group, columns); } } else { this.AddDataRows(worksheet, 0, 0, grid.Items, columns); } foreach (CellRange range in this.mergedCells) { worksheet.MergeCells(range.FromRowIndex, range.FromColumnIndex, range.ToRowIndex, range.ToColumnIndex); } } } }
private static void GenerateDocument(string filePath) { using (FileStream stream = File.OpenWrite(filePath)) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { // Creating a style which would be used later in the code. SpreadCellStyle style = workbook.CellStyles.Add("MyStyle"); style.Underline = SpreadUnderlineType.DoubleAccounting; style.VerticalAlignment = SpreadVerticalAlignment.Top; using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("My sheet")) { // It is mandatory to export the worksheet view state before filling the worksheet with data. using (IWorksheetViewExporter worksheetView = worksheet.CreateWorksheetViewExporter()) { worksheetView.SetFirstVisibleCell(3, 0); worksheetView.AddSelectionRange(9, 0, 13, 6); worksheetView.SetActiveSelectionCell(11, 3); } // It is mandatory to export the column setting before exporting the row and cell data. worksheet.SkipColumns(1); using (IColumnExporter column = worksheet.CreateColumnExporter()) { column.SetWidthInPixels(80); } worksheet.SkipRows(3); using (IRowExporter row = worksheet.CreateRowExporter()) { row.SkipCells(3); using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Merged cell."); cell.SetFormat(new SpreadCellFormat() { CellStyle = style, HorizontalAlignment = SpreadHorizontalAlignment.Center, VerticalAlignment = SpreadVerticalAlignment.Center }); } } using (IRowExporter row = worksheet.CreateRowExporter()) { row.SetHeightInPixels(200); using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(123.456); } using (ICellExporter cell = row.CreateCellExporter()) { SpreadCellFormat format = new SpreadCellFormat() { NumberFormat = "dd/mm/yyyy", IsBold = true }; cell.SetFormat(format); cell.SetValue(42370); } } worksheet.MergeCells(3, 3, 6, 6); } } } Console.WriteLine("Document generated."); ProcessStartInfo psi = new ProcessStartInfo() { FileName = filePath, UseShellExecute = true }; Process.Start(psi); }
private async void GenerateDocument() { var maxTitleCharCount = this.Source.Max(p => p.Title.Length); var maxAuthorCharCount = this.Source.Max(p => p.Author.Length); using (MemoryStream stream = new MemoryStream()) { using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, stream)) { using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet1")) { using (IWorksheetViewExporter viewExporter = worksheet.CreateWorksheetViewExporter()) { // just moving the selection so the bottom border of the header row is visible viewExporter.AddSelectionRange(0, 3, 0, 3); } using (IColumnExporter titleColumn = worksheet.CreateColumnExporter()) { titleColumn.SetWidthInCharacters(maxTitleCharCount); } using (IColumnExporter authorColumn = worksheet.CreateColumnExporter()) { authorColumn.SetWidthInCharacters(maxAuthorCharCount); } using (IRowExporter row = worksheet.CreateRowExporter()) { SpreadCellFormat headerformat = new SpreadCellFormat(); headerformat.CellStyle = workbook.CellStyles["Heading 1"]; headerformat.HorizontalAlignment = SpreadHorizontalAlignment.Center; using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Books"); cell.SetFormat(headerformat); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetFormat(headerformat); } } using (IRowExporter row = worksheet.CreateRowExporter()) { SpreadCellFormat subHeaderformat = new SpreadCellFormat(); subHeaderformat.CellStyle = workbook.CellStyles["Heading 2"]; using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Title"); cell.SetFormat(subHeaderformat); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue("Author"); cell.SetFormat(subHeaderformat); } } for (int i = 0; i < this.Source.Count; i++) { Book book = this.Source[i]; string styleName = i % 2 == 0 ? "20% - Accent1" : "20% - Accent2"; SpreadCellFormat format = new SpreadCellFormat(); format.CellStyle = workbook.CellStyles[styleName]; using (IRowExporter row = worksheet.CreateRowExporter()) { using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(book.Title); cell.SetFormat(format); } using (ICellExporter cell = row.CreateCellExporter()) { cell.SetValue(book.Author); cell.SetFormat(format); } } } worksheet.MergeCells(0, 0, 0, 1); } } await DependencyService.Get <IXlsxFileViewer>().View(stream, "GettingStarted.xlsx"); } }
/// <summary> /// Crea via stream un documento Excel (csv non previsto per via di fogli multipli) con tutte le tabelle economiche di una valutazione /// </summary> /// <param name="Eval">Dati valutazione (definizioni, tabelle, ammissioni, importi)</param> /// <param name="documentStream">Stream output</param> /// <param name="DocFormat">Formato - Solo XLSX</param> /// <param name="settings">Impostazioni di esportazione (caratteri, larghezze, colori) - Per personalizzazioni future.</param> public static void EcoEvalTableExportStream( Eco.Domain.EconomicEvaluation Eval, Stream documentStream, SpreadDocumentFormat DocFormat = SpreadDocumentFormat.Xlsx, dto.dtoEcoTableExportSettings settings = null) { if (settings == null) { settings = new dto.dtoEcoTableExportSettings(); } int exportedCellsCount = 0; SpreadDocumentFormat selectedDocumentFormat; int totalCellsCount; DateTime exportStarted; bool canExport; if (Eval == null || Eval.Tables == null || !Eval.Tables.Any()) { return; } using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(DocFormat, documentStream)) { int SheetNumber = 0; foreach (Eco.Domain.EconomicTable table in Eval.Tables) { if (table != null && table.FieldDefinition != null) { SheetNumber++; int headCols = table.HeaderValues.Count(); int totalCols = headCols + 7; string sheetName = string.Format("{0}-{1}", SheetNumber, table.FieldDefinition.Name); if (sheetName.Length > 25) { sheetName = string.Format("{0}...", sheetName.Substring(0, 20)); } sheetName = CleanFileName(sheetName); using (IWorksheetExporter worksheetExporter = workbookExporter.CreateWorksheetExporter(sheetName)) { for (int i = 0; i < totalCols; i++) { using (IColumnExporter columnExporter = worksheetExporter.CreateColumnExporter()) { if (i >= headCols) { columnExporter.SetWidthInCharacters(settings.ColumnWidths[i - headCols]); } else { columnExporter.SetWidthInCharacters(settings.ColumnAddWidth); } } } ExportHeaderRows(worksheetExporter, table.HeaderValues, settings, table.FieldDefinition.Name); foreach (AdvEconomic.Domain.EconomicItem itm in table.Items) { using (IRowExporter rowExporter = worksheetExporter.CreateRowExporter()) { rowExporter.SetHeightInPoints(settings.RowHeight); int columnIndex = 0; int iv = 0; foreach (string value in itm.InfoValues) { iv++; if (iv <= headCols) { using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.NormalFormat, itm.IsAdmit)); cellExporter.SetValue(value.Replace(" ", " ")); } } } if (iv < headCols) { using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.NormalFormat, itm.IsAdmit)); cellExporter.SetValue(""); } } //Quantità using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.DoubleFormat, itm.IsAdmit)); cellExporter.SetValue(itm.RequestQuantity); } //Prezzo unitario using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.DoubleEcoFormat, itm.IsAdmit)); cellExporter.SetValue(itm.RequestUnitPrice); } //Totale richiesto using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.DoubleEcoFormat, itm.IsAdmit)); cellExporter.SetValue(itm.RequestTotal); } //Approvato using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.NormalFormat, itm.IsAdmit)); cellExporter.SetValue(itm.IsAdmit ? settings.BoolValue[1] : settings.BoolValue[0]); } //Quantità ammessa using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.DoubleFormat, itm.IsAdmit)); cellExporter.SetValue(itm.AdmitQuantity); } //Totale ammesso using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.DoubleEcoFormat, itm.IsAdmit)); cellExporter.SetValue(itm.AdmitTotal); } //Commenti using (ICellExporter cellExporter = rowExporter.CreateCellExporter()) { cellExporter.SetFormat(dto.dtoEcoTableExportSettings.InvalidCellFormat(settings.NormalFormat, itm.IsAdmit)); if (itm.Comment != null) { cellExporter.SetValue(itm.Comment.Replace(" ", " ").Replace("<br>", "\r\n")); } else { cellExporter.SetValue(""); } } } } } } } } }