C# 将html转换为word文档时未反映字体

C# 将html转换为word文档时未反映字体,c#,html,c#-4.0,ms-word,C#,Html,C# 4.0,Ms Word,我的网站是c#ASP.NET4.0。这里我使用HTML打开xml代码和dll 为此,我将执行以下步骤:- 1) 首先,我打开现有的word文档,复制一些段落,字体为“Arial”。 2) 然后,我使用HTMLTOpenXMLDLL将html解析为我的html到word转换代码 我的c代码在这里:- protected void btnExportToWord_Click(object sender, EventArgs e) { string readText = str

我的网站是c#ASP.NET4.0。这里我使用HTML打开xml代码和dll

为此,我将执行以下步骤:-

1) 首先,我打开现有的word文档,复制一些段落,字体为“Arial”。 2) 然后,我使用HTMLTOpenXMLDLL将html解析为我的html到word转换代码

我的c代码在这里:-

protected void btnExportToWord_Click(object sender, EventArgs e)
    {
        string readText = string.Empty;
        const string filename = "C:\\Temp\\ExportToDocNew1.docx";

        try
        {               
            HTMLFile = "<html><head><style type=\"text/css\" media=\"screen,print\">";
            HTMLFile += " .divPageBreak { page-break-after: always; display: block;  clear: both;}";
            HTMLFile += " table,td{border:1px solid black;border-collapse:collapse;}";
            HTMLFile += "</style></head><body><div class=\"divPageBreak\">" + "<p>&nbsp;</p>
protectedvoid btnExportToWord\u单击(对象发送方,事件参数e)
{
string readText=string.Empty;
常量字符串filename=“C:\\Temp\\ExportToDocNew1.docx”;
尝试
{               
HTMLFile=“”;
HTMLFile+=“.divPageBreak{page break after:always;display:block;clear:both;}”;
HTMLFile+=“表,td{border:1px纯黑色;边框折叠:折叠;}”;
HTMLFile+=“”+”

自您报到履职之日起,您将有资格获得随附附件中规定的工资、津贴和其他福利。附件中所示的薪酬是全面的、包罗万象的,因此应视为包括公司的所有负债。您的薪酬待遇严格符合您与公司之间的诚信,不得与任何人讨论,也不得以任何方式向任何人透露

“+”

使用(MemoryStream generatedDocument=new MemoryStream())
{
使用(WordprocessingDocument包=WordprocessingDocument.Create(generatedDocument,WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart=package.MainDocumentPart;
if(mainPart==null)
{
mainPart=package.AddMainDocumentPart();
新的DocumentFormat.OpenXml.Wordprocessing.Document(newbody()).Save(mainPart);
}
HtmlConverter converter=新HtmlConverter(主要部件);
Body Body=mainPart.Document.Body;
SectionProperties sectionProps=新的SectionProperties();
PageMargin PageMargin=new PageMargin(){Top=1000,Right=(uint32值)1008U,Bottom=2000,Left=(uint32值)1008U,Header=(uint32值)1600U,Footer=(uint32值)720U,Footer=(uint32值)0U};
sectionProps.Append(页边距);
body.Append(sectionProps);
var段落=converter.Parse(HTMLFile);
word.Application wordApp=新的word.Application();
对于(int i=0;i
您还没有明确提出我可以看到的问题,因此我认为您对在C#/.NET中使用从html到docx的字体感兴趣

我不知道“HTMLTOpen xml dll”,但作为替代方案,当然支持字体:

 /**
 * Map a font family, for example "Century Gothic" in:
 * 
 *    font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * 
 * to a w:rFonts object, for example:
 * 
 *    <w:rFonts w:ascii="Arial Black" w:hAnsi="Arial Black"/>
 * 
 * Assuming style font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * the first font family for which there is a mapping is the one
 * which will be used. 
 * 
 * xhtml-renderer's CSSName defaults font-family: serif
 * 
 * It is your responsibility to ensure a suitable font is available 
 * on the target system (or embedded in the docx package).  If we 
 * (eventually) support CSS @font-face, docx4j could do that
 * for you (at least for font formats we can convert to something
 * embeddable).
 * 
 * You should set these up once, for all your subsequent 
 * imports, since some stuff is cached and currently won't get updated
 * if you add fonts later.
 * 
 * @since 3.0
 */
public static void addFontMapping(String cssFontFamily, RFonts rFonts) {
    FontHandler.addFontMapping(cssFontFamily, rFonts);
}

public static void addFontMapping(String cssFontFamily, String font) {

    FontHandler.addFontMapping(cssFontFamily, font);
}
 /**
 * Map a font family, for example "Century Gothic" in:
 * 
 *    font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * 
 * to a w:rFonts object, for example:
 * 
 *    <w:rFonts w:ascii="Arial Black" w:hAnsi="Arial Black"/>
 * 
 * Assuming style font-family:"Century Gothic", Helvetica, Arial, sans-serif;
 * the first font family for which there is a mapping is the one
 * which will be used. 
 * 
 * xhtml-renderer's CSSName defaults font-family: serif
 * 
 * It is your responsibility to ensure a suitable font is available 
 * on the target system (or embedded in the docx package).  If we 
 * (eventually) support CSS @font-face, docx4j could do that
 * for you (at least for font formats we can convert to something
 * embeddable).
 * 
 * You should set these up once, for all your subsequent 
 * imports, since some stuff is cached and currently won't get updated
 * if you add fonts later.
 * 
 * @since 3.0
 */
public static void addFontMapping(String cssFontFamily, RFonts rFonts) {
    FontHandler.addFontMapping(cssFontFamily, rFonts);
}

public static void addFontMapping(String cssFontFamily, String font) {

    FontHandler.addFontMapping(cssFontFamily, font);
}
# docx4j-ImportXHTML.properties

docx4j-ImportXHTML.fonts.default.serif=Times New Roman

docx4j-ImportXHTML.fonts.default.sans-serif=Arial

docx4j-ImportXHTML.fonts.default.monospace=Courier New