由于工作需要,改功能已测试OK!
#region 导入word到编辑器
//Html文件名
private string _htmlFileName;
///
/// 上传Word文档
///
///
///
private string UpLoadFile(HtmlInputFile inputFile)
{
string fileName, fileExtension;
UploadedFile file = RadUploadContext.Current.UploadedFiles[0];
//string Path = Server.MapPath(@"Uploads");
////如果路径不存在,则创建
//if (System.IO.Directory.Exists(Path) == false)
//{
// System.IO.Directory.CreateDirectory(Path);
//}
////组合路径,file.GetName()取得文件名
//Path = Path + "/" + file.GetName().ToString();
//file.SaveAs(Path, true);
fileName = file.GetName();
//建立上传对象
//HttpPostedFile postedFile = inputFile.PostedFile;
//fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
string phyPath = Server.MapPath("~/") + "Portals\\0\\WordImport\\";
//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(phyPath);
if (!upDir.Exists)
{
upDir.Create();
}
//保存文件
try
{
file.SaveAs(phyPath + fileName);
}
catch
{
}
return phyPath + fileName;
}
///
/// word转成html
///
///
private string WordToHtml(object wordFileName)
{
//在此处放置用户代码以初始化页面
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式,另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
/*
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[]{saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML});
*/
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
null, word, null);
return saveFileName.ToString();
}
///
/// 读取html文件,返回字符串
///
///
///
private string getHtml(string strHtmlFileName)
{
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(strHtmlFileName, encoding);
string str = sr.ReadToEnd();
sr.Close();
return str;
}
///
///
///
///
///
private void findUsedFromHtml(string strHtml, string strFileName)
{
string strStyle;
string strBody;
// stytle 部分
int index = 0;
int intStyleStart = 0;
int intStyleEnd = 0;
while (index { int intStyleStartTmp = strHtml.IndexOf("", intContentEndTmp); if (intStyleEndTmp - intCOntentEndTmp== 5) { intStyleEnd = intStyleEndTmp; break; } else { index = intContentEndTmp + 4; } } strStyle = strHtml.Substring(intStyleStart, intStyleEnd - intStyleStart + 8); // Body部分 int bodyStart = strHtml.IndexOf(" int bodyEnd = strHtml.IndexOf(""); strBody = strHtml.Substring(bodyStart, bodyEnd - bodyStart + 7); //替换图片地址 string fullName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1); string strOld = fullName.Replace("doc", "files"); string strNew = Page.Request.ApplicationPath + "/Portals/0/WordImport/" + strOld; strBody = strBody.Replace(strOld, strNew); strBody = strBody.Replace("v:imagedata", "img"); strBody = strBody.Replace("", ""); //this.TextBox1.Text = strBody; // this.TextArea1.InnerText = strStyle; this.content.Value = strStyle + strBody; } #endregion 导入Word到编辑器