0
當我在本地機器上使用此類時,它會提示用戶打開xlsx文件,但是當我發佈到Azure時,它不再有效。我很確定我的問題是,我沒有在Azure上處理文件流。希望有人能幫助我。C#不知道如何處理System.Web.HttpResponse
,當我在本地發佈的作品的代碼 -
if (vm.ExportClicked != null && vm.ExportClicked.ToLower() == "true")
{
//doing this because I need the entire list not just a single page
CustomerLastOrderListViewModel vm1 = new CustomerLastOrderListViewModel();
var oPagedCustomerLists = oFilteredCustomerList.OrderByDescending(c => c.Id);
//.ToList();
List<CustomerLastOrderViewModel> customersLastOrders = CombineWithLastOrder(oPagedCustomerLists.ToList());
vm1.CustomersLastOrder = customersLastOrders;
DataTable dt = CreateCustomerEmailData(vm1, form);
CreateExcelFile.CreateExcelDocument(dt, "CustomerEmailList.xlsx", HttpContext.ApplicationInstance.Response);
我使用的這種反應,因爲我不能讓它與別的編譯。 Excel類在文件流中返回信息,我想知道,因爲我在Azure上,並且沒有安裝Excel,所以我必須做一些不同的事情。
這是CreateExcelDocument -
public static bool CreateExcelDocument(DataTable dt, string filename, System.Web.HttpResponse Response)
{
try
{
DataSet ds = new DataSet();
ds.Tables.Add(dt);
CreateExcelDocumentAsStream(ds, filename, Response);
ds.Tables.Remove(dt);
return true;
}
catch (Exception ex)
{
Trace.WriteLine("Failed, exception thrown: " + ex.Message);
return false;
}
}
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 you have
// manually added System.Web to this project's References.
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
byte[] data1 = new byte[stream.Length];
stream.Read(data1, 0, data1.Length);
stream.Close();
Response.BinaryWrite(data1);
Response.Flush();
Response.End();
return true;
}
catch (Exception ex)
{
Trace.WriteLine("Failed, exception thrown: " + ex.Message);
return false;
}
}
此代碼是客戶端還是服務器端?我有點困惑...還有哪裏來了這個'CreateExcelDocumentAsStream'方法來自哪裏?它似乎不是.NET – evanmcdonnal
服務器端的一部分。CreateExeclDoctumentAsStream是excel類的一部分。將添加上面。 – Craig