Asp.net MVC後臺代碼 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage(); ...
Asp.net MVC後臺代碼
public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage(); OfficeOpenXml.ExcelWorkbook wb = ep.Workbook; OfficeOpenXml.ExcelWorksheet ws = wb.Worksheets.Add("我的工作表"); ////配置文件屬性 //wb.Properties.Category = "類別"; //wb.Properties.Author = "作者"; //wb.Properties.Comments = "備註"; //wb.Properties.Company = "公司"; //wb.Properties.Keywords = "關鍵字"; //wb.Properties.Manager = "管理者"; //wb.Properties.Status = "內容狀態"; //wb.Properties.Subject = "主題"; //wb.Properties.Title = "標題"; //wb.Properties.LastModifiedBy = "最後一次保存者"; // var list = GetList(); int x = 0; for (int i = 0; i < 300000; i++) { //if (x == 1000000) //{ // ws = wb.Worksheets.Add("我的工作表" + Guid.NewGuid().ToString()); // x = 0; //} for (int j = 1; j <= 9; j++) { ws.Cells[(x + 1), j].Value = DateTime.Now.ToString(); } x++; } //寫數據 //ws.Cells[1, 1].Value = "Hello"; //ws.Cells[1, 1].Style.Numberformat.Format = "yyyy-MM-dd"; //ws.Column(1).Width = 40;//修改列寬 //ws.Cells["B1"].Value = "World"; //ws.Cells[3, 3, 3, 5].Merge = true; //ws.Cells[3, 3].Value = "Cells[3, 3, 3, 5]合併"; //ws.Cells["A4:D5"].Merge = true; //ws.Cells["A4:D5"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//居中 //ws.Cells["A4"].Value = "Cells[\"A4:D5\"]合併"; //寫到客戶端(下載) Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=FileFlow.xlsx"); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; byte[] data=ep.GetAsByteArray(); Response.AddHeader("Content-Length", data.Length.ToString()); Response.BinaryWrite(data); //ep.SaveAs(Response.OutputStream); 第二種方式 Response.Flush(); Response.End(); return null; }
FileInfo newFile = new FileInfo(@"d:\test.xlsx"); if (newFile.Exists) { newFile.Delete(); newFile = new FileInfo(@"d:\test.xlsx"); } //using (ExcelPackage package = new ExcelPackage(newFile)) //{ // ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); // worksheet.Cells[1, 1].Value = "名稱"; // worksheet.Cells[1, 2].Value = "價格"; // worksheet.Cells[1, 3].Value = "銷量"; // worksheet.Cells[2, 1].Value = "大米"; // worksheet.Cells[2, 2].Value = 56; // worksheet.Cells[2, 3].Value = 100; // worksheet.Cells[3, 1].Value = "玉米"; // worksheet.Cells[3, 2].Value = 45; // worksheet.Cells[3, 3].Value = 150; // worksheet.Cells[4, 1].Value = "小米"; // worksheet.Cells[4, 2].Value = 38; // worksheet.Cells[4, 3].Value = 130; // worksheet.Cells[5, 1].Value = "糯米"; // worksheet.Cells[5, 2].Value = 22; // worksheet.Cells[5, 3].Value = 200; // package.Save(); //} OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage(newFile); OfficeOpenXml.ExcelWorkbook wb = ep.Workbook; OfficeOpenXml.ExcelWorksheet ws = wb.Worksheets.Add("我的工作表"); //配置文件屬性 //wb.Properties.Category = "類別"; //wb.Properties.Author = "作者"; //wb.Properties.Comments = "備註"; //wb.Properties.Company = "公司"; //wb.Properties.Keywords = "關鍵字"; //wb.Properties.Manager = "管理者"; //wb.Properties.Status = "內容狀態"; //wb.Properties.Subject = "主題"; //wb.Properties.Title = "標題"; //wb.Properties.LastModifiedBy = "最後一次保存者"; // var list = GetList(); int x = 0; for (int i = 0; i < 100; i++) { if (x == 1000000) { ws = wb.Worksheets.Add("我的工作表" + Guid.NewGuid().ToString()); x = 0; } for (int j = 1; j <= 2; j++) { ws.Cells[(x + 1), j].Value = DateTime.Now.ToString(); Console.WriteLine((i+1)); } x++; } ep.Save();
Client的版本親測至少可導出千萬級別的數據,Web版本要看伺服器記憶體配置。。
epplus下載路徑: http://epplus.codeplex.com/