Click here to Skip to main content
15,042,397 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to read excel row and sheet specific using open XML C# into sql server table ?

I have a excel file with no format I mean I can only read excel file in first sheet and first row, how i read second sheet and row A6? ( the names of a columns begin in A6 to D6)

I try with oledb and interoop and i read open xml is the choice for that

do you have examples , pls im newbie and late on the work

What I have tried:

protected void insertBoqElements_Click(object sender, EventArgs e)  
{  
    try  
    {  
        //specify the file name where its actually exist   
        string filepath = @ "D:\TPMS\Uploaded_Boq\test.xlsx";  
  
        //open the excel using openxml sdk  
        using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))  
        {  
  
            //create the object for workbook part  
            WorkbookPart wbPart = doc.WorkbookPart;  
  
            //statement to get the count of the worksheet  
            int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();  
  
            //statement to get the sheet object  
            Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);  
  
            //statement to get the worksheet object by using the sheet id  
            Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;  
  
            //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,....child[4]=sheetdata  
            int wkschildno = 4;  
  
  
            //statement to get the sheetdata which contains the rows and cell in table  
            SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);  
  
  
            //getting the row as per the specified index of getitem method  
            Row currentrow = (Row) Rows.ChildElements.GetItem(1);  
  
            //getting the cell as per the specified index of getitem method  
            Cell currentcell = (Cell) currentrow.ChildElements.GetItem(1);  
  
            //statement to take the integer value  
            string currentcellvalue = currentcell.InnerText;  
  
        }  
    } catch (Exception Ex)  
    {  
  
        lbldisplayerrors.Text = Ex.Message;  
    }  
  
}  
Posted
Updated 2-Oct-18 20:12pm

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900