将 docx 转换为 html c# 时文件包含损坏的数据

file contains corrupted data while converting docx to html c#

我正在从 SQL 数据库 varbinary 字段值编写一个 docx 文件。文件正在正确写入。当我打开文件时,我收到消息“word fund unreadable content..”(下面的屏幕截图)。如果我单击“是”,那么我将获得包含正确内容的 docx 文件。这里有 2 个任务,首先读取数据库并写入 docx 文件,然后读取 docx 文件并转换为 html.

我需要将此 docx 文件转换为 html,然后需要保存在数据库中。 转换时出现错误 ”文件包含损坏的数据,请参阅我下面的代码来编写 docx 并转换为 html。

编写docx代码:

cmd.CommandText = "SELECT [pricing_discussion_ole] FROM [dbo].[Query]  where deal_identifier='ARCGL00202020'";                    
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            int size = 1024 * 1024;
                            byte[] buffer = new byte[size];
                            int readBytes = 0;
                            int index = 0;
                            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
                                {
                                    fs.Write(buffer, 0, readBytes);
                                    index += readBytes;
                                }
                            }
                        }
                    }

正在将 docx 转换为 HTml 但出现错误(文件包含损坏的数据) 打开文件。有什么帮助吗?

写入docx文件后,读取docx并转换为html

using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false)) //  getitng error here (file contain corrupted data)
                {
                    HtmlConverterSettings settings = new HtmlConverterSettings()
                    {
                        PageTitle = "My Page Title"
                    };
                    XElement html = HtmlConverter.ConvertToHtml(doc, settings);
                    var result = html.ToStringNewLineOnAttributes();

                }

我遇到了问题,在写入文件时从数据库中获取不需要的字节代码,导致打开文件错误。谢谢!