//调用打开后的Excel
DataTable dt = OpenExcel();
string strMessage = dt.Rows[3][2].ToString();
//打开Excel
public DataTable OpenExcel()
{
string strPath = @"system/setup/part_list.xls";
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
OleConn.Close();
return OleDsExcle.Tables["Sheet1"];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
Winfrom操作EXCEL
最新推荐文章于 2021-12-21 13:48:24 发布