我用asp.net 2.0 做了一个读取excel数据到datalist中的程序。在本地调试的时候是可以的,可以正常的现实,但是我把代码放到服务器中,就总是提示我:
Microsoft Jet 数据库引擎找不到对象'C:\Documents and Settings\Administrator\桌面\aaa.xls'。请确定对象是否存在,并正确地写出它的名称和路径。 
不知道如何解决,请高数指点:我的代码如下:if (FileUpload1.HasFile)
        {
            string filepath = FileUpload1.PostedFile.FileName;
            string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);            string dbName = "Sheet1";            DL_Main.DataSource = GetExcelContent(filepath, dbName);
            this.DL_Main.DataBind();
           
           
        }
        else
        {            Response.Write("<script>alert('请选择要上传的文件');</script>");
        }private DataTable GetExcelContent(string filepath, string dbName)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
        System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection(strCon);
        string strCom = "SELECT * FROM [" + dbName + "$]";
        myConn.Open();
        System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, myConn);
        //创建一个DataSet对象 
        DataTable myDataSet = new DataTable();
        //得到自己的DataSet对象 
        myCommand.Fill(myDataSet);
        //关闭此数据链接 
        myConn.Close();
        return myDataSet;    }
可能是路径的问题,请高数指导!!

解决方案 »

  1.   

    using System;   
    using System.Collections.Generic;   
    using System.Text;   
    using System.Data;   
    using System.Data.OleDb;   
    using System.Data.SqlClient;   
    using System.IO;   
    using Microsoft.Office.Interop.Excel;   
    namespace TestAccess   
    {   
        class Program   
        {   
            static void Main(string[] args)   
            {   
      
                string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";   
                strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";   
                strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";   
                OleDbConnection objConnection = new OleDbConnection(strConnection);   
                objConnection.Open();   
                OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);   
                DataSet ds = new DataSet();   
                myCommandd.Fill(ds, "[Sheet1$]");   
                System.Data.DataTable dt = ds.Tables["[Sheet1$]"];   
                Console.WriteLine(dt.Columns[0].ToString());   
                Console.WriteLine(dt.Columns[1].ToString());   
                DataRow drDisplay = dt.Rows[0];   
                int[] num = new int[dt.Columns.Count];   
                for (int j = 0; ; )   
                {   
                    for (int i = 0; i < dt.Columns.Count; i++)   
                    {   
      
                        if (drDisplay[i] is DBNull) ;   
                        else  
                            num[i] += Convert.ToInt32(drDisplay[i]);   
      
                    }   
                    if (++j >= dt.Rows.Count) break;   
                    drDisplay = dt.Rows[j];   
                }   
                objConnection.Close();   
                object MissingValue = Type.Missing;   
                Microsoft.Office.Interop.Excel.Application app = new Application();   
                Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,   
    MissingValue, MissingValue, MissingValue,   
    MissingValue, MissingValue, MissingValue,   
    MissingValue, MissingValue, MissingValue,   
    MissingValue, MissingValue, MissingValue,   
    MissingValue);   
                Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;   
                for (int i = 0; i < dt.Columns.Count; i++)   
                {   
                    //注意下面是i+1,,excel小标默认从1开始   
                    wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();   
                }   
      
                wbook.Save();   
                wbook.Close(true, null, null);   
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);   
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);   
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);   
                wsheet = null;   
                wbook = null;   
                app = null;   
                GC.Collect();   
            }   
        }   
    }  
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspx
      

  2.   


    string filepath = FileUpload1.PostedFile.FileName你这是准备把客户端的文件往服务器上传的吗!?
    然后按这个路径去操作excel的吗!?
      

  3.   

    很明显,路径错误
    你需要先FileUpload1.Save到服务器,再进行操作
      

  4.   

    本帖最后由 net_lover 于 2009-12-24 15:56:22 编辑
      

  5.   

    本帖最后由 net_lover 于 2009-12-24 15:57:00 编辑
      

  6.   

    多谢!!各位大哥有上传excel文件的代码吗?
    给小弟参考一下!!!
      

  7.   

    如果是txt格式的,可以参考不经保存,直接读取上传文件的内容 
    http://dotnet.aspx.cc/article/78280914-a75c-40dc-9dac-322b3d81be35/read.aspx