使用 C# 在数据库中保存 excel 文件

Save excel file in database using c#

我有一个 excel 文件。现在我需要在数据库中保存 excel 文件的数据。使用简单示例使用 c# 执行此操作的最简单方法是什么?提前致谢

这会做你想做的事。

private void button1_Click(object sender, EventArgs e)
{
    System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your_path\Import_List.xls;Extended Properties=Excel 8.0;");

    ExcelConnection.Open();

    string expr = "SELECT * FROM [Sheet1$]";
    OleDbCommand objCmdSelect = new OleDbCommand(expr, ExcelConnection);
    OleDbDataReader objDR = null;
    SqlConnection SQLconn = new SqlConnection();
    string ConnString = "Data Source=Your_Database_Name;Initial Catalog=Table_Name;Trusted_Connection=True;";
    SQLconn.ConnectionString = ConnString;
    SQLconn.Open();

    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLconn))
    {

        bulkCopy.DestinationTableName = "tblTest";

        try
        {
            objDR = objCmdSelect.ExecuteReader();
            bulkCopy.WriteToServer(objDR);
            ExcelConnection.Close();

            //objDR.Close()
            SQLconn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

}

要从 SQL 服务器 table 复制到 Excel 文件,请尝试以下操作。

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.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                sql = "Insert into [Sheet1$] (id,name) values('5','e')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}

或者,使用 'Where' 子句,您可以更好地控制输出。

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.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                sql = "Update [Sheet1$] set name = 'New Name' where id=1";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}