qualcuno sa come o qualche libreria da utilizzare per questo?esportazione IEnumerable <T> in Excel
risposta
Date un'occhiata qui per la parte libreria:
Il codice sotto è il codice che utilizzo per convertire un IEnumerable<T>
ad un DataTable contenente colonne chiamate per ogni proprietà, insieme con i valori di ciascun elemento nel IEnumerable come righe. E 'un piccolo salto da questo a salvarla come file CSV
public class IEnumerableToDataTable
{
public static DataTable CreateDataTableForPropertiesOfType<T>()
{
DataTable dt = new DataTable();
PropertyInfo[] piT = typeof(T).GetProperties();
foreach (PropertyInfo pi in piT)
{
Type propertyType = null;
if (pi.PropertyType.IsGenericType)
{
propertyType = pi.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = pi.PropertyType;
}
DataColumn dc = new DataColumn(pi.Name, propertyType);
if (pi.CanRead)
{
dt.Columns.Add(dc);
}
}
return dt;
}
public static DataTable ToDataTable<T>(IEnumerable<T> items)
{
var table = CreateDataTableForPropertiesOfType<T>();
PropertyInfo[] piT = typeof(T).GetProperties();
foreach (var item in items)
{
var dr = table.NewRow();
for (int property = 0; property < table.Columns.Count; property++)
{
if (piT[property].CanRead)
{
dr[property] = piT[property].GetValue(item, null);
}
}
table.Rows.Add(dr);
}
return table;
}
}
Quindi, si potrebbe scrivere il seguente per convertire i dati in un DataTable:
IEnumerable<Thing> values = GetMyIEnumerableValues();
var tableOfData = IEnumerableToDataTable.ToDataTable(values);
Una volta che è lì, è abbastanza banale per scriverlo in un file CSV o in qualsiasi altro formato di tua scelta, poiché è stata eseguita tutta la riflessione "faticosa" per estrarre i dati dallo IEnumerable<T>
.
ho risolto un problema simile, come hai chiesto. Come avete un IEnumerable
IEnumerable<TaskSheetHead> taskSheetHead = new TaskSheetHeadService().GetEmployeeTimesheet(entity);
Quindi, se si vuole fare un Excel da questo basta creare una cartella di lavoro
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet();
E iterare con un looping e poi aggiungerlo al foglio di lavoro
foreach (TaskSheetHead taskSheethead in taskSheetHead)
{
//decimal totalTask = decimal.Zero;
//double totalTime = 0;
foreach (TaskSheet taskSheet in taskSheethead.TaskSheets)
{
var row2 = sheet.CreateRow(rowNumber++);
row2.CreateCell(0).SetCellValue(taskSheethead.Date.ToString("MMMM dd, yyyy"));
row2.CreateCell(1).SetCellValue(taskSheet.Task.Project.ProjectName + ":" + taskSheet.Task.Title + "-" + taskSheet.Comment);
row2.CreateCell(2).SetCellValue(taskSheet.ExpendedHour);
row2.CreateCell(3).SetCellValue(taskSheet.IsBilledToClient);
//totalTask += 1;
//totalTime += taskSheet.ExpendedHour;
}
var row3 = sheet.CreateRow(rowNumber++);
row3.CreateCell(0).SetCellValue("");
row3.CreateCell(1).SetCellValue("");
row3.CreateCell(2).SetCellValue("");
row3.CreateCell(3).SetCellValue("");
}
Qui ho un altro elenco di bambini, quindi ho iterato utilizzando un ciclo interno. Fai come la tua scelta.
- 1. esportazione in Excel C#
- 2. Esportazione da PHP a Excel
- 3. IEnumerable <IEnumerable <T>> per IEnumerable <T> utilizzando LINQ
- 4. T-SQL: esportazione in nuovo file Excel
- 5. php esportazione in Excel non mostrare griglia
- 6. JavaScript - esportazione dati tabella HTML in Excel
- 7. Esportazione di un ListView in formato Excel
- 8. problemi RadGrid Telerik con esportazione in Excel
- 9. Come utilizzare l'espressione del generatore CMake $ <TARGET_FILE: tgt>?
- 10. Flatten IEnumerable <IEnumerable <>>; comprensione generici
- 11. IEnumerable <> to IList <>
- 12. Esportazione Excel file da visualizzare (MVC)
- 13. ASP.NET Problema di codifica esportazione Excel
- 14. Perché IEnumerable <T> è definito come IEnumerable <out T>, non IEnumerable <T>
- 15. Conversione da IEnumerable <IEnumerable <string>> per ICollection <T>
- 16. esportazione dati da excel a database sqlite
- 17. IEnumerable <T> in C#
- 18. inizializzazione IEnumerable <string> In C#
- 19. IEnumerable <CustomType> in PowerShell
- 20. Converti IEnumerable <int> in int []
- 21. Come concatenare due IEnumerable <T> in un nuovo IEnumerable <T>?
- 22. IEnumerable <T> conversione
- 23. dati Esportazione in cellule specifiche in foglio di Excel
- 24. Trasmetti da IEnumerable a IEnumerable <object>
- 25. Differenza tra IEnumerable e IEnumerable <T>?
- 26. jQuery DataTables: esportazione di singole colonne in Excel
- 27. PHPExcel esportazione "Contenuto illeggibile" File Excel in yii2
- 28. Esportazione dei dati dei difetti QC HP in Excel
- 29. esportazione in Excel, pdf e doc utilizzando Ruby on Rails
- 30. Esportazione di dati MySQL in Excel/CSV tramite php
ne avete bisogno per essere esportati al volo? in caso contrario, prendere in considerazione la possibilità di salvare in formato csv – ULysses
Esportare in un file Excel o CSV essere accettabile? – Rob
Questo tipo dipende dalla T :) – SWeko