简体   繁体   English

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

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

I am converting html to docx using Microsoft.Office.Interop.Word. 我正在使用Microsoft.Office.Interop.Word将html转换为docx。 Also html have img tag. html也有img标签。 Converted docx files shows imges properly in server but in other machines images does't come. 转换后的docx文件可在服务器上正确显示图像,但在其他计算机上则不会显示图像。

After investigating i found that images in docx is not embedded as it shows image path of server. 经过调查,我发现docx中的图像未嵌入,因为它显示了服务器的图像路径。

Any help on this would be of great use. 在这方面的任何帮助将很有用。

Code is as follows : 代码如下:

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);

Below is a simplified example of looping through to set images to save with the document (see SavePictureWithDocument ). 以下是循环浏览以设置图像以与文档一起保存的简化示例(请参阅SavePictureWithDocument )。

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

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

So beautiful and logical 如此美丽和逻辑

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

You get a null when you try to access the index i=0 , because numbering is from 1 当您尝试访问索引i=0 ,您将获得null ,因为编号从1开始

For Office 2016 for some reason I've received wordDoc.InlineShapes[i] = null, thus I've extend solution with additional check: 对于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;
}

or same with Lamdbda 或与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