【发布时间】:2016-01-26 12:53:46
【问题描述】:
我正在尝试在 Excel 工作表中选择总和并将其添加到列表框中,每一个都在一个新行中。
private void btn_update_Click(object sender, EventArgs e) {
for (int i = list_sheetnames.Items.Count -1; i >= 0; i--) {
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox_filename.Text + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
string selectCmd = "Select SUM(Total) As Total_SUM From [" + list_sheetnames.Items[i] + "$]";
using(OleDbConnection excelConn = new OleDbConnection(connString)) {
excelConn.Open();
OleDbCommand command = new OleDbCommand(selectCmd, excelConn);
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable sheetInfo = new DataTable();
da.Fill(sheetInfo);
//Do something with the data.
//list_total.Items.Add(sheetInfo);
list_total.DataSource = da.ToString();
}
}
}
我遇到了一个错误
【问题讨论】:
-
你得到哪个错误?
-
我稍微改写了你的问题,请包括错误