Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Count exact no of rows from excel sheet, save it in datatable and show the exact row value in gridview in asp.net c#

Thanks In advance
Maheswar Tripathy
Posted

Hi Maheswar, try this;

The below code will return as a DataSet, then you can bind your Gridview using this Dataset Collection of Tables. pass your excel file path to this function

C#
public static DataSet ImportExceltoDataset(string YourFilePath)
        {

            Microsoft.Office.Interop.Excel.Application oXL;
            Workbook oWB;
            Worksheet oSheet;

            Range oRng;
            //  creat a Application object
            oXL = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                //   get   WorkBook  object
                oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value);

                //   get   WorkSheet object 
                int M = oXL.Worksheets.Count;

                for (int sheetnum = 1; sheetnum <= M; sheetnum++)
                {
                    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[sheetnum];

                    for (int j = oSheet.UsedRange.Cells.Columns.Count + 10; j > 0; j--)
                    {
                        if (j > oSheet.UsedRange.Cells.Columns.Count)
                        {
                            if (string.IsNullOrEmpty(((Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j]).Text.ToString()))
                            {
                                ((Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j]).EntireColumn.Delete(null);
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(((Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j]).Text.ToString()))
                            {
                                int IsDeleteCunt = 1;
                                for (int i = 1; i < oSheet.UsedRange.Cells.Rows.Count; i++)
                                {
                                    if (string.IsNullOrEmpty(((Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j]).Text.ToString()))
                                    {
                                        IsDeleteCunt++;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (IsDeleteCunt == oSheet.UsedRange.Cells.Rows.Count)// Here you can get the used rows.
                                {
                                    ((Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j]).EntireColumn.Delete(null);
                                }
                            }
                        }
                    }
                }

                DataSet ds = new DataSet();
                string WrkshtName = "";
                for (int N = 1; N <= M; N++)
                {
                    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[N];
                    WrkshtName = oSheet.Name;
                    System.Data.DataTable dt = new System.Data.DataTable(WrkshtName);
                    ds.Tables.Add(dt);
                    DataRow dr;

                    StringBuilder sb = new StringBuilder();

                    int jValue = oSheet.UsedRange.Cells.Columns.Count;
                    int iValue = oSheet.UsedRange.Cells.Rows.Count;
                    int EmptyColumnCount = 1;
                    for (int j = 1; j <= jValue; j++)
                    {
                        oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j];

                        string strValue = oRng.Text.ToString();
                        if (strValue.Trim() == "")
                        {
                            EmptyColumnCount++;
                        }

                        dt.Columns.Add(strValue, System.Type.GetType("System.String"));
                    }
                    if (EmptyColumnCount >= jValue)
                    {
                        ds.Tables.Remove(WrkshtName);
                    }
                    else
                    {
                        for (int i = 2; i <= iValue; i++)
                        {
                            dr = ds.Tables[WrkshtName].NewRow();
                            int k = 0;
                            EmptyColumnCount = 1;
                            for (int j = 1; j <= jValue; j++)
                            {
                                oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
                                ((Range)oSheet.Cells[1, j]).EntireColumn.AutoFit();

                                string strValue = oRng.Text.ToString();
                                if (strValue.Trim() == "")
                                {
                                    EmptyColumnCount++;
                                }
                                dr[k] = strValue;
                                k++;
                            }
                            if (EmptyColumnCount < jValue)
                            {
                                ds.Tables[WrkshtName].Rows.Add(dr);
                            }
                        }
                    }
                }
                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }   
        }



I hope this will help you. If you found any errors/difficulty means let me know.

Thank's
Mohan G
 
Share this answer
 
v2
Comments
Maheswar Tripathy 11-Sep-13 1:30am    
Thanks Mohan G it will help me
A simple answer is that you need to check each row of the excel sheet to check if that row is containing any data in one of it's cell. If it does then update the counter in your code; once you are done you can read the file and create the datatable then load it with the data from excel sheet (you can minimize do it in a better way by reading and counting the rows at the same time and then just load that data in the datatable; in that case you will have to read the excel file once only). Once data-table is loaded, you can do whatever you want to do with it.
 
Share this answer
 

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