SQL データベースの varbinary フィールド値から docx ファイルを作成しています。ファイルは正常に書き込まれています。ファイルを開くと、「Word Fund unreadable content..」というメッセージが表示されます (下のスクリーンショット)。[はい] をクリックすると、適切な内容の docx ファイルが取得されます。ここでは、最初にデータベースを読み取り、docx ファイルを書き込み、次に docx ファイルを読み取り、html に変換する 2 つのタスクがあります。
この 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();
}