Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every body
I am curently working on windows project. I want to import the excel data into database using c#.

My SpreadSheet is
ExcelSheet
|--------------------------------------------------|
|                                                  |
|--------------------------------------------------|
|                                                  |
|--------------------------------------------------|
| Merchant no:12345                                |
|--------------------------------------------------|
| merchant no|       Id No |Amount   | branch name |
|------------|-------------|---------|-------------|
|    12345   |         101 |  10000  | Hyd         |
|------------| ------------| --------|-------------|
|    12345   |         102 |  20000  |   Bombay    |
|------------|-------------|---------|-------------|
|     12345  |         103 |  30000  |     Delhi   |
 --------------------------------------------------


Now I want to read total excel sheet based on cell value=12345 (here starting two rows are empty and my code read all records ignore the empteis) and stored all records into database.This is my task. I already implement the code like below
C#
        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtExcel=new DataTable();
                string SourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
                OleDbConnection con = new OleDbConnection(SourceConstr);
                string query="Select * from [Sheet1$]";
                OleDbDataAdapter data=new OleDbDataAdapter(query,con);
                data.Fill(dtExcel);
                //dgvExcelData.DataSource = dtExcel;
                //dgvExcelData.ColumnHeadersVisible=false;
                string DestConstr = @"Data Source=COMPUTER-8EB749;Initial Catalog=TRMSDB;Integrated Security=true";
                SqlConnection connection = new SqlConnection(DestConstr);
                connection.Open();

               
               string Mno = "";
               foreach (DataRow rowExcel in dtExcel.Rows)
               {
                   foreach (DataColumn colExcel in dtExcel.Columns)
                   {
                       Mno = rowExcel[colExcel].ToString().Trim();
                       if (Mno != "")
                       {
                           string Mno1 = Mno.Substring(16, 10);
                           Mno =Mno1.ToString();
                           //Int32 MerchNo = Convert.ToInt32(Mno);
                       }
                       break;
                   }
                   if(Mno!="")// Mno contains the exact MerchantNo.
                   {
                       for(int i=0;i<dtexcel.rows.count;i++)>
                       {
                           if (dtExcel.Rows.Contains("MerchantNo=1105393011"))
                           {
                               string str = dtExcel.Rows[i][0].ToString().Trim();
                               string str1 = dtExcel.Rows[i][1].ToString().Trim();
                           }
                       }
                       
                   }
                      
                   }
        
}

Yes I have this type of knowledge for import the excel into data table and data table to data base.

My requirements on this excel sheet are
1. ignore the empty rows.
2. not read the first two rows and treated as empty rows.
3. read records based on cell value(12345)

my Queries are
1. I want to read total record at a time or read cell values based on row no and store into variable?

I am in confusion. Anybody give me good suggestion for import data and give good examples on this
or modify my code, please.
Posted
Updated 5-Sep-11 19:39pm
v2
Comments
Prerak Patel 6-Sep-11 1:40am    
Use code block for code segments.

1 solution

private void btn2Excel_Click(object sender, EventArgs e)
{

string strFileName = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Files (*.xls)|*.xls|(*.xlsm)|*.xlsm";
openFileDialog.DefaultExt = ".xls";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
strFileName = openFileDialog.FileName;


string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strFileName + @";Extended Properties=""Excel 12.0;HDR=YES;""";
OleDbConnection objConn = new OleDbConnection(connectionString);
// Open connection with the database.
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [MalePopulation$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(objDataset1);

DataSet dset2 = new DataSet();

OleDbCommand objCmdSelect1 = new OleDbCommand("SELECT * FROM [FemalePopulation$]", objConn);
OleDbDataAdapter objAdapter2 = new OleDbDataAdapter();
objAdapter2.SelectCommand = objCmdSelect1;
objAdapter2.Fill(dset2);


//for (i = 0; i < objDataset1.Tables[0].Columns.Count; i++)
//{
// for (j = 0; j < objDataset1.Tables[0].Rows.Count; j++)
// {
// DataColumn dc = new DataColumn();
// string str= dc.ColumnName;
// dr[j] = objDataset1.Tables[0].Rows[j][str];

// }
//}

SqlDataAdapter das = new SqlDataAdapter("Select * from TestPopulation", con);

DataSet dsetTEst = new DataSet();
das.Fill(dsetTEst);

int Year=2000;
dsetTEst.Clear();

for (int i = 0; i < objDataset1.Tables[0].Rows.Count; i++)
{

for(int j=0;j<objDataset1.Tables[0].Columns.Count;j++)
{
DataRow dRow = dsetTEst.Tables[0].NewRow();
dRow[0] = j;
dRow[1] = 2;
dRow[2] = Convert.ToDouble(objDataset1.Tables[0].Rows[i][Year.ToString()].ToString());
dRow[3] = Convert.ToDouble(dset2.Tables[0].Rows[i][Year.ToString()].ToString());
dRow[5] = objDataset1.Tables[0].Rows[i]["F1"];
dRow[4] = Year;
Year++;
obj._papulationid =Convert.ToInt32(dRow[0]);
obj._educationid =Convert.ToInt32( dRow[1]);
obj._malecount = Convert.ToDouble(dRow[2]);
obj._femalecount = Convert.ToDouble(dRow[3]);
obj._age = Convert.ToInt32(dRow[5]);
obj._year = Convert.ToInt32(dRow[4]);
obj.proj_update();

}

}
// Clean up objects.
objConn.Close();

}
}


Try this
 
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