当前位置:编程学习 > C#/ASP.NET >>

急急急, 都进来看看吧!

  private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            //打开一个文件选择框
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
            ofd.CheckFileExists = true;  //验证路径有效性
            ofd.CheckPathExists = true; //验证文件有效性


            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }

            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                return;
            }
            //调用导入数据方法
            EcxelToDataGridView(strName, this.OutLookAll); 

        }
        /// Excel数据导入方法   
        public void EcxelToDataGridView(string filePath, DataGridView dgv)
        {

            
      
            try
            {
                //根据路径打开一个Excel文件并将数据填充到DataSet中
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended Properties=\"Excel 12.0;HDR=YES\"";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "select * from [sheet1$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "[Sheet1$]"); 
                OutLookAll.DataMember = "[Sheet1$]";
                OutLookAll.DataSource = ds;
                conn.Close();
            }
            catch  
            {

                MessageBox.Show("连接失败");
                return;
              
            }
          
        } 




 

   string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended     Properties=\"Excel 12.0;HDR=YES\"";
OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();

老是打开失败 把 上面换成   string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='A0A.xlsx';Extended Properties=\"Excel 12.0;HDR=YES\"";  还得把 A0A.xlsx 添加到 项目中去 麻烦 


把Data Source 写死了 高人 帮我想想办法 

要求用上面的 
   string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended     Properties=\"Excel 12.0;HDR=YES\"";
OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open(); 





--------------------编程问答-------------------- string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended Properties=\"Excel 12.0;HDR=YES\"";

改为

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties=\"Excel 12.0;HDR=YES\""; --------------------编程问答--------------------
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended Properties=\"Excel 12.0;HDR=YES\"";


改为

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties=\"Excel 12.0;HDR=YES\"";
--------------------编程问答-------------------- 进来看看 --------------------编程问答-------------------- 看看看看 --------------------编程问答-------------------- 只看不说话 --------------------编程问答-------------------- 露个脸! --------------------编程问答--------------------
引用 2 楼 wuyazhe 的回复:
C# code
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+“filePath”+';Extended Properties=\"Excel 12.0;HDR=YES\"";


改为

C# code
string strConn = "Provider=Microsoft.ACE.OLEDB.……


同意,顶
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,