m0_65996482 2022-05-07 18:00 采纳率: 100%
浏览 87
已结题

asp.net word转html里面的图片无法显示

运行结果就是里面有图片的位置和边框,但是显示不出来,用的是web窗体文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Word = Microsoft.Office.Interop.Word;

namespace WebWordToHtml
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = WordToHtml("d:\test.doc");
StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312"));
string ss = fread.ReadToEnd();
Response.Write(ss);
fread.Close();
fread.Dispose();
}

    /// <summary> 
    /// word转成html 
    /// </summary> 
    /// <param name="wordFileName"></param> 
    private string WordToHtml(object wordFileName)
    {
        //在此处放置用户代码以初始化页面 
        Word.Application word = new Word.Application();
        Type wordType = word.GetType();
        Word.Documents docs = word.Documents;
        //打开文件 
        Type docsType = docs.GetType();
        Word.Document doc = (Word.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;
        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
        docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        //退出 Word 
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        return saveFileName.ToString();
    } 


}

}

  • 写回答

2条回答 默认 最新

  • Yijing Sun 2022-05-09 11:42
    关注

    你好,
    你应该是找的网上的代码吧。直接写字符串到网页会发现,文字可显示,图片、表格无法显示。因此在后面重跳转到html文件页面。建议你看一下下面这个代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using Microsoft.Office.Interop.Word;
    using System.Reflection;
    using System.IO;
    using Word = Microsoft.Office.Interop.Word;
     
    public partial class Content : System.Web.UI.Page
     
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
            string SNo = Session["SNo"].ToString().Trim();
            string data = Session["data"].ToString().Trim();
           // string url = Geturl(SNo, DNo);
            string url = "~/Files/"+SNo+"/"+data+".doc";
            string serverPath = Server.MapPath(url);
            string html = serverPath.Replace(".doc", ".html");
            if (!File.Exists(@html)) //html页面不存在,把word转换成html
            {
                string filename = WordToHtml(serverPath);
                //StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312"));
                //string ss = fread.ReadToEnd();
     
                //Response.Write(ss); //直接写字符串到网页会发现,文字可显示,图片、表格无法显示。因此在后面重跳转到html文件页面。
                //fread.Close();
                //fread.Dispose();
            }
           // else {
                StreamReader fread = new StreamReader(html, System.Text.Encoding.GetEncoding("gb2312"));
                string ss = fread.ReadToEnd();
     
                Response.Write(ss); //直接写字符串到网页会发现,文字可显示,图片、表格无法显示。因此在后面重跳转到html文件页面。
                fread.Close();
                fread.Dispose();
            
          //  }
     
        }
        //public string Geturl(string SNo, string DNo)
        //{
     
        //    Tool tool = new Tool();
        //    SqlConnection mycon = tool.Getconn();
        //    mycon.Open();
     
        //    string sql = "select Url from StudebtData where SNo = '" + SNo + "' and DNo = '" + DNo + "'";
        //    SqlCommand myCmd = new SqlCommand(sql, mycon);
        //    SqlDataReader Dr = myCmd.ExecuteReader();
        //    Dr.Read();
        //    String Url = Dr["Url"].ToString().Trim();
        //    return Url;
        //}
        //将word转换html(带格式)
        private string WordToHtml(object wordFileName)
        {
     
            //在此处放置用户代码以初始化页面 
            Word.Application word = new Word.Application();
            Type wordType = word.GetType();
            Word.Documents docs = word.Documents;
            //打开文件 
            Type docsType = docs.GetType();
            Word.Document doc = (Word.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;
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            //退出 Word 
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return saveFileName.ToString();
        }
     
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月13日
  • 已采纳回答 5月13日
  • 创建了问题 5月7日

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装