【发布时间】:2017-03-22 07:11:22
【问题描述】:
我一直在使用此代码读取一个 excel 文件,当我尝试读取保存的 excel 文件时它工作正常
string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;" + @"Extended Properties='Excel 8.0;HDR=Yes;'";
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]",
connection);
using(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
}
}
}
打开一个每秒更新数据的 excel 文件的应用程序,这是应用程序的图片
在这里您可以看到正在任务栏中打开的 excel 文件。当我使用过程代码使用此代码读取 excel 文件的路径时,我没有得到任何数据,因为位于该路径中的文件是空的
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == "EXCEL")
{
Console.WriteLine(theprocess.ProcessName, theprocess.Id);
string fullPath = theprocess.MainModule.FileName;
//fullpath = C:\\Program Files (x86)\\Microsoft Office\\Office12\\EXCEL.EXE
}
}
应用程序可能直接使用 ms excel 的实例。是否有任何替代步骤可以直接通过进程 ID 而不是路径读取此 excel 文件?
提前致谢。
【问题讨论】:
-
你有没有想过更新excel文件的应用程序也会锁定它?
-
它没有被锁定。它每秒更新一次