Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to import an excel to databse from asp.net website through dataset.
here is my first part.


C#
int xlColCount = 1;
       wb = app.Workbooks.Open(FilePath, 0, false, 5, "", "", true, Ex.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
       ws = (Ex.Worksheet)wb.ActiveSheet;
       xlColCount = ws.Columns.Count;
       xlColCount = ws.UsedRange.Columns.Count;


       ws.Columns.ClearFormats();

       xlColCount = ws.UsedRange.Columns.Count;

       //creating datacolumns
                   for (int i = 0; i < xlColCount; i++)
                   {

                       try
                       {
                           DSname.Tables[0].Columns.Add(((Ex.Range)ws.Cells[1,strColArr[i]]).Value2.ToString().Trim());
                       }
                       catch(Exception ex)
                       {
                           //error occured
                       }
                   }


first i am creating the column name based on the excel heading into dataset column.here xlColCount=198(total no.of columns from the excel template and all are filled ) but when reaching 172th column(i=172) it is giving index out of range error.

what could be the reason.I need to create a dataset with coulmn names from all excel coulmn names.

Anyone pls help it is urgent....
Posted
Comments
njammy 30-Jun-15 6:13am    
Are all the column values non-null when it is being read by the code? That can sometimes cause the col count to be less that what is physically in the file.

1 solution

strColArr[i] array capacity was the problem.It was initialized with 171 in size.
 
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