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 itCodePudding user response: