How to import excel data considering the first row that do not have a header in C #?

2

To be able to import data from excel I use the following code:

Foldername = Server.MapPath("~/Importados/");

fileuploader.SaveAs(Foldername + filename.ToString());

String conStr = "";
switch (Extension)
{
    case ".xls": //Excel 97-03
        conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                 "Data Source=" + Foldername + "" + filename + ";" +
                 "Extended Properties=Excel 8.0;";
        break;

    case ".xlsx": //Excel 07
        conStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                 "Data Source=" + Foldername + "" + filename + ";" +
                 "Extended Properties=Excel 8.0;" +
                            "";
        break;
}
OleDbConnection oconn = new OleDbConnection();
OleDbCommand oCmd = new OleDbCommand();
//oconn.ConnectionString = conStr;
string conect = oconn.ConnectionString;

OleDbDataAdapter oDa = new OleDbDataAdapter();
DataSet oDs = new DataSet();
oconn.ConnectionString = conStr;
oconn.Open();
oCmd.CommandText = "select * from [Hoja1$]";
oCmd.Connection = oconn;
oDa.SelectCommand = oCmd;
oDa.Fill(oDs);
int fila = -1;
int avanzar = 0;
var campopartida="";
var camponombrepartida = "";

while (avanzar == 0)
{
    //para saber, posiblemente con mensajes despues o como querias en que fila se producen las incidencias
    fila = fila + 1;
    if ((oDs.Tables[0].Rows[fila][0]).ToString() == "")
    {
         avanzar = 1;
         break;
    }

    campopartida = (oDs.Tables[0].Rows[fila][0].ToString());
    camponombrepartida = oDs.Tables[0].Rows[fila][1].ToString();


}

This code is effective when you start reading from row 2 of the excel file, but it does not take the first row. I want the first row to be read as well, since it contains data and not headers.

To try to solve this problem, I was investigating that we had to add the following attribute HDR=No to the string conStr , that is to say:

 conStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                       "Data Source=" + Foldername + "" + filename + ";" +
                       "Extended Properties=Excel 8.0;" +
                        "HDR=No";

But in doing so I get the following error in oconn.Open ();:

  

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not controlled in the user's code   Additional information: The installable ISAM file could not be found.

Then try to try adding an additional row at the beginning, and so read from the first row that had data using this code:

Foldername = Server.MapPath("~/Importados/");
fileuploader.SaveAs(Foldername + filename.ToString());

String conStr = "";
switch (Extension)
{
     case ".xls": //Excel 97-03
         conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                  "Data Source=" + Foldername + "" + filename + ";" +
                  "Extended Properties=Excel 8.0;";
         break;

     case ".xlsx": //Excel 07
         conStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                  "Data Source=" + Foldername + "" + filename + ";" +
                  "Extended Properties=Excel 8.0;" +
                   "";
         break;
}
OleDbConnection oconn = new OleDbConnection();
OleDbCommand oCmd = new OleDbCommand();
//oconn.ConnectionString = conStr;


OleDbDataAdapter oDa = new OleDbDataAdapter();
//DataSet oDs = new DataSet();
oconn.ConnectionString = conStr;
oconn.Open();   
oCmd.Connection = oconn;
oDa.SelectCommand = oCmd;                      

oCmd.CommandText = "select * from [Hoja1$]";
//oDa.Fill(oDs);

DataTable dtDataTable = new DataTable();
using (OleDbDataAdapter oledbAdapter = new OleDbDataAdapter(oCmd))
     oledbAdapter.Fill(dtDataTable);
string stRowNumber = (dtDataTable.Rows.Count + 1).ToString();
string stRowID = "A" + stRowNumber + ":B" + stRowNumber;
oCmd.CommandText = "UPDATE [Hoja1$" + stRowID + "] SET F1=45, F2='54'";
oCmd.ExecuteNonQuery();
//oCmd.CommandText = "Insert into [Hoja1$] ([Hoja1$A1:A1],[Hoja1$B1:B1]) values('7','e')";
//oCmd.ExecuteNonQuery();
oconn.Close();

But I got the following error in oCmd.ExecuteNonQuery(); :

  

No values have been specified for some of the required parametersException of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not controlled in the user's code   Additional information: No values have been specified for some of the required parameters.

I'm already a bit bored trying to solve, since the solutions that appear in google do not give me results.

I hope I can count on you. receive any idea or help that is useful to me. Thank you in advance.

Note: The excel file I try to import is from office 2016

    
asked by Danilo 01.08.2018 в 06:54
source

1 answer

1

The Extended Properties of the connection string must be in quotes to be accepted, otherwise the exception you specify is thrown first. Test your first code using this:

conStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"", Foldername + filename);
    
answered by 01.08.2018 / 10:53
source