Read/Edit Excel 2007 File Contents using EPPlus

In my previous articles related to EPPlus I have shown you that how you can generate Basic and Advance reports using EPPlus. Many people has asked me that how we can read the contents of an excel file using EPPlus. This is actually quite simple task. Here is the code to do this:-
using (ExcelPackage p = new ExcelPackage())
{
    using (FileStream stream = new FileStream("92b69c48-dda7-4544-bdcc-c6925a5f1bec.xlsx"FileMode.Open))
    {
        p.Load(stream);
 
        ExcelWorksheet ws = p.Workbook.Worksheets["Sample Sheet"];
        int rowIndex = 2;
 
        string text = ws.Cells[rowIndex, 1].Value.ToString(); //Read Text
        MessageBox.Show("Text in [" + rowIndex + ",1]=" + text);
 
        string comment = ws.Comments[0].Text; // Access Comments
        MessageBox.Show("Comments = " + comment);
 
        string pictureName = ws.Drawings[0].Name;// Access Picture
        MessageBox.Show("Picture = " + pictureName);
    }
}

Things are very simple so I am not sharing more details. You can use above code to edit your existing excel files as well.

To download code, use this link.

Feel free to comment. Happy Coding !!!

Comments (14)

Loading... Logging you in...
  • Logged in as
This was a short but helpful post, it got me going. Once you touch it, the rest is progressively easy to discover.
Hey Zeeshan,
Thanks for the code. I can execute them perfectly.

Can u tell me how I should edit the file

I have tried adding
worksheet.Cells[6, 1].Value = "12010";

But I don't get any change in the value
1 reply · active 548 weeks ago
using (ExcelPackage p = new ExcelPackage())
{
using (FileStream stream = new FileStream("92b69c48-dda7-4544-bdcc-c6925a5f1bec.xlsx", FileMode.Open))
{
p.Load(stream);

ExcelWorksheet ws = p.Workbook.Worksheets["Sample Sheet"];
int rowIndex = 2;

string text = ws.Cells[rowIndex, 1].Value.ToString(); //Read Text
MessageBox.Show("Text in [" + rowIndex + ",1]=" + text);

string comment = ws.Comments[0].Text; // Access Comments
MessageBox.Show("Comments = " + comment);

string pictureName = ws.Drawings[0].Name;// Access Picture
MessageBox.Show("Picture = " + pictureName);
}
//Here is the change

p.Save();
}
It worked!

I had to add

p.Save();

at the end of the function and I could add any entry I wanted.

Thanks for the codes!
1 reply · active 548 weeks ago
How i read all file and save it in data table? plz help me
You can loop on ws.Cells[1, 1].Value and store that information in DataTable
How can I fill my aspxgridview? Could you give my any sample code?
xdfdsf
A short but very helpful article. Thank you very much for sharing.
Just a note for those who get the error message "A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT)) " from using this. I later learned this was because the existing Excel file that I wanted to edit/modify wasn't fully MS-Excel format compliant. I created the original excel file in Open office as an .xls file, but EPPlus was not able to read it. When I regenerated this original excel file in Online Excel, everything worked fine with this code.
1 reply · active 433 weeks ago
You are saving file as Xls, its extension should be Xlsx.
is it working for .xls ?
Good job...

Post a new comment

Comments by