Pedro Pagotto
Pedro Pagotto

Reputation: 31

Problems reading all the lines of an excel with OLEDB and C#

I am developing a program to import excel spreadsheets with C # language, using the OLEDB component, when importing a spreadsheet with 100547 rows the program can only read 54046. Follows the source code:

   public class ReadExcel
{

    public string ConnectionExcel(ExcelUpload excelUpload)
    {
        //connection String for xls file format.
        if (excelUpload.fileExtension == ".xls")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        //connection String for xlsx file format.
        else if (excelUpload.fileExtension == ".xlsx")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }else
        {
            excelUpload.excelConnectionString = "";
        }
        return excelUpload.excelConnectionString;
    }

    public DataTable readArqExcel(string excelConnectionString, DataSet ds)
    {
        //Create Connection to Excel work book and add oledb namespace
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        excelConnection.Open();
        DataTable dt = new DataTable();

        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dt == null)
        {
            return null;
        }

        //Numero de planilhas contidas no excel
        String[] excelSheets = new String[dt.Rows.Count];
        int count = 0;

        //excel data saves in temp file here.
        foreach (DataRow row in dt.Rows)
        {
            excelSheets[count] = row["TABLE_NAME"].ToString();
            count++;
        }

        OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
        string query = string.Format("Select * from [{0}]", excelSheets[0]);
        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
        {
            dataAdapter.Fill(ds);
        }

        excelConnection.Close();

        return ds.Tables[0];
    }

I have tested IIS 8 (REMOTE SERVER) and IIS Express (Visual Studio local server), I noticed that on the IIS Express server the code works perfectly, but in IIS 8 the code ends up reading the file in half. Is it some kind of web server configuration?

Upvotes: 2

Views: 1294

Answers (1)

Pedro Pagotto
Pedro Pagotto

Reputation: 31

problem resolved, i altered string of connection OLEBD with MsExcel. Alter parameter IMEX = 2 for IMEX = 1, as below

if (excelUpload.fileExtension == ".xls")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
        }
        //connection String for xlsx file format.
        else if (excelUpload.fileExtension == ".xlsx")
        {
            excelUpload.excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelUpload.fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
        }

Upvotes: 1

Related Questions