Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Excel Document using EPPlus

Tags:

I am trying to open an Excel document using EPPlus reference/package. I can't get the Excel application to open. What code am I missing?

protected void BtnTest_Click(object sender, EventArgs e) {      FileInfo newFile = new FileInfo("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls");       ExcelPackage pck = new ExcelPackage(newFile);      //Add the Content sheet      var ws = pck.Workbook.Worksheets.Add("Content");      ws.View.ShowGridLines = false;       ws.Column(4).OutlineLevel = 1;      ws.Column(4).Collapsed = true;      ws.Column(5).OutlineLevel = 1;      ws.Column(5).Collapsed = true;      ws.OutLineSummaryRight = true;       //Headers      ws.Cells["B1"].Value = "Name";      ws.Cells["C1"].Value = "Size";      ws.Cells["D1"].Value = "Created";      ws.Cells["E1"].Value = "Last modified";      ws.Cells["B1:E1"].Style.Font.Bold = true; } 

I have tried pck.open(newFile);, but it doesn't allow it ...

like image 622
Code Ratchet Avatar asked Aug 10 '12 08:08

Code Ratchet


People also ask

Does EPPlus support XLS?

EPPlus does not work with the XLS format.

Does EPPlus require Excel?

No, it does not require Excel to be installed on the server, as you can read in the docs: EPPlus is a . NET library that reads and writes Excel files using the Office Open XML format (xlsx). EPPlus has no dependencies other than .


2 Answers

Try this:

protected void BtnTest_Click(object sender, EventArgs e) {     FileInfo newFile = new FileInfo("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls");      ExcelPackage pck = new ExcelPackage(newFile);     //Add the Content sheet     var ws = pck.Workbook.Worksheets.Add("Content");     ws.View.ShowGridLines = false;      ws.Column(4).OutlineLevel = 1;     ws.Column(4).Collapsed = true;     ws.Column(5).OutlineLevel = 1;     ws.Column(5).Collapsed = true;     ws.OutLineSummaryRight = true;      //Headers     ws.Cells["B1"].Value = "Name";     ws.Cells["C1"].Value = "Size";     ws.Cells["D1"].Value = "Created";     ws.Cells["E1"].Value = "Last modified";     ws.Cells["B1:E1"].Style.Font.Bold = true;      pck.Save();     System.Diagnostics.Process.Start("C:\\Users\\Scott.Atkinson\\Desktop\\Book.xls"); } 

Hope this helps!

like image 72
matthewr Avatar answered Sep 28 '22 16:09

matthewr


Try this in ASP NET Core to avoid win32 exception:

private void CreateWorkbook(string fileName) {     using (var p = new ExcelPackage())     {         p.Workbook.Properties.Author = "Barry Guvenkaya";         p.Workbook.Properties.Title = "MyTitle";         p.Workbook.Worksheets.Add("MySheet");         var bin = p.GetAsByteArray();         File.WriteAllBytes(fileName, bin);          // Below code opens the excel doc         var proc = new Process();         proc.StartInfo = new ProcessStartInfo(fileName)         {             UseShellExecute = true         };         proc.Start();     } } 
like image 36
Baz Guvenkaya Avatar answered Sep 28 '22 14:09

Baz Guvenkaya