[英]OOxml or Office OpenXML C# - export from DataTable to excel boolean data type causes repair excel
[英]How to export data from C# from List<object> or IEnumerable to Excel (MS OpenXml)?
我正在嘗試使用 Microsoft 的 Open XML 庫將List<dynamic>
或IEnumerable
導出到 Excel 文件。
庫: https://github.com/OfficeDev/Open-XML-SDK
類似請求的示例: https://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie
上面的例子工作得很好,但之間有很多代碼(到目前為止工作很整潔,但我正在尋找優化)
但是這個庫的文檔沒有擴展(或者沒有很多例子),我看到的例子是關於如何將數據導出到 Excel 它使用DataTable
或Dataset
; 因此,他們進行從List
到DataTable
的對話以導出DataTable
。 對於其他庫更容易解決的問題,這似乎相當復雜。
因此,如果有人有一個關於如何快速和通用導出的示例,我們將不勝感激。
僅供參考,這是我的實現,使用@mikesknowledgebase 的代碼( http://www.mikesknowledgebase.com - 網頁不起作用......但至少給予學分:D)
在此處發布所有信息
所以,我將它用於.Net Core 2.2; 目的是使用該方法將List<dynamic>
導出到Excel
。
最終結果(在最簡單的例子中):
[HttpGet("[action]")]
public FileResult exportExample()
{
List<dynamic> data = new List<dynamic>();
data.Add(new { a = 1, b = "HELLO WORLD", c = DateTime.Now });
data.Add(new { a = 2, b = "TEST", c = 34 });
// Implementation of Dictionary to limit the columns and the type of the List
// Works with some standard, and flat files (Not with Dynamic Objects that have multiples levels - Indentation of a dynamic into a property)
Dictionary<string, Type> columns = new Dictionary<string, Type>();
columns.Add("a", typeof(int));
columns.Add("b", typeof(string));
columns.Add("c", typeof(object)); // Accepts any (Numbers or DateTime)
string excelContentType;
var excelStream = CreateExcelFile.CreateExcelStream(data, columns, out excelContentType);
return File(excelStream, excelContentType, "report.xlsx");
}
我在 Mike 的 class 中創建了一些其他方法...
使用Dictionary<string, type>
為列獲取List
的方法...以及另一個返回ContentType
的參數:
public static byte[] CreateExcelStream<T>(List<T> list, Dictionary<string, Type> columns, out string contentType )
{
DataSet ds = new DataSet();
ds.Tables.Add(ListToDataTable(list, columns));
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // "application/vnd.ms-excel";
return CreateExcelStream(ds).fwToByteArray();
}
將List
轉換為DataTable
的另一種方法:
public static DataTable ListToDataTable<dynamic>(List<dynamic> list, Dictionary<string, Type> columns)
{
DataTable dt = new DataTable();
foreach (var column in columns)
dt.Columns.Add(new DataColumn(column.Key, GetNullableType(column.Value)));
foreach (var t in list)
{
DataRow row = dt.NewRow();
((object)t)
.GetType()
.GetProperties()
.ToList()
.ForEach(p =>
{
if (!IsNullableType(p.PropertyType))
row[p.Name] = p.GetValue(t, null);
else
row[p.Name] = (p.GetValue(t, null) ?? DBNull.Value);
});
dt.Rows.Add(row);
}
return dt;
}
我包含了一個擴展來將stream
轉換為byteArray
:
public static byte[] fwToByteArray(this Stream stream)
{
stream.Position = 0;
byte[] buffer = new byte[stream.Length];
for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;)
totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
return buffer;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.