导入Excel到数据库中是一个常见的操作,这里采用两种实现,一种比较简洁,一种侧重效率。
这里说明一下:导入的Excel可以包含多个sheet,最终都导入到dataset的同一个table中。
一、简洁实现:
-
-
-
-
-
-
public DataSet ExcelToDataSet(string strFileSourse)
-
{
-
DataSet ds = new DataSet();
-
-
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileSourse + ";
-
Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
-
-
-
-
OleDbConnection conn = new OleDbConnection(strConn);
-
try
-
{
-
conn.Open();
-
-
-
-
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null,
-
"TABLE" });
-
-
-
-
-
-
for (int i = 0; i < dtSheet.Rows.Count; i++)
-
{
-
string strSheetName = dtSheet.Rows[i]["TABLE_NAME"].ToString();
-
OleDbDataAdapter OleDa = new OleDbDataAdapter("Select × from [" + strSheetName + "]", conn);
-
OleDa.Fill(ds, "TableName");
-
conn.Close();
-
}
-
}
-
catch (Exception)
-
{
-
-
throw;
-
}
-
finally
-
{
-
if (conn.State!=ConnectionState.Closed)
-
{
-
conn.Close();
-
}
-
}
-
-
return ds;
-
}
二、侧重效率:
-
-
-
-
-
-
public DataSet ImportExcel(string strFileUrl)
-
{
-
-
-
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
-
-
afterTime = DateTime.Now;
-
if (xlApp==null)
-
{
-
returnstatue = -1;
-
returnmessage = "无法创建Microsoft.Office.Interop.Excel.Application对象,请先检查您的计算机是否安装了Office!";
-
return null;
-
}
-
-
-
Microsoft.Office.Interop.Excel.Workbook workBook;
-
try
-
{
-
workBook = xlApp.Workbooks.Open(strFileUrl, 0, false, 5, "", "", false,
-
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
-
-
}
-
catch (Exception)
-
{
-
-
returnstatue = -1;
-
returnmessage = "Excel文件处于打开状态,请保存您的文件后关闭!";
-
return null;
-
}
-
-
-
int intSheetCount = workBook.Worksheets.Count;
-
-
string[] SheetSet = new string[intSheetCount];
-
-
-
for (int i = 0; i < intSheetCount; i++)
-
{
-
SheetSet[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[i]).Name;
-
-
}
-
-
-
workBook.Close(null, null, null);
-
-
xlApp.Quit();
-
-
-
if (workBook!=null)
-
{
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
-
workBook = null;
-
-
}
-
-
if (xlApp!=null)
-
{
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
-
xlApp = null;
-
}
-
-
-
GC.Collect();
-
-
-
DataSet ds = new DataSet();
-
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileUrl + ";
-
Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
-
using (OleDbConnection conn = new OleDbConnection(strConn))
-
{
-
conn.Open();
-
OleDbDataAdapter da;
-
for (int i = 0; i < intSheetCount; i++)
-
{
-
string sql = "Select × from ["+SheetSet[i-1]+"+$]";
-
da = new OleDbDataAdapter(sql, conn);
-
-
da.Fill(ds, "Table");
-
da.Dispose();
-
}
-
conn.Close();
-
conn.Dispose();
-
}
-
-
-
-
Kill(xlApp);
-
return ds;
-
}
销毁进程:
-
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
-
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
-
-
-
-
-
private void Kill(Microsoft.Office.Interop.Excel.Application excel)
-
{
-
-
IntPtr t = new IntPtr(excel.Hwnd);
-
-
int k = 0;
-
-
GetWindowThreadProcessId(t, out k);
-
-
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
-
-
p.Kill();
-
}
以上是两种不同的实现,第二种更多的调用了操作系统的接口,这里只限于是Windows操作系统,包括销毁进程的方法,也是调用了系统的接口。统的接口。