C# 使用从 ComboBox 中选择的值从通过 OpenFileDialog 加载到 DataGridView 中的文件中更新 Excel 中的列
C# Update column in Excel from file loaded in DataGridView via OpenFileDialog with a value selected from a ComboBox
我有一个小表格,其中有 2 个 Buttons
(Browse
和 updateExcel
)、一个 ComboBox
(comboBox1
)和一个 DataGridView
(dataGridView1
)
第一个按钮让您 select 一个 Excel 文件,然后将文件加载到 DataGridView
:
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = @"C:\";
op.Title = "Browse Excel Files";
op.CheckFileExists = true;
op.CheckPathExists = true;
op.DefaultExt = "xls";
op.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
op.FilterIndex = 2;
op.RestoreDirectory = true;
op.ReadOnlyChecked = true;
op.ShowReadOnly = true;
if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(op.FileName))
{
string[] Arr = null;
Arr = op.FileName.Split('.');
if (Arr.Length > 0)
{
if (Arr[Arr.Length - 1] == "xls")
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
op.FileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
}
else if (Arr[Arr.Length - 1] == "xlsx")
{
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + op.FileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
}
}
FillData();
}
}
这也使用了以下代码:
public string sConnectionString;
private void FillData()
{
if (sConnectionString.Length > 0)
{
OleDbConnection cn = new OleDbConnection(sConnectionString);
{
cn.Open();
DataTable dt = new DataTable();
OleDbDataAdapter Adpt = new OleDbDataAdapter("select * from [sheet1$]", cn);
Adpt.Fill(dt);
dataGridView1.DataSource = dt;
}
try { }
catch (Exception ex)
{
}
}
}
显示文件后,我有一个 ComboBox
(comboBox1
),它设置了可以 selected 的静态值。
我想做的是在另一个按钮 (updateExcel
) 上,它采用您在 ComboBox
中 select 编辑的值,然后替换 C 列中的所有值.
当前使用情况:
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System;
using System.ComponentModel;
using System.Data;
例如:
如果我的 Excel 文件是:
a b c d e f
aaa bbb ccc ddd eee fff
ggg hhh iii jjj kkk lll
mmm nnn ooo ppp qqq rrr
然后我从 ComboBox
中选择 XXX
我希望输出为:
a b c d e f
aaa bbb XXX ddd eee fff
ggg hhh XXX jjj kkk lll
mmm nnn XXX ppp qqq rrr
我使用以下代码解决了这个问题:
private void updateExcel_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
dataGridView1[2, i].Value = ConsigneeCombo.Text;
}
}
我有一个小表格,其中有 2 个 Buttons
(Browse
和 updateExcel
)、一个 ComboBox
(comboBox1
)和一个 DataGridView
(dataGridView1
)
第一个按钮让您 select 一个 Excel 文件,然后将文件加载到 DataGridView
:
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = @"C:\";
op.Title = "Browse Excel Files";
op.CheckFileExists = true;
op.CheckPathExists = true;
op.DefaultExt = "xls";
op.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
op.FilterIndex = 2;
op.RestoreDirectory = true;
op.ReadOnlyChecked = true;
op.ShowReadOnly = true;
if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(op.FileName))
{
string[] Arr = null;
Arr = op.FileName.Split('.');
if (Arr.Length > 0)
{
if (Arr[Arr.Length - 1] == "xls")
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
op.FileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
}
else if (Arr[Arr.Length - 1] == "xlsx")
{
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + op.FileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
}
}
FillData();
}
}
这也使用了以下代码:
public string sConnectionString;
private void FillData()
{
if (sConnectionString.Length > 0)
{
OleDbConnection cn = new OleDbConnection(sConnectionString);
{
cn.Open();
DataTable dt = new DataTable();
OleDbDataAdapter Adpt = new OleDbDataAdapter("select * from [sheet1$]", cn);
Adpt.Fill(dt);
dataGridView1.DataSource = dt;
}
try { }
catch (Exception ex)
{
}
}
}
显示文件后,我有一个 ComboBox
(comboBox1
),它设置了可以 selected 的静态值。
我想做的是在另一个按钮 (updateExcel
) 上,它采用您在 ComboBox
中 select 编辑的值,然后替换 C 列中的所有值.
当前使用情况:
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System;
using System.ComponentModel;
using System.Data;
例如:
如果我的 Excel 文件是:
a b c d e f
aaa bbb ccc ddd eee fff
ggg hhh iii jjj kkk lll
mmm nnn ooo ppp qqq rrr
然后我从 ComboBox
中选择 XXX
我希望输出为:
a b c d e f
aaa bbb XXX ddd eee fff
ggg hhh XXX jjj kkk lll
mmm nnn XXX ppp qqq rrr
我使用以下代码解决了这个问题:
private void updateExcel_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
dataGridView1[2, i].Value = ConsigneeCombo.Text;
}
}