程序问题...... [英] Problem in program...

查看:41
本文介绍了程序问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
以下程序将Excel文件中的数据放入DataTable。

Hi The following program puts data from an Excel file into a DataTable.

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();

        }
    }



对于excel文件中的每个字段,都有一个命令if。

问题是如果在Excel文件中添加一个字段用于if的字段操作,我添加。

Excel文件中每个字段的系数不存在。用户可以更改


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

推荐答案

;

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 ]是件数
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 = 得分: +(ram + cpu + hard + monitor).ToString();
dTable.Dispose();
dataAdapter.Dispose();
dbCommand.Dispose();

excelConnection.Close();
excelConnection.Dispose();

}
}
"; 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(); } }



对于excel文件中的每个字段,都有一个命令if。

问题是如果在Excel文件中添加一个字段用于if的字段操作,我添加。

Excel文件中每个字段的系数不存在。用户可以更改


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


这篇关于程序问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆