Problems reading all the rows of an Excel with OLEDB and C #

1

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

Here is 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 tried the IIS 8 (REMOTE SERVER) and IIS Express (local server of Visual Studio), I noticed that on the server IIS Express the code works perfectly, but in IIS 8 the code ends up reading the file in the middle.

Is it some kind of Web server configuration?

    
asked by anonymous 29.03.2017 / 19:16

1 answer

0

I was able to solve the problem through the connection string with Excel as below, changing the IMEX = 2 parameter to

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\"";
        }
    
20.06.2017 / 22:51