无法在Asp .Net中导入xlsx文件

时间:2011-01-17 10:55:43

标签: asp.net vb.net

我无法在Asp .Net中导入xlsx文件,导致以下错误: -

无法找到可安装的ISAM。

我使用下面的代码导入xlsx文件: -

“功能

   Protected Function ExcelConnection() As OleDbCommand

    ' Connect to the Excel Spreadsheet
    Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=C:\Users\tcs0028\Desktop\Upload1.xlsx;" & _
                "Extended Properties=Excel 12.0;"

    ' create your excel connection object using the connection string
    Dim objXConn As New OleDbConnection(xConnStr)
    objXConn.Open()

    Dim objCommand As New OleDbCommand("SELECT * FROM [Sheet1$]", objXConn)
    Return objCommand

Plz建议?

5 个答案:

答案 0 :(得分:2)

您需要先将连接字符串更改为

Dim ConnStr As String =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =〜\ Upload1.xlsx”; Extended Properties = \“Excel 12.0 Xml; HDR = YES; IMEX = 1 \”;“;

答案 1 :(得分:1)

试试Excel Package Plus这是一个带有附加功能的Excel软件包,它仍然可以维护和开发。

答案 2 :(得分:1)

 public class ImportExcel
    {
        public DataTable Importar(string arquivo)
        {
            string ext = Path.GetExtension(arquivo);
            string aspas = "\"";
            string Conexao = string.Empty;

            if (ext == ".xls")
            {
                Conexao = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + arquivo + ";" + "Extended Properties=" + aspas + "Excel 8.0;HDR=YES" + aspas;
            }
            if (ext == ".xlsx")
            {
                Conexao = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + arquivo + ";" + "Extended Properties=" + aspas + "Excel 12.0;HDR=YES" + aspas;
            }

            System.Data.OleDb.OleDbConnection Cn = new System.Data.OleDb.OleDbConnection();
            Cn.ConnectionString = Conexao;
            Cn.Open();
            object[] Restricoes = { null, null, null, "TABLE" };
            DataTable DTSchema = Cn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, Restricoes);
            if (DTSchema.Rows.Count > 0)
            {
                string Sheet = DTSchema.Rows[0]["TABLE_NAME"].ToString();
                System.Data.OleDb.OleDbCommand Comando = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + Sheet + "]", Cn);
                DataTable Dados = new DataTable();
                System.Data.OleDb.OleDbDataAdapter DA = new System.Data.OleDb.OleDbDataAdapter(Comando);
                DA.Fill(Dados);
                Cn.Close();
                return Dados;
            }
            return null;
        }
    }

重要

  
    

要导入xlsx文件,您需要安装从microsoft下载的AccessDatabaseEngine.exe

         

导入完成后,用户无法打开工作表,因此在临时文件夹中创建此副本并将此路径作为参数传递非常重要。     (别忘了删除临时的)

  

使用:

System;
System.Data;
System.Data.Odbc;
System.Data.OleDb;
System.IO;
System.Reflection;
System.Text;
System.Web;

答案 3 :(得分:0)

您可以尝试使用Excel Package

答案 4 :(得分:0)

您需要先将连接字符串更改为

Dim xConnStr As String =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \ tcs0028 \ Desktop \ Upload1.xlsx”;扩展属性= \“Excel 12.0 Xml; HDR = YES; IMEX = 1 \ “;”;

接下来你需要安装MSExcel以便能够导入Excel文件,或者你可以运行一个免费的替代软件包,就像安装了Excel 2007一样 这是链接:

http://www.microsoft.com/downloads/en/details.aspx?familyid=7554f536-8c28-4598-9b72-ef94e038c891&displaylang=en

希望有所帮助。