如何导入excel文件,从表格编号和从和原始数据的用户提供的数据适配器
本文关键字:用户 何导入 原始数据 适配器 数据 编号 excel 导入 文件 表格 | 更新日期: 2023-09-27 18:09:04
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetNo ="Sheet" +txtImportExcelSheetNo.Text+"$";
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
string toRow = txtImportExcelToRw.Text;
string fromRow = txtImportExcelfromRw.Text;
con.Close();
}
}
//Read Data from the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
//Populate DataGridView.
}
其中sheetno, toRow和fromRow是由用户指定的,用于加载excel工作表。并要导入excel文件我想在datatable中加载excel表格。提前感谢
可以使用datatable.clone()
请查看下面的代码:
using (OleDbConnection con = new OleDbConnection(conStr))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
DataTable dtnew = new DataTable();
dtnew=dt.Clone();
if (dt.Rows.Count > 0)
{
for (int i = fromrow; i < dt.Rows.Count; i++)
{
DataRow dtRow = dt.Rows[i];
dtnew.ImportRow(dtRow);
}
有一个超级棒的工具叫做(NuGet) closesedxml。您可以使用它轻松地操作Excel文件。
我不能给你完整的答案,因为请求please give me a solution
太宽泛了。你必须先自己试试。