Search code examples
c#fileopendocument

file contains corrupted data while converting docx to html c#


I'm writing a docx file from SQL database varbinary field value. File is writing properly. When I open the file I'm getting message "word fund unreadable content.." (screenshot below). If I click Yes then I'm getting my docx file with proper content. here have 2 task first read database and write docx file then read docx file and covert to html.

I need to convert this docx file to html and then need to save in database. while converting I'm getting error "file contains corrupted data" , please see my below code to write docx and convert to html.

Write docx code:

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

enter image description here

converting docx to HTml but getting error (file contain corrupted data) to open the file. Any help please?

After writing docx file , Read docx and covert to 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();

                }

Solution

  • I got the issue, while writing file getting unwanted byte code from database that causing to open file error. Thanks !