You must Sign In to post a response.
  • Category: ASP.NET

    Import Excel File into database

    Import Excel to database with 2 or more Excel sheets using open xml in asp.net with c# without using OLEDB Concept.
  • #763574
    Hi NarayanaPrasad,

    What's your query?
    What you want?
    Import Excel to database, if you want the code, then I suggest you to Google it and if you struck in any place then let us know we will help you.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763590
    I suggest you to use OLEDB rather than openxml as it is very complex to understand, if you want to go with openxml then you need to use spreadSheetDocument.WorkbookPart
    I have some snippet to read excel file using openXml

    DataTable dt = new DataTable();

    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(@"..\..\example.xlsx", false))
    {
    WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
    IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
    string relationshipId = sheets.First().Id.Value;
    WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
    Worksheet workSheet = worksheetPart.Worksheet;
    SheetData sheetData = workSheet.GetFirstChild<SheetData>();
    IEnumerable<Row> rows = sheetData.Descendants<Row>();

    foreach (Cell cell in rows.ElementAt(0))
    {
    dt.Columns.Add(GetCellValue(spreadSheetDocument, cell));
    }
    foreach (Row row in rows) //this will also include your header row...
    {
    DataRow tempRow = dt.NewRow();

    for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
    {
    tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i-1));
    }
    dt.Rows.Add(tempRow);
    }
    }
    dt.Rows.RemoveAt(0); //...so i'm taking it out here.
    for more detail switch to following link
    http://stackoverflow.com/questions/12848230/openxml-sax-and-simply-reading-an-xlsx-file

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763606
    you will have to use OledbConnection and mention sheet name with Oledbcommand

    OleDbConnection oconn = null;
    string FilePath = "C:\\Products.xlsx";
    oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
    Data Source=" + FilePath + ";
    Extended Properties=Excel 8.0");
    OleDbCommand ocmd = new OleDbCommand("
    select * from [" + sheetname + "$]",
    oconn);

    for more details

    http://dotnetmentors.com/aspnet/display-excel-data-in-web-page.aspx

    Laxmikant


Sign In to post your comments