asp. Net word to HTML. The picture in it cannot be displayed

CSDN Q & A 2022-05-14 13:17:52 阅读数:441

asp.aspwordhtml.html

The result is the position and border of the picture , But it doesn't show , It's using web Form file
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 Turn into html  /// </summary>  /// <param name="wordFileName"></param>  private string WordToHtml(object wordFileName) { // Put the user code here to initialize the page  Word.Application word = new Word.Application(); Type wordType = word.GetType(); Word.Documents docs = word.Documents; // Open file  Type docsType = docs.GetType(); Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); // Transformation format , Save as  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); // sign out Word  wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return saveFileName.ToString(); } }

}




Take the answer :

Hello ,
You should be looking for the online code . Write the string directly to the web page and you will find , Text can be displayed , picture 、 The table cannot be displayed . So jump back to html File page . I suggest you take a look at the following code .

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 The page doesn't exist , hold word convert to html { string filename = WordToHtml(serverPath); //StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312")); //string ss = fread.ReadToEnd(); //Response.Write(ss); // Write the string directly to the web page and you will find , Text can be displayed , picture 、 The table cannot be displayed . So jump back to html File page . //fread.Close(); //fread.Dispose(); } // else {
 StreamReader fread = new StreamReader(html, System.Text.Encoding.GetEncoding("gb2312")); string ss = fread.ReadToEnd(); Response.Write(ss); // Write the string directly to the web page and you will find , Text can be displayed , picture 、 The table cannot be displayed . So jump back to html File page . 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; //} // take word transformation html( With format ) private string WordToHtml(object wordFileName) { // Put the user code here to initialize the page  Word.Application word = new Word.Application(); Type wordType = word.GetType(); Word.Documents docs = word.Documents; // Open file  Type docsType = docs.GetType(); Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); // Transformation format , Save as  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); // sign out Word  wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return saveFileName.ToString(); } }

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/134/202205071813343773.html