方法1:调用com组件,导出access数据到Excel,就是直接调用access的导出功能,此方法速度超级快
using Access;
Access.ApplicationClass oAccess = new Access.ApplicationClass();
oAccess.Visible = false;
try
{
//ACCESS9:
oAccess.OpenCurrentDatabase("d:\\wcf.mdb",false,"");
//导出到excel
oAccess.DoCmd.TransferSpreadsheet(Access.AcDataTransferType.acExport,Access.AcSpreadSheetType.acSpreadsheetTypeExcel9,"工作表名","d:\\wcf.xls",true,null,null);
//导入txt
// oAccess.DoCmd.TransferText(Access.AcTextTransferType.acExportDelim,"","Enterprise","d:\\wcf.txt",true,"",0);
oAccess.CloseCurrentDatabase();
oAccess.DoCmd.Quit(Access.AcQuitOption.acQuitSaveNone);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess);
oAccess = null;
MessageBox.Show("导入成功");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
GC.Collect();
}
方法2:此方法速度也是超级快,只不过导出的格式非标准的Excel格式,默认工作表名与文件名相同
string FileName="d:\\abc.xls";
System.Data.DataTable dt=new System.Data.DataTable();
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine="";
objFileStream = new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Unicode);
for(int i=0;i<dt.Columns.Count;i++)
{
strLine=strLine+dt.Columns[i].ColumnName.ToString()+Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine="";
for(int i=0;i<dt.Rows.Count;i++)
{
strLine=strLine+(i+1)+Convert.ToChar(9);
for(int j=1;j<dt.Columns.Count;j++)
{
strLine=strLine+dt.Rows[i][j].ToString()+Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine="";
}
objStreamWriter.Close();
objFileStream.Close();
方法3:用Ado.net 此方法速度较以上两个显得慢了一些,数据量越大越明显
int Id=0;
&n
|