繁体   English   中英

使用Microsoft.Office.Interop将C#html转换为docx

[英]c# html to docx conversion using Microsoft.Office.Interop

我正在使用Microsoft.Office.Interop.Word将html转换为docx。 html也有img标签。 转换后的docx文件可在服务器上正确显示图像,但在其他计算机上则不会显示图像。

经过调查,我发现docx中的图像未嵌入,因为它显示了服务器的图像路径。

在这方面的任何帮助将很有用。

代码如下:

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
                Object oMissing = System.Reflection.Missing.Value;
                //wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                word.Visible = false;
                //Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
                Object openType = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatWebPages;
                Object filepath = documentPath;
                word.Documents.Open(FileName: filepath, ReadOnly: false, Format: openType);
                Object confirmconversion = System.Reflection.Missing.Value;
                Object readOnly = false;
                string htmlFileNameWithExtension = Path.GetFileName(documentPath);
                string htmlFileNameWithoutExtension = Path.GetFileNameWithoutExtension(documentPath);

                Object saveto = documentPath.Replace(htmlFileNameWithExtension, htmlFileNameWithoutExtension);

                Object oallowsubstitution = System.Reflection.Missing.Value;


                wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                object fileFormat = WdSaveFormat.wdFormatDocumentDefault;


                wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
                               ref oMissing);


                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                word.Quit(ref oMissing, ref oMissing, ref oMissing);

以下是循环浏览以设置图像以与文档一起保存的简化示例(请参阅SavePictureWithDocument )。

for (var i = 0; i < wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat == null) {
        continue;
    }

    wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
}

如此美丽和逻辑

for (var i = !!! 1 ; i <= !!! wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat != null) {
        wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
    }    
}

当您尝试访问索引i=0 ,您将获得null ,因为编号从1开始

对于Office 2016,由于某种原因,我收到了wordDoc.InlineShapes [i] = null,因此我通过附加检查扩展了解决方案:

for (var i = 0; i < wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat == null) {
        continue;
    }

    wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
}

或与Lamdbda相同

document.InlineShapes.ToList().
                    Where(v => v != null && v.LinkFormat != null).ToList().
                    ForEach(v => v.LinkFormat.SavePictureWithDocument = true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM