簡體   English   中英

如何將 Excel 文件數據導入 SQLite 數據庫中的 c# Z0F4137ED1502B50455D6083AA258B 表格?

[英]how to import Excel file data into SQLite database in c# windows form?

我正在嘗試將 excel 文件數據導入 SQLite 數據庫。 我使用了 2 個按鈕,導入和保存按鈕。

這是導入按鈕的代碼,我在其中嘗試從給定路徑獲取文件並將文件數據加載到 datagridview 中。

    private void import_btn_Click(object sender, EventArgs e)
        {
            string fileName= @"C:\Users\***\Documents\Customers.xlsx";
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(fileName);
            Worksheet sheet = workbook.Worksheets[0];
            this.dataGridView1.DataSource = sheet.ExportDataTable();
        }

在這里,我將數據導入 SQLite 數據庫表。

      private void save_btn_Click(object sender, EventArgs e)
        {
            string code = "C-0002";
            SQLiteConnection Con = new SQLiteConnection("Data Source=|DataDirectory|JDS_DB.sqlite");
            SQLiteCommand com;
            string str;
            Con.Open();
            for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
            {
                str = "insert into Customers(Name,Code,Group_,Address,Phone,Cell,Email_Address,Date,Opening_balance)values(@Name,@Code,@Group_,@Address,@Phone,@Cell,@Email_Address,@Date,@Opening_balance)";
                com = new SQLiteCommand(str, Con);
                com.Parameters.AddWithValue("@Name", dataGridView1.Rows[index].Cells[0].Value.ToString());
                com.Parameters.AddWithValue("@Code",code);
                com.Parameters.AddWithValue("@Group_", dataGridView1.Rows[index].Cells[1].Value.ToString());
                com.Parameters.AddWithValue("@Address", dataGridView1.Rows[index].Cells[2].Value.ToString());
                com.Parameters.AddWithValue("@Phone", dataGridView1.Rows[index].Cells[3].Value.ToString());
                com.Parameters.AddWithValue("@Cell", dataGridView1.Rows[index].Cells[4].Value.ToString());
                com.Parameters.AddWithValue("@Email_Address", dataGridView1.Rows[index].Cells[5].Value.ToString());
                com.Parameters.AddWithValue("@Date", dataGridView1.Rows[index].Cells[6].Value.ToString());
                com.Parameters.AddWithValue("@Opening_balance", dataGridView1.Rows[index].Cells[7].Value.ToString());
                com.ExecuteNonQuery();
                MessageBox.Show("added");
            }
            Con.Close();
        }

問題:

它工作正常,但我想從 excel 讀取數據並直接將其導入數據庫表中,而不是使用 datagridview。 如果有人知道怎么做,請告訴我,謝謝

您可以使用OleDbConnection打開與 Excel 文件的直接連接,在該連接上打開OleDbDataReader ,然后逐行讀取 SQLite 連接。

未經測試,因為您沒有告訴我們,您使用的是哪種 Excel 集成(互操作,...)。 基本上,您將結果存儲在變量或屬性中,這里我使用“數據”,然后直接訪問它。

private void save_btn_Click(object sender, EventArgs e)
{
    // Load the data
    string fileName = @"C:\Users\***\Documents\Customers.xlsx";
    Workbook workbook = new Workbook();
    workbook.LoadFromFile(fileName);
    Worksheet sheet = workbook.Worksheets[0];
    DataTable data = sheet.ExportDataTable();

    // Write the data
    string code = "C-0002";
    SQLiteConnection Con = new SQLiteConnection("Data Source=|DataDirectory|JDS_DB.sqlite");
    SQLiteCommand com;
    string str;
    Con.Open();
    foreach (DataRow row in data.Rows) //iterate over all rows
    {
        str = "insert into Customers(Name,Code,Group_,Address,Phone,Cell,Email_Address,Date,Opening_balance)values(@Name,@Code,@Group_,@Address,@Phone,@Cell,@Email_Address,@Date,@Opening_balance)";
        com = new SQLiteCommand(str, Con);
        com.Parameters.AddWithValue("@Name", row.Field<string>(0));
        com.Parameters.AddWithValue("@Code", row.Field<string>(1));
        //... you get the point :)
        com.ExecuteNonQuery();
        MessageBox.Show("added");
    }
    Con.Close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM