如何将excel数据导入gridControl(Dev控件)中,grid中有数据,在原有数据上追加
展开全部
在 WinForm中打开Excel
托一个 openFileDialog(openFileDialogSource) 和 toolStrip 和ComboBox(ctlPath) 控件
代码:
DataTable dTable;
// 打开
private void buttonOpen_Click(object sender, EventArgs e)
{
if (openFileDialogSource.ShowDialog()==DialogResult.OK)
{
ctlPath.Text = openFileDialogSource.FileName;
ExceltoDataSet(ctlPath.Text);
}
}
//打开方法
public DataTable ExceltoDataSet(string path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "Select * From [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, tableName);
System.Data.DataTable dt = ds.Tables[0];
dTable = dt;
return dt;
}
//显示数据
private void tsbRefresh_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(ctlPath.Text))
{
ctlPath.Focus();
return;
}
try
{
for (int i = 7; i >= 0; i--)
{
dTable.Rows.Remove(dTable.Rows[i]);
}
gridSource.DataSource = dTable;// gridSource :( DataGridView)
}
catch (Exception)
{
throw;
}
}
托一个 openFileDialog(openFileDialogSource) 和 toolStrip 和ComboBox(ctlPath) 控件
代码:
DataTable dTable;
// 打开
private void buttonOpen_Click(object sender, EventArgs e)
{
if (openFileDialogSource.ShowDialog()==DialogResult.OK)
{
ctlPath.Text = openFileDialogSource.FileName;
ExceltoDataSet(ctlPath.Text);
}
}
//打开方法
public DataTable ExceltoDataSet(string path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "Select * From [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, tableName);
System.Data.DataTable dt = ds.Tables[0];
dTable = dt;
return dt;
}
//显示数据
private void tsbRefresh_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(ctlPath.Text))
{
ctlPath.Focus();
return;
}
try
{
for (int i = 7; i >= 0; i--)
{
dTable.Rows.Remove(dTable.Rows[i]);
}
gridSource.DataSource = dTable;// gridSource :( DataGridView)
}
catch (Exception)
{
throw;
}
}
TableDI
2024-07-18 广告
2024-07-18 广告
在上海悉息信息科技有限公司,我们深知Excel在数据处理中的重要作用。在Excel中引用不同工作表(sheet)的数据是常见的操作,这有助于整合和分析跨多个工作表的信息。通过在工作表名称前加上感叹号“!”,您可以轻松地引用其他工作表中的数据...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询