你好我每个人使用HTML Agility和Openxml将我的html内容转换为word文件内容。转换Html Contenttpo Word文档时出现错误
Student's Name: | Class: |
- Describe the purpose of Windows Update.
- Explain why using Windows Update is critical to maintaining an operating system.
- Summarize the process used to access and install Windows Updates.
- 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 = "" + "
" + "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