在C#中将Excel文件插入数据库

问题描述 投票:-2回答:1

它基本上是一个与Arduino连接的系统,您可以使用您的校园卡与系统链接,当您链接时,您可以使用打印机或3D打印机。

数据库到Excel的导出已经起作用,但不是导入到数据库的功能。

码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace HTC_toestemming_systeem_V1
    {
        public partial class ImportFromExcel : Form
        {
            //doesnt work
             private string openLocation;
             public int columns { get; set; }
             public int rows { get; set; }

        public ImportFromExcel()
        {
            InitializeComponent();
        }

        private void Kies_Folder_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog sfd = new OpenFileDialog())
            {
                selectFile:
                sfd.Title = "Selecteer een bestand";
                sfd.Filter = "Excel bestand (*.xls)|*.xls";

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    openLocation = sfd.FileName;
                    label1.Text = openLocation;
                }
                else
                {
                    if (MessageBox.Show("Geen locatie geselecteerd!\n\nWil je alsnog een locatie selecteren ?", "Dan niet", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information) == DialogResult.Retry)
                    {
                        goto selectFile;
                    }
                    return;
                }
            }
        }

        private void Lees_Excel_Click(object sender, EventArgs e)
        {
            //gebruikt de Excel.cs bestand
            Excel_ excel = new Excel_(@openLocation, 1);
            int column = excel.columns;
            int row = excel.rows;
            //MessageBox.Show(column + " + " + row);
            //doet alles in een array
            string[,] read = excel.ReadRange(1, 1, row, column);
            excel.Close();
        }

        private void ImportFromExcel_Load(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
 }
c# excel import export printers
1个回答
0
投票

我认为最简单的方法是使用EPPlus库并将文件内容加载到DataTable中,然后您可以使用多种方法将数据导入数据库。

  • 如果您只想将数据插入数据库,可以使用SqlBulkCopy

这里有一些有用的教程和资源:

© www.soinside.com 2019 - 2024. All rights reserved.