-2

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

                }
Letoncse
  • 702
  • 4
  • 15
  • 36
  • 1
    What you are writing is not a docx. A docx file is a zip file containing very strict XML files. (To check, copy an existing docx file, rename it to a zip extension, and unpack it; you will see what I mean). There are libraries to help you, if you search for them, or you can create your own using standard .Net libraries (lots of work, though). It would be far easier to write a routine to output your data as html directly, rather than going through docx – Jonathan Willcock Oct 14 '21 at 10:38
  • Hi Jonathan, Thanks for your reply. yes that will be better if convert SQL varbinary data to html directly. Do you have any code sample please? Thanks ! – Letoncse Oct 14 '21 at 10:40

1 Answers1

0

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

Letoncse
  • 702
  • 4
  • 15
  • 36