110,571
社区成员
发帖
与我相关
我的任务
分享
//以datatable的形式返回sheet的内容
public DataTable ShowSheet(string filePath, string sheetName)
{
string strConn = FatherForm.xml._excConn1 + filePath + FatherForm.xml._excConn2;
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string sql = "select * from [" + sheetName + "$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
都能返回一个dt,插入到sheet不难,这里就不写了。
2.
//返回所有sheet名
public string[] ImportExcel(string filePath)
{
string strConn = FatherForm.xml._excConn1 + filePath + FatherForm.xml._excConn2;
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dt=conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"table"});
int len=dt.Rows.Count;
string[] sheetNames = new string[len];
int start;
for (start = 0; start < len; start++)
{
string allName=dt.Rows[start]["TABLE_NAME"].ToString();
sheetNames[start] = allName.Replace("$","");//remove $ sysmbol
}
conn.Close();
return sheetNames;
}