把Excel里的数据转换为DataTable,并返回DataTable:
public static DataTable ExcelToDataTable(string excelPath)  //把Excel里的数据转换为DataTable,并返回DataTable
        {
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
            string strCom = "SELECT * FROM [Sheet1$]";
            DataTable dt;
            try
            {
                Conn.Open();
                System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "[Sheet1$]");
                Conn.Close();
                dt = ds.Tables[0];
            }
            catch (Exception err)
            {
                throw err;
            
            }
            return dt;
        }这个方法如果Excel表是打开的就能获得这个Excel里面的数据,如果Excel没有打开就会在conn.open()那里报错。
报错内容:.Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC)。请安装 Microsoft Data Access Components(MDAC) 2.6 或更高版本。还有一个我上传一个Excel表代码:Webservice 代码:
 [WebMethod]
        public void GetFileNew(Byte[] b, string fileName) 
        {
            AcceptFile(b, fileName);
        }
        public void AcceptFile(Byte[] b,string fileName)
        {
            string path = System.Configuration.ConfigurationManager.AppSettings["Path"];                string vPath = string.Format("{0}/{1}",path,fileName);                string finalPath = Server.MapPath(vPath);
                FileStream fStream = new FileStream(finalPath, FileMode.Create, FileAccess.Write);
                
                fStream.Write(b, 0,b.Length);
                fStream.Close();
        }
        [WebMethod]
        public Byte[] ReadFile(string filepath)
        {
            return SendFile(filepath); 
        }
        public Byte[] SendFile(string filepath)
        {
            // public static Byte[] SendFile(string fPath)
        
            FileStream fStream;
            fStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
            Byte[] b = new Byte[fStream.Length];
            fStream.Read(b, 0, b.Length);
            fStream.Flush();
            fStream.Close();
            return b;
    
        }
调用 Webservice代码:
  private void button4_Click(object sender, EventArgs e)
        {
            test2.ExcelServiceSoapClient h3 = new ExcelEnterFrm.test2.ExcelServiceSoapClient();
            string filepath=txtFileAddress.Text; 
            string NewExcel=filepath.Substring(filepath.LastIndexOf("\\")).Replace("\\","");
            string file = "" + operaname + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + NewExcel + "";
            Byte[] c1=h3.ReadFile(filepath);
            h3.GetFileNew(c1, file);
        }这个方法能够上传Excel表,但是如果Excel表是打开的,就上传不了,就报错,说表正在使用中。
求教:有没有方法(或者在我这个代码基础上做一下改动)使得Excel表打开或者关闭都能够导入、导出、上传。

解决方案 »

  1.   

    我做EXECL一直用NPOI 觉得不错  LZ可以试试  Excel确实不能打开 会提示被占用资源~
      

  2.   

    貌似没看出啥问题   参考:    public static  DataTable FromExcelToDataset(string sExcelPath)
        {
            try
            {
                string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + sExcelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
                string excelSelect = string.Format("select * from [Sheet1$]");
                DataSet ds = new DataSet();
                using (OleDbConnection oleconn = new OleDbConnection(strConn))
                {
                    //oleconn.Open();
                    OleDbDataAdapter oleAdapter = new OleDbDataAdapter(excelSelect, oleconn);
                    oleAdapter.Fill(ds);
                    //oleconn.Close();
                }
                return ds.Tables[0];
            }
            catch (OleDbException ex)
            {
                return null;
                throw ex;
            }
        }
      

  3.   

    这样读取EXCEL会有各种问题,是否可以考虑直接将EXCEL文件上传,然后服务端解析呢
      

  4.   

    GemBox.ExcelLite.dll你引入这个dll看看!
      

  5.   

    你使用的是什么版本????包括vs,包括excel
      

  6.   

    直接将EXCEL文件上传到服务端,在服务端绑定数据直接将xml导入DataTable就行了这样潜在的问题少些
      

  7.   


    vs2008 sql2005  office2007
      

  8.   

    这个不知道具体怎么弄    好像说Excel本身就是一个XML
      

  9.   


    NPOI 要钱!!!???