Importword.cs
上传用户:lanchensha
上传日期:2022-02-27
资源大小:7530k
文件大小:8k
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Web;
- using System.IO;
- using System.Text;
- using System.Web.UI;
- using System.Text.RegularExpressions;
- using DotNetTextBox;
- namespace Word_dntb
- {
- #region word文档导入编辑器的功能插件
- /// <summary>
- /// word文档导入编辑器的类
- /// </summary>
- public partial class Importword : System.Web.UI.Page
- {
- string wordfile = "";
- public string uploadpath = "";
- protected System.Web.UI.WebControls.HiddenField worddoc;
- protected System.Web.UI.WebControls.FileUpload FileUpload1;
- protected System.Web.UI.WebControls.CheckBox saveword;
- protected System.Web.UI.WebControls.Button btnUpload;
- protected System.Web.UI.WebControls.Button canceloading;
- /// <summary>
- /// 页面的初始化
- /// </summary>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request.Cookies["uploadFolder"] != null)
- {
- uploadpath = Request.Cookies["uploadFolder"].Value.ToLower();
- }
-
- if (!IsPostBack)
- {
- Response.Expires = -1;
- if (Request.Cookies["languages"] != null)
- {
- ResourceManager.SiteLanguageKey = Request.Cookies["languages"].Value;
- }
- else
- {
- ResourceManager.SiteLanguageKey = HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToLower().Split(',')[0];
- }
- btnUpload.Text = ResourceManager.GetString("importeditor");
- saveword.Text = ResourceManager.GetString("saveword");
- canceloading.Text = ResourceManager.GetString("canceloading");
- }
- }
- /// <summary>
- /// 转换word文档为html代码并插入编辑器
- /// </summary>
- protected void wordToHtml()
- {
- Word.ApplicationClass word = new Word.ApplicationClass();
- Type wordType = word.GetType();
- Word.Documents docs = word.Documents;
- // 打开文件
- Type docsType = docs.GetType();
- //应当先把文件上传至服务器然后再解析文件为html
- object fileName = wordfile;
- Word.Document doc = (Word.Document)docsType.InvokeMember("Open",
- System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });
- // 转换格式,另存为html
- Type docType = doc.GetType();
- string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
- System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
- //被转换的html文档保存的位置
- string ConfigPath = HttpContext.Current.Server.MapPath(uploadpath + filename + ".html");
- object saveFileName = ConfigPath;
- docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
-
- //docType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
- // 退出 Word
- wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,word,null);
- string line;
- StringBuilder strhtml = new StringBuilder();
- try
- {
- StreamReader sr = new StreamReader(ConfigPath, System.Text.Encoding.Default);
- while ((line = sr.ReadLine()) != null)
- {
- strhtml.Append(line);
- }
- sr.Close();
- }
- catch
- {
- wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
- StreamReader sr = new StreamReader(ConfigPath, System.Text.Encoding.Default);
- while ((line = sr.ReadLine()) != null)
- {
- strhtml.Append(line);
- }
- sr.Close();
- }
- string photoname;
- string content = getBody(strhtml.ToString()).Replace("src=", " src=").Replace(filename + ".files/", Request.CurrentExecutionFilePath.Replace("importword.aspx", "") + uploadpath);
- if (Directory.Exists(Server.MapPath(uploadpath + filename + ".files/")))
- {
- DirectoryInfo dir = new DirectoryInfo(Server.MapPath(uploadpath + filename + ".files/"));
- FileInfo[] fis = dir.GetFiles();
- foreach (FileInfo fi in fis)
- {
- //if (!File.Exists(Server.MapPath(uploadpath + fi.Name)))
- //{
- photoname = filename + fi.Name.Replace(fi.Extension,"");
- photoname = photoname + fi.Extension.ToLower();
- content = content.Replace(fi.Name, photoname);
- fi.MoveTo(Server.MapPath(uploadpath + photoname));
- //}
- }
- Directory.Delete(Server.MapPath(uploadpath + filename + ".files/"), true);
- }
- worddoc.Value = content;
- if (!saveword.Checked)
- {
- File.Delete(wordfile);
- }
- File.Delete(ConfigPath);
- }
- /// <summary>
- /// 处理上传word文件的操作
- /// </summary>
- public bool uploadWord()
- {
- if (FileUpload1.HasFile)
- {
- string fileName = FileUpload1.PostedFile.FileName;
- FileInfo file = new FileInfo(fileName);
- string extendName = file.Extension.ToLower();
- try
- {
- if (extendName == ".doc")
- {
- DateTime now = DateTime.Now;
- wordfile = System.Web.HttpContext.Current.Server.MapPath(uploadpath + now.DayOfYear.ToString() + FileUpload1.PostedFile.ContentLength.ToString() + extendName);
- if (!File.Exists(wordfile))
- {
- FileUpload1.PostedFile.SaveAs(wordfile);
- }
- }
- else
- {
- ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("errortype") + "')", true);
- return false;
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 过滤页头和页尾
- /// </summary>
- public string getBody(string html)
- {
- string resultStr = "";
- Regex re = new Regex(@"<body[^>]s*[^>]*>(?<text>.*?)</body>", RegexOptions.IgnoreCase);
- MatchCollection mc = re.Matches(html);
- foreach (Match m in mc)
- {
- resultStr += m.Groups["text"].Value;
- }
- return resultStr;
- }
- /// <summary>
- /// 处理上传按键的操作
- /// </summary>
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- if (uploadpath != "")
- {
- if (uploadWord())
- {
- wordToHtml();
- ClientScript.RegisterStartupScript(typeof(Page), "Key", "addeditor();", true);
- }
- }
- }
- }
- #endregion
- }