簡體   English   中英

在C#中將Excel文件導入到DataGridView中,缺少某些列

[英]Importing Excel File to DataGridView in C#, some columns are missing

**

  • 我想將Excel導入到DataGridView並保存到數據庫,但是首先,盡管有數據,但我還是在列中得到了一些空白數據。

  • 數據以Excel文件形式顯示,如圖所示,我想將此數據導入到DataGridView並將其保存到數據庫名稱Records.SDF。

**

在此處輸入圖片說明

 private void importFromExcelToolStripMenuItem_Click(object sender, EventArgs e)
    {

        openFileDialog1.ShowDialog();

        try
        {
            string filePath = openFileDialog1.FileName;
            string extension = Path.GetExtension(filePath);
            string conStr;

            conStr = string.Empty;
            switch (extension)
            {

                case ".xls": //Excel 97-03

                    string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
                    conStr = Excel03ConString; //string.Format(Excel03ConString, filePath);
                    break;

                case ".xlsx": //Excel 07
                    string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                            filePath +
                            ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';";
                    conStr = Excel07ConString;
                    break;
            }


            String name = "Sheet1";

               OleDbConnection con = new OleDbConnection(conStr);
               OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
               con.Open();

               OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
               System.Data.DataTable data = new System.Data.DataTable();

               sda.Fill(data);
               RecordsDataGridView.DataSource = data;

            DialogResult result = MessageBox.Show("Are you sure you want to Save the Recreations?", "Save Format",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes) { SaveData(); }

        }
        catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Exception Occured"); }
    }

    public void SaveData()
    {
        // Save the data.

        SqlCeConnection conn =
                new SqlCeConnection(
                   @"Data Source=|DataDirectory|\Records.sdf;Persist Security Info=False");

        SqlCeCommand com;
        string str;
        conn.Open();
        for (int index = 0; index < RecordsDataGridView.Rows.Count - 1; index++)
        {
            str = @"Insert Into ChequeRecords(ID,BankName,Date,AccountNo, Chequebook, ChequeNo, Payee, Amount, Remarks) Values(" + RecordsDataGridView.Rows[index].Cells[0].Value.ToString() + ", '" + RecordsDataGridView.Rows[index].Cells[1].Value.ToString() + "'," + RecordsDataGridView.Rows[index].Cells[2].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[3].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[4].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[5].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[6].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[7].Value.ToString() + "," + RecordsDataGridView.Rows[index].Cells[8].Value.ToString() + ")";
            com = new SqlCeCommand(str, conn);
            com.ExecuteNonQuery();
        }
        conn.Close();


    }

}

不知道為什么在第二,第四和第六列中會得到空白數據。

在此處輸入圖片說明

我的表格列沒有空間,但這有關系嗎? 在此處輸入圖片說明

這樣嘗試。

using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection ;
                System.Data.DataSet DtSet ;
                System.Data.OleDb.OleDbDataAdapter MyCommand ;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                dataGridView1.DataSource = DtSet.Tables[0];
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}

如果這不起作用,則Excel中必須有某種東西將其丟棄。 逐行逐步瀏覽此代碼示例(F11),然后查看失敗的地方。

http://csharp.net-informations.com/excel/csharp-excel-oledb.htm

暫無
暫無

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

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