我一直堅持這幾天,儘管有所有的幫助,但這些解決方案都沒有爲我工作。我想要做的是使用EPPlus庫創建一個Excel文件,其中包含我從存儲過程中提取的一些基本數據。這是我在我的ExportDocument.cs文件中的代碼:在EPPlus中編寫一個Excel文件
public static ExcelPackage CreateExcelDocument(int [] arr)
{
String path = @"D:\temp\testsheet3.xlsx";
//FileInfo newFile = null;
/*if (!File.Exists(path + "\\testsheet2.xlsx"))
newFile = new FileInfo(path + "\\testsheet2.xlsx");
else
return newFile;*/
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("testsheet");
ws.Cells["B1"].Value = "Number of Used Agencies";
ws.Cells["C1"].Value = "Active Agencies";
ws.Cells["D1"].Value = "Inactive Agencies";
ws.Cells["E1"].Value = "Total Hours Volunteered";
ws.Cells["B1:E1"].Style.Font.Bold = true;
int x = 2;
char pos = 'B';
foreach (object o in arr)
{
String str = pos + x.ToString();
ws.Cells[str].Value = o.ToString();
if (pos > 'E')
{
pos = 'B';
x++;
}
pos++;
}
//newFile.Create();
//newFile.MoveTo(@"C:/testSheet.xlsx");
//package.SaveAs(newFile);
package.Save();
/*Stream stream = File.Create(path);
package.SaveAs(stream);
stream.Close();*/
//byte[] data = File.ReadAllBytes(path);
//byte[] bin = package.GetAsByteArray();
//String path = Path.GetTempPath();
//File.WriteAllBytes(path, bin);
//File.WriteAllBytes(path + "testsheet.xlsx", bin);
//HttpContext.Current.Response.Write("<script>alert('"+ temp + "');</script>");
//System.Diagnostics.Process.Start(path + "\\testsheet.xlsx");
return package;
}
}
所有註釋代碼是,我發現在互聯網上嘗試不同的東西。請注意,這是一個學校組織,我們沒有使用MVC。我然後使用代碼隱藏文件來拉這個方法是這樣的:
protected void GenerateReport(Object o, EventArgs e)
{
//NamingContainer.FindControl("ReportsControl").Visible = false;
//NamingContainer.FindControl("ReportsGenerateControl").Visible = true;
Session["reportSession"] = ddReport.SelectedItem.Value.ToString();
int [] arr = new int [ReportRepository.GetAgencyCounts().Count];
ReportRepository.GetAgencyCounts().CopyTo(arr, 0);
ExcelPackage pck = ExportDocument.CreateExcelDocument(arr);
/*try
{
byte [] data = ExportDocument.CreateExcelDocument(arr).GetAsByteArray();
Response.Clear();
Response.Buffer = true;
Response.BinaryWrite(data);
Response.AddHeader("content-length", data.Length.ToString());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Flush();
Response.Close();
Response.End();
}
catch (Exception ex) { }*/
/*var stream = new MemoryStream();
pck.SaveAs(stream);
String filename = "myfile.xlsx";
String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var cd = new System.Net.Mime.ContentDisposition
{
Inline = false,
FileName = filename
};
Response.AppendHeader("Content-Disposition", cd.ToString());
stream.Position = 0;
return File(stream, contentType, filename);*/
/*Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.TransmitFile(Path.GetFullPath(file.Name));
Response.Flush();
Response.Close();*/
/*Response.ClearHeaders();
Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Sample2.xlsx");
Response.Flush();
Response.Close();*/
}
再次注意,所有的註釋代碼是我從沒有工作過各種來源找到的東西。
所以我沒有收到任何錯誤,但是當我點擊應用程序中的按鈕來執行代碼後面的代碼時,什麼都沒有發生。它正在加載並運行,但沒有創建文件,沒有任何開放。這是我第一次使用EPPlus,而且我並不完全熟悉以編程的方式導出出色的東西,所以我覺得迷失在這裏。
你們有什麼建議嗎?我很樂意澄清我還沒有完全達成的任何觀點。
你已經聲明瞭一個'path',但我沒有看到它在代碼中使用(忽略註釋的位)。你如何告訴excelpackage將其保存到某個位置? – 2013-03-27 03:19:08
當前路徑變量未被使用。現在寫的方式是,ExcelPackage包對象正在使用其保存方法,然後我在使用Response.BinaryWrite(byte [] arr)將其寫出的代碼隱藏文件中調用該ExcelPackage。這是我試過的最後一種方法。使用它,不需要路徑。 – Rahlord 2013-03-27 03:37:53
此外,我還嘗試將文件保存到路徑,然後從那裏打開它,但我最終得到了我現在遇到的相同問題。該文件從來沒有創建,我的代碼沒有彈出或錯誤 – Rahlord 2013-03-27 03:38:36