加速Oledb写入Excel

时间:2014-11-11 18:52:16

标签: c# excel oledb

我正在尝试编写一个读取,排序和重写Excel工作表的程序,我已经写过它可以做到这一切,我现在主要担心的是以某种方式加快写入过程。

反正有人知道这样做吗?这是我写入新Excel表格的代码

foreach (DataRow dr in WriteThis.Rows)
{
    OleDbCommand Command2 = new OleDbCommand();
    string CommText = "INSERT INTO [" + Table + "$] Values(";
    foreach (string s in Columns)
    {
        CommText = CommText + '\u0022' + dr[s] + '\u0022' + ", ";
    }
    CommText = CommText.Remove(CommText.Length - 2);
    CommText += ");";
    //MessageBox.Show(CommText);
    Command2.CommandText = CommText;
    // CommText = CommText.Replace("\\", "");
    Command2.CommandText = CommText;
    Command2.Connection = Conn;
    Conn.Open();
    Command2.ExecuteNonQuery();
    Conn.Close();
}

1 个答案:

答案 0 :(得分:0)

取出连接开口的循环

Conn.Open();
foreach (DataRow dr in WriteThis.Rows)
{
    OleDbCommand Command2 = new OleDbCommand();
    string CommText = "INSERT INTO [" + Table + "$] Values(";
    foreach (string s in Columns)
    {
        CommText = CommText + '\u0022' + dr[s] + '\u0022' + ", ";
    }
    CommText = CommText.Remove(CommText.Length - 2);
    CommText += ");";
    //MessageBox.Show(CommText);
    Command2.CommandText = CommText;
    // CommText = CommText.Replace("\\", "");
    Command2.CommandText = CommText;
    Command2.Connection = Conn;
    //Conn.Open();
    Command2.ExecuteNonQuery();
    //Conn.Close();
}
Conn.Close();

希望帮助