Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,
I am uploading file as .csv in my project and with the help of stream reader i am taking excel columns to my sql table ..

C#
string[] rec = line.Split(',');
                                if (rec.Length > 0)
                                {
                                    DataRow dr = myExcelTable.NewRow();
                                    if (rec[0] != null)
                                    {
                                        dr["EmployeeId"] = rec[0];
                                    }
                                    else
                                    {
                                        dr["EmployeeId"] = "";
                                    }

                                    if (rec[1] != null)
                                    {
                                        dr["Date"] = rec[1];
                                    }
                                    else
                                    {
                                        dr["Date"] = "";
                                    }



this is what i am doing .. bt this will be ok if my excel has selected column ... what if my excel has many column .. can you suggest me alternative for array here which i used for Datarow and String[] rec here
Posted
Updated 1-Aug-13 3:29am
v2
Comments
Torakami 1-Aug-13 9:28am    
suggest me anything which both will gets added by using looping condition ..
Mukesh Ghosh 1-Aug-13 9:36am    
do you want to read excel or write excel?
Torakami 1-Aug-13 9:36am    
i wanted to read excel.. i am uploading

Don't use Split.
You can search here for CSV readers or try Rive[^].

Are you trying to match the CSV to an existing Excel sheet? Or simply import the CSV?
If the former, I recommend importing the CSV and then copying the data to the sheet.
 
Share this answer
 
TRy this, it will return a data table , from data table you can get all column as required

C#
public DataTable SelectExcelSheet(string strSheetName, string strSheetRange)
        {
            try
            {
                //Open and query
                if (oleConn == null) Open();

                if (oleConn.State != ConnectionState.Open)
                    return null;

                DataTable dataTable = new DataTable(strSheetName);

                //Fill table
                string SelectQuery = string.Format("SELECT * FROM [{0}${1}]", strSheetName, strSheetRange);
                using (OleDbDataAdapter oleAdapter = new OleDbDataAdapter(SelectQuery, oleConn))
                {
                    oleAdapter.FillSchema(dataTable, SchemaType.Source);
                    oleAdapter.Fill(dataTable);
                }
                dataTable.Dispose();
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (oleConn != null) Close();
            }
        }
 
Share this answer
 
v2
Comments
Maciej Los 1-Aug-13 16:04pm    
Mukesh Ghosh, you're not newbie on this forum. Please, use text formatting.

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