以下是我拥有的代码,在下面的最终方法中,我完成了所有操作。请忽略方法的返回类型,我后来改了。
public static byte[] CreateExcelDocument<T>(List<T> list, string filename)
{
DataSet ds = new DataSet();
ds.Tables.Add(ListToDataTable(list));
byte[] byteArray = CreateExcelDocumentAsStream(ds, filename);
return byteArray;
}
public static bool CreateExcelDocumentAsStream(DataSet ds, string filename, System.Web.HttpResponse Response)
{
try
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
{
WriteExcelFile(ds, document);
}
stream.Flush();
stream.Position = 0;
Response.ClearContent();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
// NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure …Run Code Online (Sandbox Code Playgroud)