SGGL_JT/SUBQHSE/BLL/Common/libreOfficeHelp.cs

123 lines
5.1 KiB
C#
Raw Normal View History

2025-04-07 17:43:30 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class libreOfficeHelp
{
public static string getLibreOfficePath()
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
return "/usr/bin/soffice";
case PlatformID.Win32NT:
//string binaryDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string binaryDirectory = Funs.RootPath;
return (binaryDirectory + @"\bin\LibreOffice\program\soffice.exe").Replace("\\", @"\");
default:
throw new PlatformNotSupportedException("你的系统暂不支持!");
}
}
public static void ToPdf(string officePath, string outPutPath)
{
using (Process p = new Process())
{
string libreOfficePath = getLibreOfficePath();
string cmdpath = "cmd.exe";
string eout = null;
string cmdstr = string.Format("soffice--headless --convert-to pdf {0} --outdir {1} ", officePath, outPutPath);
ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, cmdstr);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo = procStartInfo;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => { eout += e.Data; });
p.Start();
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}
//获取libreoffice命令的路径
// string libreOfficePath = getLibreOfficePath();
// string cmdpath = @"C:\Windows\System32\cmd.exe";
//string cmdstr = libreOfficePath + "| " + string.Format("soffice--headless --convert-to pdf {0} --outdir {1} ", officePath, outPutPath);
//string libreOfficePath = @"C:\Program Files\LibreOffice\program\soffice.exe";
//ProcessStartInfo procStartInfo = new ProcessStartInfo(cmdpath, cmdstr);
//procStartInfo.RedirectStandardOutput = true;
//procStartInfo.UseShellExecute = false;
//procStartInfo.CreateNoWindow = true;
//procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
////开启线程
//Process process = new Process() { StartInfo = procStartInfo, };
//process.Start();
//process.WaitForExit();
//process.Close();
//if (process.ExitCode != 0)
//{
// throw new LibreOfficeFailedException(process.ExitCode);
//}
}
public static void ToHtml(string officePath, string outPutPath)
{
using (Process p = new Process())
{
string libreOfficePath = getLibreOfficePath();
string eout = null;
string cmdstr = string.Format("soffice--headless --convert-to html {0} --outdir {1} ", officePath, outPutPath);
ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, cmdstr);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo = procStartInfo;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => { eout += e.Data; });
p.Start();
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}
////获取libreoffice命令的路径
//string libreOfficePath = getLibreOfficePath();
////string libreOfficePath = @"C:\Program Files\LibreOffice\program\soffice.exe";
//ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, string.Format("soffice--headless --convert-to html {0} --outdir {1} ", officePath, outPutPath));
//procStartInfo.RedirectStandardOutput = true;
//procStartInfo.UseShellExecute = false;
//procStartInfo.CreateNoWindow = true;
//procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
////开启线程
//Process process = new Process() { StartInfo = procStartInfo, };
//process.Start();
//process.WaitForExit();
//if (process.ExitCode != 0)
//{
// throw new LibreOfficeFailedException(process.ExitCode);
//}
}
}
public class LibreOfficeFailedException : Exception
{
public LibreOfficeFailedException(int exitCode)
: base(string.Format("LibreOffice错误 {0}", exitCode))
{ }
}
}