word错误提示html,转换Html Contenttpo Word文档时出现错误

你好我每个人使用HTML Agility和Openxml将我的html内容转换为word文件内容。转换Html Contenttpo Word文档时出现错误

less_title_27.jpg

Exercise
Student's Name: Class:
  
  1. Describe the purpose of Windows Update.
  1. Explain why using Windows Update is critical to maintaining an operating system.
  1. Summarize the process used to access and install Windows Updates.
  1. Compare and contrast using Windows Update and using a Windows Service Pack.

Note: You must print your completed exercise

to submit to your instructor.

Do Not close this window without printing your exercise or your answers will be lost.

这是我用来转换的html内容。 但我解析它时得到以下错误。

at NotesFor.HtmlToOpenXml.TableContext.get_CurrentTable()

at NotesFor.HtmlToOpenXml.HtmlConverter.ProcessTableColumn(HtmlEnumerator en)

at NotesFor.HtmlToOpenXml.HtmlConverter.ProcessHtmlChunks(HtmlEnumerator en, String endTag)

at NotesFor.HtmlToOpenXml.HtmlConverter.Parse(String html)

at WebApplication3.WebForm3.Button1_Click(Object sender, EventArgs e) in C:\Users\USER\Documents\Visual Studio 2008\Projects\Piyush_training\WebApplication3\WebForm3.aspx.cs:line 102

我的代码如下。

using DocumentFormat.OpenXml.Drawing;

using NotesFor.HtmlToOpenXml;

using System.IO;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;

using DocumentFormat.OpenXml;

using HtmlAgilityPack;

using System.Text;

protected void Button1_Click(object sender, EventArgs e)

{

const string filename = "C:/Temp/test.docx";

Response.ContentEncoding = System.Text.Encoding.UTF7;

System.Text.StringBuilder SB = new System.Text.StringBuilder();

System.IO.StringWriter SW = new System.IO.StringWriter();

串pagecontent = 上述HTML内容; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(pagecontent); if(doc == null); doc.OptionCheckSyntax = true; doc.OptionAutoCloseOnEnd = true; doc.OptionFixNestedTags = true; int errorCount = doc.ParseErrors.Count(); string output =“”;

doc.Save(SW);

System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);

strBody = "" + "

" + "
" + htmlTW.InnerWriter.ToString() + "
" + "" + "";

string html = strBody;

try

{

using (MemoryStream generatedDocument = new MemoryStream())

{

using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))

{

MainDocumentPart mainPart = package.MainDocumentPart;

if (mainPart == null)

{

mainPart = package.AddMainDocumentPart();

new Document(new Body()).Save(mainPart);

}

HtmlConverter converter = new HtmlConverter(mainPart);

converter.ExcludeLinkAnchor = true;

converter.RefreshStyles();

converter.ImageProcessing = ImageProcessing.AutomaticDownload;

Body body = mainPart.Document.Body;

converter.ConsiderDivAsParagraph = false;

var paragraphs = converter.Parse(html);

for (int i = 0; i < paragraphs.Count; i++)

{

body.Append(paragraphs[i]);

}

mainPart.Document.Save();

}

File.WriteAllBytes(filename, generatedDocument.ToArray());

}

System.Diagnostics.Process.Start(filename);

}

catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

2011-10-05

Vir

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值