Click here to Skip to main content
15,908,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi The following program puts data from an Excel file into a DataTable.
C#
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Excel 2007 Files|*.xlsx";
            of.Title = "Open Excel 2007 Files";
            DialogResult dr = new DialogResult();
            dr = of.ShowDialog();
            if (dr == DialogResult.Cancel)
            return;
 
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source= " + of.FileName + " ;Extended Properties=\"Excel 12.0;HDR=No;\"";

            string strSQL = "SELECT * FROM [QueryExportToExcel$]";
         
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open();
 
            OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
 
            DataTable dTable = new DataTable();
            dataAdapter.Fill(dTable);

            DataGridView1.DataSource = dTable;
            int ram = 0;
            int cpu = 0;
			int hard = 0;
			int monitor = 0;
            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                string s=dTable.Rows[i][0].ToString().Trim();
                if (s.Contains ( "ram"))
                {
				// dTable.Rows[i][1] is Number of piece
                    ram += Convert.ToInt32(dTable.Rows[i][1]) * 5;
                }
                if (s.Contains( "cpu"))
                {
                    cpu += Convert.ToInt32(dTable.Rows[i][1]) * 3;
                }
				if (s.Contains ( "hard"))
                {
                    hard += Convert.ToInt32(dTable.Rows[i][1]) * 5;
                }
				if (s.Contains ( "monitor"))
                {
                    monitor += Convert.ToInt32(dTable.Rows[i][1]) * 5;
                }
            }
            label1.Text ="Score:"+ (ram + cpu + hard + monitor).ToString();
            dTable.Dispose();
            dataAdapter.Dispose();
            dbCommand.Dispose();
 
            excelConnection.Close();
            excelConnection.Dispose();

        }
    }

For each field in the excel file, there is a command "if" left.
The problem is that if a field is added to the Excel file for the field operations of an "if" I add.
Coefficients for each field in the Excel file does not exist. It is possible that the user can change
Posted
Updated 15-Aug-13 22:49pm
v2
Comments
Andreas Gieriet 16-Aug-13 5:28am    
Sorry for my ignorance, but I don't understand what you ask.
- What do you mean by "command" and what do you mean by "if"-command?
- What is "field operations"?
When you run this program, what is the effect?
Cheers
Andi
Maciej Los 16-Aug-13 6:15am    
Agree
idenizeni 16-Aug-13 18:21pm    
To clarify...
Your code checks the Excel cell values using an 'if' statement and performs your logic. And you want the code to be adaptable. So as new Excel cell values are added the 'if' conditions can be easily added and/or maintained by the users who maintain the Excel sheet?

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