-2

我正在从 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();

                }
4

1 回答 1

0

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

于 2021-10-15T11:18:16.633 回答