C# 用程序实现将Excel 导入到sql 数据库都的表中,该怎么解决

C# 用程序实现将Excel 导入到sql 数据库都的表中
C# 用程序实现将Excel 导入到sql 数据库都的表中 
具体专门实现,求大师
------解决思路----------------------
http://bbs.****.net/topics/390830774


当然了 上面的Demo只是导入到内存(datatable)中

至于你如何写入数据库 跟导入没什么关系..

你可以for insert 或者也可以sqlbulkcopy进行批量写.
------解决思路----------------------
参考:
http://blog.****.net/jpday/article/details/9915445
http://wenku.baidu.com/link?url=Ud2PVVHoz-3Xdt_RNTPR2zj8-H5f7LfV5KVVmbfmTWdrZDy9bQVLaKR_zRBsMeiOTIQMbmbkSe2yfOTkl4fitIapAHs2LH8nLH1q8IsVknu
------解决思路----------------------
        private void button1_Click(object sender, EventArgs e)
        {
              string connString =............................;//数据库连接字符串
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {

                TransferData(fd.FileName, "TPM", connString);//TPM表名

            }   
        }
        public void TransferData(string excelFile, string sheetName, string connectionString)
        {

            DataSet ds = new DataSet();

            try
            {

                //获取全部数据    

                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +

                     "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";

                OleDbConnection conn = new OleDbConnection(strConn);

                conn.Open();

                string strExcel = "";

                OleDbDataAdapter myCommand = null;

                strExcel = string.Format("select * from [{0}$]", sheetName);

                myCommand = new OleDbDataAdapter(strExcel, strConn);

                myCommand.Fill(ds, sheetName);



                //如果目标表不存在则创建    

                string strSql = string.Format("if object_id('{0}') is null create table {0}(", sheetName);

                foreach (System.Data.DataColumn c in ds.Tables[0].Columns)
                {

                    strSql += string.Format("[{0}] varchar(255),", c.ColumnName);

                }

                strSql = strSql.Trim(',') + ")";



                using (System.Data.SqlClient.SqlConnection sqlconn =

                    new System.Data.SqlClient.SqlConnection(connectionString))
                {

                    sqlconn.Open();

                    System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();

                    command.CommandText = strSql;

                    command.ExecuteNonQuery();

                    sqlconn.Close();

                }

                //用bcp导入数据    

                using (System.Data.SqlClient.SqlBulkCopy bcp =

                        new System.Data.SqlClient.SqlBulkCopy(connectionString))
                {

                    bcp.SqlRowsCopied +=

                         new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);

                    bcp.BatchSize = 100;//每次传输的行数    

                    bcp.NotifyAfter = 100;//进度提示的行数    

                    bcp.DestinationTableName = sheetName;//目标表    

                    bcp.WriteToServer(ds.Tables[0]);

                }

            }

            catch (Exception ex)
            {

                System.Windows.Forms.MessageBox.Show(ex.Message);

            }



        }
------解决思路----------------------
新手不要老想"一步到位"
excel导入数据库
其实是先excel读取到内存
内存在写入数据库
这是两个过程

读取excel的方法有多种
访问数据库的方法也有多种
你需要找到一种合适的方法来实现这个过程

没有任何一种方法是能把excel直接导入到数据库里的
除非是你手动打开excel,再手动打开数据库客户端,然后把数据复制,粘贴进去
------解决思路----------------------
参考MSDN代码示例项目: Imoprt Data from Excel to SQL Server

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.OleDb; 
using System.Data.Common; 
using System.Data.SqlClient; 
 
public partial class _Default : System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
 
    protected void Import_Click(object sender, EventArgs e) 
    {  
        // if you have Excel 2007 uncomment this line of code 
        //  string excelConnectionString =string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0",path); 
       
        string ExcelContentType = "application/vnd.ms-excel"; 
        string Excel2010ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
        if (FileUpload1.HasFile) 
        { 
            //Check the Content Type of the file 
            if(FileUpload1.PostedFile.ContentType==ExcelContentType 
------解决思路----------------------
 FileUpload1.PostedFile.ContentType==Excel2010ContentType) 
            { 
                try 
                { 
                    //Save file path 
                    string path = string.Concat(Server.MapPath("~/TempFiles/"), FileUpload1.FileName); 
                    //Save File as Temp then you can delete it if you want 
                    FileUpload1.SaveAs(path); 
                    //string path = @"C:\Users\Johnney\Desktop\ExcelData.xls"; 
                    //For Office Excel 2010  please take a look to the followng link  http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/0f03c2de-3ee2-475f-b6a2-f4efb97de302/#ae1e6748-297d-4c6e-8f1e-8108f438e62e 
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path); 
 
                    // Create Connection to Excel Workbook 
                    using (OleDbConnection connection = 
                                 new OleDbConnection(excelConnectionString)) 
                    { 
                        OleDbCommand command = new OleDbCommand 
                                ("Select * FROM [Sheet1$]", connection); 
 
                        connection.Open(); 
 
                        // Create DbDataReader to Data Worksheet 
                        using (DbDataReader dr = command.ExecuteReader()) 
                        { 
 
                            // SQL Server Connection String 
                            string sqlConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=ExcelDB;Integrated Security=True"; 
 
                            // Bulk Copy to SQL Server 
                            using (SqlBulkCopy bulkCopy = 
                                       new SqlBulkCopy(sqlConnectionString)) 
                            { 
                                bulkCopy.DestinationTableName = "Employee"; 
                                bulkCopy.WriteToServer(dr); 
                                Label1.Text = "The data has been exported succefuly from Excel to SQL"; 
                            } 
                        } 
                    } 
                } 
 
                catch (Exception ex) 
                { 
                    Label1.Text = ex.Message; 
                } 
            } 
        } 
    } 
}

------解决思路----------------------
引用:
Quote: 引用:

你要研究如何读EXCEL,那你首先要保证你的EXCEL是完整的,没有破损
否则说什么都白搭


亲,EXCEL源文件(本地的)可以正常打开,就是上传到项目里面项目里的EXCEL才打不开的,打不开可能就造成了访问不到EXCEL的Sheet$工作表了

所以说,你应该查看上传文件的代码,为什么上传的文件不完整,导致打不开
而不是想办法把一个已经不完整的excel补充完整