111,087
社区成员




private static void Test()
{
Console.Read();
using (var con=new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'",@"F:/1.xls")))
{
con.Open();
con.Close();
}
GC.Collect();
Console.Read();
}
private static void KillExcel()
{
Process killer = new Process();
try
{
foreach (Process thisproc in Process.GetProcessesByName("EXCEL")) //循环查找
{
string str = thisproc.MainWindowTitle; //发现程序中打开跟用户自己打开的区别就在这个属性
//用户打开的str 是文件的名称,程序中打开的就是空字符串
if (string.IsNullOrEmpty(str))
{
thisproc.Kill();
}
}
}
catch
{
string msg = "关闭失败";
MessageBox.Show(msg);
}
}
有没有看进程里是否多了很多EXCEL进程?每次操作完杀死多余的没有界面的EXCEL进程试试.
郑笑彬 08:35:05
public DataSet ImportFromExcel(string FilePath)
{
System.Data.DataTable dt = new System.Data.DataTable();
ArrayList TablesList = new ArrayList();
System.Data.DataSet ds = new DataSet();
OleDbConnection dbcon = null;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
string ss = excelApp.Version;
switch (ss)
{
case "12.0":
dbcon = new OleDbConnection("Provider=Microsoft.ACE." + "OLEDB.12.0;Extended Properties=\"Excel 8.0\";Data Source=" + FilePath);
break;
default:
dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0");
break;
}
try
{
TablesList = GetExcelTables(FilePath);
try
{
for (int i = 0; i < TablesList.Count; i++)
{
string str = "select * from [" + TablesList[i] + "$]";
System.Data.DataTable table;
OleDbCommand cmd = new OleDbCommand(str, dbcon);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
table = new System.Data.DataTable(TablesList[i].ToString());
adapter.Fill(table);
ds.Tables.Add(table);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "读取EXCEL工作簿出错");
}
}
finally
{
}
return ds;
}
使用Microsoft.Office.Interop.Excel.Apllication可以直接读取整个文档到dataset啊
也可以指定单个sheet读取到datatable
怎么可能只能按Range读取呢