苏飞论坛

标题: excel导入出错的问题 [打印本页]

作者: renxiaoyao    时间: 2013-8-3 16:41
标题: excel导入出错的问题
如题一样,我才刚学的C#现在想写个导入excel,但是老是出现这个问题,请大家帮我看看啊
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;        
using System.Data.OleDb;



namespace 导入excel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        DataSet ds = new DataSet();         //全局变量
            private void button1_Click(object sender, EventArgs e)   
        {     
            //测试,将excel中的sheet1导入到sqlserver中
string connString = "server=localhost;uid=sa;pwd=sqlgis;database=master";     
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();   
            if (fd.ShowDialog() == DialogResult.OK)   
            {     
                TransferData(fd.FileName, "sheet1", connString);
            }   
        }   
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);   

               //如果目标表不存在则创建 exce文件的第一行为列标题,从第二行开始全部都是数据记录

                string strSql = string.Format("if object_id('{0}') is null create table {0}(", sheetName);   //以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导入数据

                //excel文件中列的顺序必须和数据表的列顺序一致,因为数据导入时,是从excel文件的第二行数据开始,不管数据表的结构是什么样的,反正就是第一列的数据会插入到数据表的第一列字段中,第二列的数据插入到数据表的第二列字段中,以此类推,它本身不会去判断要插入的数据是对应数据表中哪一个字段的


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

        //进度显示


   void bcp_SqlRowsCopied(object sender,  System.Data.SqlClient.SqlRowsCopiedEventArgs e)   
        {     
            this.Text = e.RowsCopied.ToString();   
            this.Update();   
        }   
    }   
}


=======================================================================
老是会提示上下文中不存在名称sqlconn,望各位大侠看看.

作者: 站长苏飞    时间: 2013-8-3 17:39
     //获取全部数据
     string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";  把这个放到方法意外,局部变量在其他方法里肯定访问不到啊
作者: renxiaoyao    时间: 2013-8-7 09:19
老大V5,能详细点吗?看的不是很懂呢...
作者: yangying    时间: 2013-8-7 09:25
  //获取全部数据
     string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";     
就是把这个放到方法的外面




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4