c #html到使用Microsoft.Office.Interop进行docx转换

时间:2015-01-03 11:15:03

标签: c# html docx

我正在使用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);

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

如此美丽和合乎逻辑

split()

当您尝试访问索引for (var i = !!! 1 ; i <= !!! wordDoc.InlineShapes.Count; i++) { if (wordDoc.InlineShapes[i].LinkFormat != null) { wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true; } } 时,您会收到null,因为编号是从1

答案 2 :(得分:0)

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