Home > Net >  C # using the OLEDB EXCEL operation, how to change your computer cannot find the column 0?
C # using the OLEDB EXCEL operation, how to change your computer cannot find the column 0?

Time:11-25

I do a small program in c #, read EXCEL, run no problem, but in the computer can't run, clew cannot find the column 0, the other computer with the original blog post, below is the code, hope ace to give advice or comments
Private void button1_Click (object sender, EventArgs e)
{
String strInputTemplate=Application. StartupPath + "\ \ Input Template. XLSX";
String strSqlInput="select product model, workshop from [Sheet1 $]".
The DataTable dtInput=DtReadExcel (strSqlInput strInputTemplate);
MessageBox. Show (dtInput. Rows [0] [r]. "product model" ToString ());
}
Public static DataTable DtReadExcel (string STRSQL, string strfilename)//returns the reading EXCEL table
{
String strConn;
StrConn="Provider=Microsoft. Ace. The OLEDB. 12.0. Data Source="+ strfilename +"; Extended Properties=\ "Excel Xml 12.0; HDR=Yes; IMEX=1 \ "; ";
Try
{
OleDbConnection OleConn=new OleDbConnection (strConn);
OleConn. The Open ();
OleDbDataAdapter OleDaExcel=new OleDbDataAdapter (STRSQL, OleConn);
The DataSet OleDsExcle=new DataSet ();
OleDaExcel. The Fill (OleDsExcle, "Sheet1");
OleConn. Close ();
Return OleDsExcle. Tables [0];
}
The catch (Exception ex)
{
The DataTable dt=new DataTable (" ex ");
DataRow Dr=dt. NewRow ();
Dr [0]=ex. ToString ();
Dt. Rows. The Add (Dr);
Return dt.
}
}

CodePudding user response:

Estimation is caused by an error, error, go to catch the inside, you catch an error to display inside, like you can't see what's wrong
 
Public static DataTable DtReadExcel (string STRSQL, string strfilename)//returns the reading EXCEL table
{
//an error here.
}
The catch (Exception ex)
{
The DataTable dt=new DataTable (" ex ");
DataRow Dr=dt. NewRow ();
Dr [0]=ex. ToString ();
Dt. Rows. The Add (Dr);
Return dt.
}
}

CodePudding user response:

Don't you set breakpoints look at ah, you are likely to be the exception into the exception, Dr [0] couldn't find it

CodePudding user response:

reference 1/f, kill matt, bull response:
estimation is caused by an error, error, go to catch the inside, you catch an error to display inside, like you can't see what's wrong
 
Public static DataTable DtReadExcel (string STRSQL, string strfilename)//returns the reading EXCEL table
{
//an error here.
}
The catch (Exception ex)
{
The DataTable dt=new DataTable (" ex ");
DataRow Dr=dt. NewRow ();
Dr [0]=ex. ToString ();
Dt. Rows. The Add (Dr);
Return dt.
}
}
your code and I don't like ah, I was running is no problem, without exception, just changed a computer can't, because I feel like my computer OFFICE is 32 bits, and other computer OFFICE is 64, I now can solve experiments for two kinds of solutions, just don't know whether to be fundamentally solved, : 1 is the project of Any CPU changed X64, (2) is in a 64 - bit mount a ACCESSDATABAEENGIN32 an OFFICE computer,
  •  Tags:  
  • C#
  • Related