#region 读取excel文件
private DataSet readExcelInDataset(string filename)
{
try
{
Excel.Application xApp = new Excel.ApplicationClass();
Excel.Workbook xBook = xApp.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
string excelSheet = xSheet.Name;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [" + excelSheet + "$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds);
return ds;
}
catch (Exception ex)
{
MessageBox.Show("导入出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}