示例代码:利用Sps提供的remoting服务,把office文档在线转换成html文档(包括页面图片的文件)

类别:.NET开发 点击:0 评论:0 推荐:


作者主页

using System;
using System.IO;
using Microsoft.HtmlTrans;

namespace ToHtmlLibrary.Core
{
 /// <summary>
 /// 把Office文档转换成html文档
 /// </summary>
 public class TransToHtml
 {
  #region Field
  private string m_SavedHtmlFileName;
  private string m_SavedPhysicalPath;
  private string m_ServiceUrl;
  #endregion
        #region Property
  public string SavedHtmlFileName
  {
   get
   {
    return m_SavedHtmlFileName;
   }
   set
   {
    m_SavedHtmlFileName = value;
   }
  }
  public string SavedPhysicalPath
  {
   get
   {
    return m_SavedPhysicalPath;
   }
   set
   {
    m_SavedPhysicalPath  = value;
   }
  }
  public string ServiceUrl
  {
   set
   {
    m_ServiceUrl = value;
   }
   get
   {
    return m_ServiceUrl;
   }
  }
  #endregion
  #region Constructor
  public TransToHtml()
  {
  }
  /// <summary>
  ///
  /// </summary>
  /// <param name="savePath">转换成的html文件存放的物理路径</param>
  /// <param name="mainFileName">html文件的文件名(不包括扩展名)</param>
  public TransToHtml(string savePath,string mainFileName)
  {
   SavedPhysicalPath = savePath;
   SavedHtmlFileName = mainFileName;
  }
     /// <summary>
     ///
     /// </summary>
     /// <param name="savePath"></param>
     /// <param name="mainFileName"></param>
     /// <param name="serviceUrl">转换remoting服务的url</param>
  public TransToHtml(string savePath,string mainFileName,string serviceUrl)
  {
   SavedPhysicalPath = savePath;
   SavedHtmlFileName = mainFileName;
   ServiceUrl = serviceUrl;
  }
  #endregion
  #region Private Method
  private string GetVirtualDocumentName(EDocumentType type)
  {
   if(type.Equals(EDocumentType.Word))
    return "abc.doc";
   if(type.Equals(EDocumentType.Excel))
    return "abc.xls";
   if(type.Equals(EDocumentType.PowerPoint))
    return "abc.ppt";
   return "";
  }
  private void DoTrans(byte[] docContent,string serviceUrl ,EDocumentType type)
  {
   if (type.Equals(EDocumentType.Unknow))
   {
    return ;
   }
   string strTask = Guid.NewGuid().ToString();
   string strDocument = GetVirtualDocumentName(type);
      
   //创建IHtmlTrLoadBalancer对象
   IHtmlTrLoadBalancer htmlTrLoadBalancer =  (IHtmlTrLoadBalancer)System.Activator.GetObject(
    typeof(IHtmlTrLoadBalancer),
    serviceUrl);
   //获取IHtmlTrLauncher对象的URI
   string strLauncherUri = htmlTrLoadBalancer.StrGetLauncher(strTask);
   //创建IHtmlTrLauncher对象
   IHtmlTrLauncher htmlTrLauncher = (IHtmlTrLauncher)
    System.Activator.GetObject(typeof(IHtmlTrLauncher),
    strLauncherUri);
   //调用IHtmlTrLauncher对象生成html
   CreateHtmlInfo chi = htmlTrLauncher.CHICreateHtml(strLauncherUri,
    docContent,BrowserType.BT_IE4, strDocument, strTask, 90, true);
   //判断IHtmlTrLoadBalancer对象的任务是否完成
   htmlTrLoadBalancer.LauncherTaskCompleted(strLauncherUri, strTask);
   //   //检查错误后输出结果
   if(chi.ce == CreationErrorType.CE_NONE && chi.fHasMainFile)
   {
    Directory.CreateDirectory(SavedPhysicalPath + "\\" + SavedHtmlFileName);
    FileStream fs = new FileStream(SavedPhysicalPath + "\\" + SavedHtmlFileName + "\\" + SavedHtmlFileName +
     chi.strMainFileName.Substring(chi.strMainFileName.LastIndexOf(".")),FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    bw.Write(chi.rgbMainFile,0,chi.rgbMainFile.Length);
    fs.Close();
    fs = null;
    bw.Close();
    bw = null;
    if (chi.fHasThicket)
    {
     string supportFilePath = string.Concat(chi.strMainFilePath, chi.strThicketFolderName, "/");
     for (int i=0;i<chi.rgrgbThicketFiles.Length;i++)
     {
      Directory.CreateDirectory(SavedPhysicalPath + "\\" + SavedHtmlFileName + "\\" +
       "HtmlView.files");
      fs = new FileStream(SavedPhysicalPath + "\\" + SavedHtmlFileName + "\\" +
       "HtmlView.files" + "\\" + chi.rgstrThicketFileNames[i],FileMode.Create);
      bw = new BinaryWriter(fs);
      bw.Write(chi.rgrgbThicketFiles[i],0,chi.rgrgbThicketFiles[i].Length);
      fs.Close();
      fs = null;
      bw.Close();
      bw = null;
     }
    }
   }
   else
   {
    throw new Exception(chi.ce.ToString() );
   }
  } 
  #endregion
  #region Public Method
  public void Trans(byte[] docContent,EDocumentType type)
  {
   Trans(docContent,ServiceUrl,type);
  }
  public void Trans(byte[] docContent,string serviceUrl ,EDocumentType type)
  {
   DoTrans(docContent,serviceUrl ,type);
  }
  #endregion
 }
}

本文地址:http://com.8s8s.com/it/it42812.htm