简体   繁体   English

ASP.NET OleDb Excel与工作表的连接

[英]ASP.NET OleDb Excel Connection to Worksheet

I have an Excel file that the customer is pulling from their financial software and uploading to my web app. 我有一个Excel文件,客户正在从他们的财务软件中提取并上传到我的Web应用程序。 I need to connect to the file through ADO and read its contents into a SQL database. 我需要通过ADO连接到文件,并将其内容读入SQL数据库。

The problem is that the file that comes from the financial software is a standalone Excel Worksheet, not Workbook, so no piece of software (besides Excel) that I've found can connect to/open it. 问题在于,来自财务软件的文件是独立的Excel工作表,而不是工作簿,因此我发现没有软件可以连接或打开该文件(Excel除外)。 No matter what connection string I use in the OleDB connector, I can't get it to work. 无论我在OleDB连接器中使用什么连接字符串,我都无法使用它。

If I open the file in Excel, then simply save it, I can connect and read it fine. 如果我在Excel中打开文件,然后将其保存,就可以连接并正常阅读了。 I wrote some code to automate Excel, using the Office interop, that opens/saves the file, and it works, but I've concluded this is bad practice to do on a server, based on testing and reading. 我使用Office互操作编写了一些代码来自动化Excel,该文件可以打开/保存文件,并且可以正常工作,但是基于测试和阅读,我认为在服务器上执行此操作是不明智的做法。

Does anyone know of a way I can get around this problem? 有人知道我可以解决这个问题的方法吗? I've seen some third party libraries, but they are very expensive. 我看过一些第三方库,但是它们非常昂贵。

Thanks in advance. 提前致谢。

    HttpPostedFile jvFile = FileUpload1.PostedFile;
string jvPath = Path.Combine(Server.MapPath("~"), Path.GetFileName(jvFile.FileName));
jvFile.SaveAs(jvPath);

string[] begins = {
                      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=",
                      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=",
                  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=",
                  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                  };
string[] ends = {
                    ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"",
                    ";Extended Properties=\"Excel 8.0;HDR=Yes;\"",
                ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"",
                ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"",
                ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"",
                ";Extended Properties=\"Excel 12.0;HDR=YES\"",
                ";Extended Properties=\"Excel 12.0 Macro;HDR=YES\"",
                ";Extended Properties=\"text;HDR=Yes;FMT=Delimited\"",
                ";Extended Properties=\"text;HDR=Yes;FMT=Fixed\";"
                };

for(int i = 0; i < begins.Length; i++)
{
    StringBuilder sbExcelFileConnStr = new StringBuilder();
    sbExcelFileConnStr.Append(begins[i]);
    sbExcelFileConnStr.Append(jvPath);
    sbExcelFileConnStr.Append(ends[i]);

    OleDbConnection dbConn = new OleDbConnection(sbExcelFileConnStr.ToString());
    string[] excelSheets = { };
    try
    {
        dbConn.Open();
    }
    catch (Exception ex)
    {
         // fails here with "System.Data.OleDb.OleDbException: 
         // External table is not in the expected format."
         //
         //
    }
}

I can't put the problem file anywhere b/c it contains sensitive data. 我无法将问题文件放在包含敏感数据的任何地方。

I've used the Excel Data Reader for reading Excel files. 我已经使用Excel数据读取器读取Excel文件。 It's free. 免费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM