Skip to content

Instantly share code, notes, and snippets.

View ram-sagar-mourya's full-sized avatar

Ram Sagar Mourya ram-sagar-mourya

  • Hyderabad, India
View GitHub Profile
@ram-sagar-mourya
ram-sagar-mourya / ReadWriteExcel.cs
Created August 3, 2017 14:42
Read and Write Excel using open xml in c#
private DataTable ReadExcelSheet(string fname, bool firstRowIsHeader)
{
List<string> Headers = new List<string>();
DataTable dt = new DataTable();
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fname, false))
{
//Read the first Sheets
Sheet sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
Worksheet worksheet = (doc.WorkbookPart.GetPartById(sheet.Id.Value) as WorksheetPart).Worksheet;
IEnumerable<Row> rows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();