【问题标题】:Save excel file in database using c#使用c#将excel文件保存在数据库中
【发布时间】:2019-02-24 18:49:41
【问题描述】:

我有一个 Excel 文件。现在我需要将excel文件的数据保存在数据库中。使用 c# 和简单示例来做到这一点的最简单方法是什么?提前致谢

【问题讨论】:

  • 将excel保存为CSV,然后使用C#解析。
  • 如果没记错的话,您还可以使用 SSMS 将该 CSV 文件导入到表格中,如果您小心的话,甚至可以将其复制/粘贴到编辑器窗口中。

标签: sql-server excel c#-2.0


【解决方案1】:

这会做你想做的。

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());
        }
    }

}

【讨论】:

  • 非常感谢您对我的帮助。您能帮我使用 c# 将数据从 sqlserver 2012 导出到 excel 文件 2013 吗?
  • 如果我的回答对您有帮助,请标记为有帮助。
【解决方案2】:

要从 SQL Server 表复制到 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());
            }
        }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2017-01-27
    相关资源
    最近更新 更多