insert into database from excel sheet that has different no. of columns

旧城冷巷雨未停 提交于 2020-01-30 10:56:21

问题


Using c# and sqlite

Pretty much exactly as the title. I want to insert into a database an excel sheet that has a different number of columns than the database. I suspect I need to use an if case to check if the columns are as the same as the db but I'm not sure what the line of code for it would be. Mostly everything I have found are different to what I'm looking for. I can get the data set to display the excel based on the bottom but

    private void ImportExcelbutton_Click(object sender, EventArgs e)
    {
        OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

        if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            try
            {
            this.filenametext.Text = OpenFileDialog1.FileName;
            using (var stream = File.Open(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read))
            {
                using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                {
                    DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                    });
                    tableCollection = result.Tables;
                    cboSheet.Items.Clear();
                    foreach (DataTable table in tableCollection)
                        cboSheet.Items.Add(table.TableName);
                }
            }
            }
            catch
            {
                MessageBox.Show("Error file is open/in use");
            }

        }
        for(i=0;i<13;i++)
        {
            if(datagridview1.Columns[i] != dataset)
            {
            //code
            {
        }
    }

来源:https://stackoverflow.com/questions/59817437/insert-into-database-from-excel-sheet-that-has-different-no-of-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!