gpt4 book ai didi

c# - asp.net 网络服务使用 office 2010 COM

转载 作者:行者123 更新时间:2023-11-30 16:28:01 25 4
gpt4 key购买 nike

我正在编写 Web 服务并想将 .docx 或 .doc 更改为 .xps。我正在使用 office com 帮助我保存为 .xps 格式,如下所示:

        [WebMethod]
public string GetDocPreviewUrl(string m_userName, string m_orgFileName)
{
string m_returnUrl = "";

string m_orgFilePath = _currentDirectory + "\\" + m_userName + "\\" + m_orgFileName;
if (File.Exists(m_orgFilePath))
{
string m_xpsFilePath = _currentDirectory + "\\" + m_userName + "\\" +
Path.GetFileNameWithoutExtension(m_orgFileName) + ".xps";

OfficeToXpsConversionResult m_converstionResult = OfficeToXps.ConvertToXps(m_orgFilePath, ref m_xpsFilePath);

m_returnUrl = _baseUrl + m_userName + "/"+ Path.GetFileName(m_xpsFilePath);
}
return m_returnUrl;
}

private static OfficeToXpsConversionResult ConvertFromWord(string sourceFilePath, ref string resultFilePath)
{
object pSourceDocPath = sourceFilePath;

string pExportFilePath = string.IsNullOrWhiteSpace(resultFilePath) ? GetTempXpsFilePath() : resultFilePath;


Word.Application() wordApplication = new Word.Application();

//wordDocument = wordApplication.Documents.Open(ref pSourceDocPath);
dynamic wordDocument = wordApplication.Documents.Add(pSourceDocPath);

//return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc);

if (wordDocument != null)
{
wordDocument.SaveAs(pExportFilePath, WdSaveFormat.wdFormatXPS);
}

resultFilePath = pExportFilePath;

return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath);
}

但是,当我尝试通过网络方法调用时出现异常:

System.Runtime.InteropServices.COMException: Word 發生問題。 at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message) at CallSite.Target(Closure , CallSite , ComObject , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at CallSite.Target(Closure , CallSite , Object , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath, String& resultFilePath) in C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs:line 145 at DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath) in C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\OfficeHelper\OfficeToXps.cs:line 63 at DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName) in C:\Users\Icicle\Documents\Fotomax WP7\DocProcessService\DocProcessService\DocDownload.asmx.cs:line 90

使用office另存为xps的代码在我的WPF项目中运行良好。如何在我的 asp.net 4.0 网络服务中使用它?谢谢。

最佳答案

从 Windows 服务(例如 IIS 和 ASP.NET)使用自动化是 NOT supported by MS !

根据您的需要,您可以为此使用一些库(免费的或商业的):

关于c# - asp.net 网络服务使用 office 2010 COM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7382704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com