請教大大~
跑到這行時,一直轉圈圈,跑不出來,可能是哪裡出了問題,謝謝~
var xLocalContext = Bootstrap.bootstrap();
環境
VS2013 C# WebForm
W7
安裝x86LibreOffice
安裝x86LibreOffice的SDK (LibreOffice_6.4.2_Win_x64_sdk.msi)
加入參考
cli_basetypes.dll
cli_cppuhelper.dll
cli_oootypes.dll
cli_ure.dll
cli_uretypes.dll
protected void Button1_Click(object sender, EventArgs e)
{
ConvertToPdfSdk(@"e:\result.xlsx", @"e:\result.pdf");
}
/// <summary>
/// 匯出PDF
/// </summary>
/// <param name="inputFile">來源檔案路徑</param>
/// <param name="outputFile">匯出檔案路徑</param>
public static void ConvertToPdfSdk(string inputFile, string outputFile)
{
if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null)
throw new InvalidProgramException("Unknown file type for OpenOffice. File = " + inputFile);
//Get a ComponentContext
var xLocalContext =
Bootstrap.bootstrap();
//Get MultiServiceFactory
var xRemoteFactory =
(XMultiServiceFactory)
xLocalContext.getServiceManager();
//Get a CompontLoader
var aLoader =
(XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
//Load the sourcefile
XComponent xComponent = null;
try
{
xComponent = InitDocument(aLoader,
PathConverter(inputFile), "_blank");
//Wait for loading
while (xComponent == null)
{
Thread.Sleep(1000);
}
// save/export the document
SaveDocument(xComponent, inputFile, PathConverter(outputFile));
}
finally
{
if (xComponent != null) xComponent.dispose();
}
}
/// <summary>
/// 文件初始化
/// </summary>
/// <param name="aLoader"></param>
/// <param name="file">來源檔案路徑</param>
/// <param name="target">目標檔案路徑</param>
/// <returns></returns>
private static XComponent InitDocument(XComponentLoader aLoader, string file, string target)
{
var openProps = new PropertyValue[1];
openProps[0] = new PropertyValue { Name = "Hidden", Value = new Any(true) };
var xComponent = aLoader.loadComponentFromURL(
file, target, 0,
openProps);
return xComponent;
}
/// <summary>
/// 儲存檔案
/// </summary>
/// <param name="xComponent">套件</param>
/// <param name="sourceFile">來源檔案路徑</param>
/// <param name="destinationFile">目標檔案路徑</param>
private static void SaveDocument(XComponent xComponent, string sourceFile, string destinationFile)
{
var propertyValues = new PropertyValue[2];
// Setting the flag for overwriting
propertyValues[1] = new PropertyValue { Name = "Overwrite", Value = new Any(true) };
//// Setting the filter name
propertyValues[0] = new PropertyValue
{
Name = "FilterName",
Value = new Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)))
};
((XStorable)xComponent).storeToURL(destinationFile, propertyValues);
}
/// <summary>
/// 檔案路徑字串格式
/// </summary>
/// <param name="file">檔案路徑</param>
/// <returns></returns>
private static string PathConverter(string file)
{
if (string.IsNullOrEmpty(file))
throw new NullReferenceException("Null or empty path passed to OpenOffice");
return String.Format("file:///{0}", file.Replace(@"\", "/"));
}
/// <summary>
/// 對應檔案類型
/// </summary>
/// <param name="extension">副檔名</param>
/// <returns></returns>
public static string ConvertExtensionToFilterType(string extension)
{
switch (extension)
{
case ".doc":
case ".docx":
case ".txt":
case ".rtf":
case ".html":
case ".htm":
case ".xml":
case ".odt":
case ".wps":
case ".wpd":
return "writer_pdf_Export";
case ".xls":
case ".xlsb":
case ".xlsx":
case ".ods":
return "calc_pdf_Export";
case ".ppt":
case ".pptx":
case ".odp":
return "impress_pdf_Export";
default:
return null;
}
ref
https://dotblogs.com.tw/WillianHsiaoDotNetBLog/2020/01/07/ExcelToPdf