Skip to content

How to read excel sheet data and add those data to database?

Parth Mashroo

Asked Mar 30, 2015, 5:10 AM EDT1 answer
Share
LinkedInX
i have so far tried so many methods starting with OLEDB,ODBC and it worked but only offline but when i published my website it showed many errors and was to slow and then i shifted to NPOI a third party freeware tool and the code that i used is as follows :
 
Code
protected void btnthirdparty_Click(object sender, EventArgs e)
Code
    {
Code
        XSSFWorkbook wb;
Code
        XSSFSheet sb;
Code
        System.Data.DataTable dtnpoi = new System.Data.DataTable("Table");
Code
        dtnpoi.Columns.Add("SheetNames");
Code
        FileUpload1.SaveAs(HttpContext.Current.Server.MapPath(FileUpload1.FileName));
Code
        string file = Server.MapPath(FileUpload1.FileName);
Code
        using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
Code
        {
Code
            wb = new XSSFWorkbook(fs);
Code
            for (int s = 0; s < wb.Count; s++)
Code
            {
Code
                ddlsheets.Items.Add(wb.GetSheetAt(s).SheetName);
Code
            }
Code
            for (int i = 0; i < wb.Count; i++)
Code
            {
Code
                dtnpoi.Rows.Add((wb.GetSheetAt(i).SheetName));
Code
            }
Code
            foreach (DataRow row in dtnpoi.Rows)
Code
            {
Code
                String[] excelsheets = new String[dtnpoi.Rows.Count];
Code
                int k = 0;
Code
                excelsheets[k] = row["SheetNames"].ToString();
Code
                string company = excelsheets[k].ToString();
Code
Code
                for (int j = 0; j < excelsheets[k].Length; j++)
Code
                {
Code
Code
                    ISheet sheet = wb.GetSheet(excelsheets[k]);
Code
                    for (int rowsh = 0; rowsh < sheet.LastRowNum; rowsh++)
Code
                    {
Code
                        if (sheet.GetRow(rowsh) != null)
Code
                        {
Code
       //--------------------The Problem Is Here---------------------------------------//
Code
                            DataRow rw = dt.Rows.Add(sheet.GetRow(rowsh).GetCell(rowsh).StringCellValue);
Code
                            //ds.Tables[0]=null;
Code
                            // = sheet.GetRow(rowsh).GetCell(rowsh).ToString();
Code
                        }
Code
                    }
Code
Code
                    for (int i = 0; i < dt.Rows.Count; i++)
Code
                    {
Code
                        DataRow rows = dt.Rows[i];
Code
                        int coloum = dt.Columns.Count;
Code
                        string[] colom = new string[coloum];
Code
                        for (int m = 0; m < dt.Rows.Count; m++)
Code
                        {
Code
                            colom[m] = row[m].ToString();
Code
                        }
CSS
                        db.USP_Insert_TestTable(company, colom[0], colom[1], colom[2], colom[3], colom[4], colom[5]);
Code
                        company = "";
Code
                    }
Code
                }
Code
                k++;
Code
            }
Code
        }
CSS
    } 

i get the names of the sheet properly but the problem is after getting those sheet names 
i need to insert those sheet data to datatable so that i can save it to databasse but am unable to do so!! 
pls help me with that!!!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.