代码示例:调用SPS提供的remoting服务,在线把Office文档转换成html文档


可以把office文档中的图片等文件一齐转换,效果不错

None.gif using  System;
None.gif
None.gif
namespace  ToHtmlLibrary
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// EDocumentType 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum EDocumentType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Word,
InBlock.gif        Excel,
InBlock.gif        PowerPoint,
InBlock.gif        Unknow
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


None.gif using  System;
None.gif
using  System.IO;
None.gif
using  Microsoft.HtmlTrans;
None.gif
None.gif
namespace  ToHtmlLibrary.Core
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 把Office文档转换成html文档
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class TransToHtml
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Field#region Field 
InBlock.gif        
private string m_SavedHtmlFileName;
InBlock.gif        
private string m_SavedPhysicalPath;
InBlock.gif        
private string m_ServiceUrl;
ExpandedSubBlockEnd.gif        
#endregion
 
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Property#region Property
InBlock.gif        
public string SavedHtmlFileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return m_SavedHtmlFileName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_SavedHtmlFileName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string SavedPhysicalPath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return m_SavedPhysicalPath;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_SavedPhysicalPath  
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string ServiceUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_ServiceUrl 
= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return m_ServiceUrl;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructor#region Constructor 
InBlock.gif        
public TransToHtml()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="savePath">转换成的html文件存放的物理路径</param>
ExpandedSubBlockEnd.gif        
/// <param name="mainFileName">html文件的文件名(不包括扩展名)</param>

InBlock.gif        public TransToHtml(string savePath,string mainFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SavedPhysicalPath 
= savePath;
InBlock.gif            SavedHtmlFileName 
= mainFileName;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="savePath"></param>
InBlock.gif        
/// <param name="mainFileName"></param>
ExpandedSubBlockEnd.gif        
/// <param name="serviceUrl">转换remoting服务的url</param>

InBlock.gif        public TransToHtml(string savePath,string mainFileName,string serviceUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SavedPhysicalPath 
= savePath;
InBlock.gif            SavedHtmlFileName 
= mainFileName;
InBlock.gif            ServiceUrl 
= serviceUrl;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Private Method#region Private Method
InBlock.gif        
private string GetVirtualDocumentName(EDocumentType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(type.Equals(EDocumentType.Word))
InBlock.gif                
return "abc.doc";
InBlock.gif            
if(type.Equals(EDocumentType.Excel))
InBlock.gif                
return "abc.xls";
InBlock.gif            
if(type.Equals(EDocumentType.PowerPoint))
InBlock.gif                
return "abc.ppt";
InBlock.gif            
return "";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void DoTrans(byte[] docContent,string serviceUrl ,EDocumentType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (type.Equals(EDocumentType.Unknow))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string strTask = Guid.NewGuid().ToString();
InBlock.gif            
string strDocument = GetVirtualDocumentName(type);
InBlock.gif                        
InBlock.gif            
//创建IHtmlTrLoadBalancer对象
InBlock.gif
            IHtmlTrLoadBalancer htmlTrLoadBalancer =  (IHtmlTrLoadBalancer)System.Activator.GetObject(
InBlock.gif                
typeof(IHtmlTrLoadBalancer),
InBlock.gif                serviceUrl);
InBlock.gif            
//获取IHtmlTrLauncher对象的URI
InBlock.gif
            string strLauncherUri = htmlTrLoadBalancer.StrGetLauncher(strTask);
InBlock.gif            
//创建IHtmlTrLauncher对象
InBlock.gif
            IHtmlTrLauncher htmlTrLauncher = (IHtmlTrLauncher)
InBlock.gif                System.Activator.GetObject(
typeof(IHtmlTrLauncher), 
InBlock.gif                strLauncherUri);
InBlock.gif            
//调用IHtmlTrLauncher对象生成html
InBlock.gif
            CreateHtmlInfo chi = htmlTrLauncher.CHICreateHtml(strLauncherUri, 
InBlock.gif                docContent,BrowserType.BT_IE4, strDocument, strTask, 
90true);
InBlock.gif            
//判断IHtmlTrLoadBalancer对象的任务是否完成
InBlock.gif
            htmlTrLoadBalancer.LauncherTaskCompleted(strLauncherUri, strTask);
InBlock.gif            
//            //检查错误后输出结果
InBlock.gif
            if(chi.ce == CreationErrorType.CE_NONE && chi.fHasMainFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Directory.CreateDirectory(SavedPhysicalPath 
+ "\\" + SavedHtmlFileName);
InBlock.gif                FileStream fs 
= new FileStream(SavedPhysicalPath + "\\" + SavedHtmlFileName + "\\" + SavedHtmlFileName +
InBlock.gif                    chi.strMainFileName.Substring(chi.strMainFileName.LastIndexOf(
".")),FileMode.Create);
InBlock.gif                BinaryWriter bw 
= new BinaryWriter(fs);
InBlock.gif                bw.Write(chi.rgbMainFile,
0,chi.rgbMainFile.Length);
InBlock.gif                fs.Close();
InBlock.gif                fs 
= null;
InBlock.gif                bw.Close();
InBlock.gif                bw 
= null;
InBlock.gif                
if (chi.fHasThicket)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string supportFilePath = string.Concat(chi.strMainFilePath, chi.strThicketFolderName, "/");
InBlock.gif                    
for (int i=0;i<chi.rgrgbThicketFiles.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Directory.CreateDirectory(SavedPhysicalPath 
+ "\\" + SavedHtmlFileName + "\\" + 
InBlock.gif                            
"HtmlView.files");
InBlock.gif                        fs 
= new FileStream(SavedPhysicalPath + "\\" + SavedHtmlFileName + "\\" + 
InBlock.gif                            
"HtmlView.files" + "\\" + chi.rgstrThicketFileNames[i],FileMode.Create);
InBlock.gif                        bw 
= new BinaryWriter(fs);
InBlock.gif                        bw.Write(chi.rgrgbThicketFiles[i],
0,chi.rgrgbThicketFiles[i].Length);
InBlock.gif                        fs.Close();
InBlock.gif                        fs 
= null;
InBlock.gif                        bw.Close();
InBlock.gif                        bw 
= null;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception(chi.ce.ToString() );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
    
ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Public Method#region Public Method
InBlock.gif        
public void Trans(byte[] docContent,EDocumentType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Trans(docContent,ServiceUrl,type);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Trans(byte[] docContent,string serviceUrl ,EDocumentType type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoTrans(docContent,serviceUrl ,type);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


我的个人主页

转载于:https://www.cnblogs.com/umlchina/archive/2004/11/17/spstohtml.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值