以前一直在想,excel的格式和我们的数据库表格式是很相似的,我能不能就像读数据库表一样的读取excel表里面的内容。今天上网搜了一下,在次总结一下,方便以后的学习。
首先,我们要知道如何去读excel。。
代码如下:
class ReadExcel
{
public DataSet readexcel(String sExcelFile)
{
string sConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + sExcelFile + ";Extended Properties=Excel 8.0";
OleDbConnection connection = new OleDbConnection(sConnectionString);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]", connection);
DataSet ds = new DataSet();
adp.Fill(ds, "Book1");
return ds;
}
}