专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

Aspose.Words.dll 将 Word 转换成 html

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:36:57       共计:3619 浏览

用于网站上,上传 Word 文档后显示文档内容(可看作在线阅读)。代码适用于 .net 2.0 或以上版本

(使用的未注册 Aspose.Words.dll 并尝试消除试用标志) 下载地址


string fileNameOut = string.Empty;
try {
    using (OpenFileDialog fd = new OpenFileDialog()) {
        fd.Filter = "Word 文档(*.doc,*.docx)|*.doc;*.docx|所有文件(*.*)|*.*";
        if (fd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) {
            fileNameOut = "D:\\html\\" + Path.GetFileNameWithoutExtension(fd.FileName) + ".html";
            using (FileStream fs = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                var doc = new Document(fs);
                var saveOptions = new HtmlSaveOptions();
                saveOptions.SaveFormat = SaveFormat.Html;
                saveOptions.ExportPageMargins = true;
                doc.Save(fileNameOut, saveOptions);
            }
        }
    }
}
catch (Exception ex) {
    throw ex;
}
if (!string.IsNullOrEmpty(fileNameOut)) {
    string fileText = File.ReadAllText(fileNameOut, Encoding.UTF8);
    string moreText = "<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; font-size:12pt\"><span style=\"font-family:'Times New Roman'; font-weight:bold; color:#ff0000\">还有部分内容未展示,请下载阅读。</span></p>";
    string fileTextNew = Regex.Replace(fileText, "<p style=\".+?\"><span style=\".+?\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2017 Aspose Pty Ltd.</span></p>", "");

    File.WriteAllText(fileNameOut,
        //fileText.Replace("<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; widows:0; orphans:0; font-size:12pt\"><span style=\"font-family:'Times New Roman'; font-weight:bold; color:#ff0000\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2017 Aspose Pty Ltd.</span></p>", "")
        //        .Replace("<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; widows:0; orphans:0; font-size:12pt\"><span style=\"font-family:Calibri; font-weight:bold; color:#ff0000\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2017 Aspose Pty Ltd.</span></p>", "")
        fileTextNew.Replace("<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; font-size:12pt\"><span style=\"font-family:'Times New Roman'; font-weight:bold; color:#ff0000\">This document was truncated here because it was created in the Evaluation Mode.</span></p>", moreText)
        .Replace("<p style=\"margin-top:0pt; margin-bottom:0pt; text-align:justify; widows:0; orphans:0; font-size:12pt\"><span style=\"font-family:'Times New Roman'; font-weight:bold; color:#ff0000\">This document was truncated here because it was created in the Evaluation Mode.</span></p>", moreText), Encoding.UTF8);
    MessageBox.Show(this, "导出 html 成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    Process.Start(fileNameOut);
}

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# 将Word文档转换为HTML | ·下一条:使用FlexPaper实现office文件的预览(C#版)

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有