日期:2014-05-17 浏览次数:20458 次
public DataTable ExcelToDataTable(string ExcelFileName, string ExcelSheet)
{
try
{
string strExcelConn = "Provider = Microsoft.jet.oledb.4.0;Data Source = '" + ExcelFileName + "';Extended Properties = 'Excel 8.0;HDR=YES;IMEX=1;'";
if (ExcelFileName.Substring(ExcelFileName.LastIndexOf('.'), ExcelFileName.Length - ExcelFileName.LastIndexOf('.')) == ".xlsx")
strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
string selconn = string.Format("select * from [{0}$]", ExcelSheet);
OleDbConnection cnnxls = new OleDbConnection(strExcelConn);
OleDbDataAdapter daxls = new OleDbDataAdapter(selconn, strExcelConn);
DataTable xlsdt = new DataTable();
daxls.Fill(xlsdt);
cnnxls.Close();
cnnxls.Dispose();
return xlsdt;
}
catch (Exception)
{
return null;
//throw;
}
}