public ExampleViewModel() { this.Products = new Products().GetData(100000).ToList(); this.SelectedExportFormat = this.ExportFormats.First(); this.ExportCommand = new DelegateCommand(this.Export); this.HeaderRowColor = DefaultHeaderRowColor; this.DataRowColor = DefaultDataRowColor; this.GroupHeaderRowColor = DefaultGroupHeaderRowColor; }
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)); }
/// <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(""); } } } } } } } } }
public ExampleViewModel() { this.Products = new Products().GetData(100000).ToList(); this.SelectedExportFormat = this.ExportFormats.First(); this.ExportCommand = new DelegateCommand(this.Export); this.HeaderRowColor = DefaultHeaderRowColor; this.DataRowColor = DefaultDataRowColor; this.GroupHeaderRowColor = DefaultGroupHeaderRowColor; }