Search code examples
c#asp.netcrystal-reportsms-wordcorrupt

Crystal reports 2008 sp5 generates a corrupt word document


Since last updates of a Crystal report, I have about problem when a word file generation. The word document is corrupt and I can not find on the web the sollution or a similar problem. I can generate a PDF document without problem. The document is corrupt with Word, but I can opens with WordPad.

the resulting file is .doc corrupted without error on my code

try
{
    t = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
    content_type = "application/msword";
    var oStream = MonReport.ExportToStream(t);
    byte[] byteArray = new byte[oStream.Length];
    oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));


    parent.Response.ClearContent();
    parent.Response.ClearHeaders();

    parent.Response.ContentType = content_type;

    parent.Response.BinaryWrite(byteArray);
    parent.Response.Flush();
    parent.Response.Close();
    MonReport.Close();
    MonReport.Dispose();

Solution

  • Your length is 1 too short. That should be:

    oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length));
                                               ^^^^^^^^^^^^^^
    

    Stream.Read's third parameter is

    "The maximum number of bytes to be read from the current stream."