我正在与'excelcnv.exe'合作,将'.xls'文件转换为'.xlsx'版本。我可以通过直接在我现在工作的机器中的“excelcnv.exe”位置来实现这一点。
在我当前的机器中,Office 2016已经安装,我直接引用了“excelcnv.exe”位置。目前,它位于'C:\Program (x86)\Microsoft \root\Office16‘位置,用于Office 2016版本。这取决于office版本和office安装的位置()。
mstrExtAddedFilePath = pstrFilePath.Replace(".xls", ".xlsx");
string processFilePath = @"C:\Program Files (x86)\Microsoft Office\root\Office16\excelcnv.exe";
string processArguments = "-oice \"" + pstrFilePath + "\" \"" + mstrExtAddedFilePath + "\"";
Process process = new Process();
process.StartInfo.FileName = processFilePath;
process.StartInfo.Arguments = processArguments;
process.Start();
但是,我希望在部署DLL的机器上获得'excelcnv.exe‘动态的位置。部署环境可以安装32位或64位office,甚至不同版本的office (2010 /2013 /2016)。
怎么弄到这个?在注册表中是否有可用的键设置?
还是 excelcnv.exe的其他替代方案来将文件转换为“.xlsx”格式?
发布于 2018-08-24 08:10:38
我只需使用GetFiles()
搜索该文件的所有目录,例如
var files = Directory.GetFiles(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"excelcnv.exe",
SearchOption.AllDirectories);
如果只有一个匹配项,您可以非常肯定它是正确的,否则您可以检查完整路径的某些模式,例如,必须在完整路径中有Office。
编辑:围绕这一点包装一些逻辑。保存到用户设置的路径,并在每次运行时从那里加载。如果exe文件不存在(File.Exist()
),则启动新搜索。
发布于 2018-08-24 12:34:23
我尝试了以下方式,以避免文件访问问题和搜索32位已安装和64位已安装版本。
var lcolExcelCnvEXEfiles = new List<string>();
lcolExcelCnvEXEfiles = GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "excelcnv.exe");
if (lcolExcelCnvEXEfiles.Count == 0)
{
lcolExcelCnvEXEfiles = GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "excelcnv.exe");
}
//Get files method that recursively search files
private List<string> GetFiles(string path, string pattern)
{
var files = new List<string>();
try
{
files.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly));
foreach (var directory in Directory.GetDirectories(path))
files.AddRange(GetFiles(directory, pattern));
}
catch (UnauthorizedAccessException) { }
return files;
}
https://stackoverflow.com/questions/51999697
复制相似问题