用OLEDB进行Excel文件数据的读取,并返回DataSet数据集。其中有几点需要注意的:
1.连接字符串中参数
IMEX 的值:
0 is Export mode 1 is Import mode 2 is Linked mode (full update capabilities)
IMEX有3个值:当IMEX=2 时,EXCEL文档中同时含有字符型和数字型时,比如第C列有3个值,2个为数值型 123,1个为字符型 ABC,当导入时,
页面不报错了,但库里只显示数值型的123,而字符型的ABC则呈现为空值。当IMEX=1时,无上述情况发生,库里可正确呈现 123 和 ABC.
2.参数
HDR的值:
HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
3.参数
Excel 8.0
对于Excel 97以上版本都用Excel 8.0
<script type="text/javascript">
</script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> <script language="JavaScript1.1" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-3198703015597092&cpa_choice=CAAQpaa1_wEaCNG5n2ml8VCpKJ2R4YcBMAA&oe=gb2312&dt=1188872107952&lmt=1188489286&prev_fmts=ref_text&format=ref_text&output=textlink&correlator=1188872105569&url=http%3A%2F%2Fwww.showec.com%2Fblog%2Fuser1%2Fyilesoft%2Farchives%2F2006%2F2006428165751.html®ion=_google_cpa_region_&cc=363&ga_vid=1298455135.1188872106&ga_sid=1188872106&ga_hid=266042123&flash=6&u_h=768&u_w=1024&u_ah=734&u_aw=1024&u_cd=32&u_tz=480&u_java=true" type="text/javascript"></script>
Google AdSense 会在您的网站上提供与内容相关的广告
/**/
/// <summary>
/// 读取Excel文件,将内容存储在DataSet中
/// </summary>
/// <param name="opnFileName">带路径的Excel文件名</param>
/// <returns>DataSet</returns>
private DataSet ExcelToDataSet( string opnFileName)
... {
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+opnFileName+";Extended Properties=/"Excel 8.0;HDR=YES;IMEX=1/"";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = new DataSet();
strExcel = "select * from [sheet1$]";
try
...{
conn.Open();
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds,"dtSource");
return ds;
}
catch (Exception ex)
...{
MessageBox.Show("导入出错:" + ex, "错误信息");
return ds;
}
finally
...{
conn.Close();
conn.Dispose();
}
}
/// 读取Excel文件,将内容存储在DataSet中
/// </summary>
/// <param name="opnFileName">带路径的Excel文件名</param>
/// <returns>DataSet</returns>
private DataSet ExcelToDataSet( string opnFileName)
... {
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+opnFileName+";Extended Properties=/"Excel 8.0;HDR=YES;IMEX=1/"";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = new DataSet();
strExcel = "select * from [sheet1$]";
try
...{
conn.Open();
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds,"dtSource");
return ds;
}
catch (Exception ex)
...{
MessageBox.Show("导入出错:" + ex, "错误信息");
return ds;
}
finally
...{
conn.Close();
conn.Dispose();
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1771401