使用OleDB将数据插入excel工作表中,使空行排在前面C#

时间:2018-12-01 09:40:21

标签: c# excel oledb

嘿,我要使用OleDB将数据插入excel工作表中,除非我从excel工作表中手动删除了几行,否则工作正常。现在,当我从OleDB插入数据时,这些行将留为空白。

这是一个代码

 String filename = @"E:DB\Account.xlsx";
                    String connection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
                    OleDbConnection con = new OleDbConnection(connection);
                    con.Open();
                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        String Command = "Insert into [Records$] (Serial_No,Client_Name,Width,Height,Qty,Sqft,Rate,Amount,Created_Date,Created_By) VALUES ('"
                                + int.Parse(data.Rows[i][0].ToString()) + "','"
                                + data.Rows[i][1].ToString() + "','"
                                + int.Parse(data.Rows[i][2].ToString()) + "','"
                                + int.Parse(data.Rows[i][3].ToString()) + "','"
                                + int.Parse(data.Rows[i][4].ToString()) + "','"
                                + int.Parse(data.Rows[i][5].ToString()) + "','"
                                + int.Parse(data.Rows[i][6].ToString()) + "','"
                                + int.Parse(data.Rows[i][7].ToString()) + "','"
                                + data.Rows[i][8].ToString() + "','"
                                + data.Rows[i][9].ToString() + "')";
                        OleDbCommand cmd = new OleDbCommand(Command, con);
                        cmd.ExecuteNonQuery();
                    }

                    con.Close();
                    MessageBox.Show("Success!");

这里是例子   enter image description here

0 个答案:

没有答案