1

Im having trouble appending the data in a MemoryStream to other Memorystream allready created an full of data. I'm doing this to create an excel from two different sources.

First I create the father memorystream:

MemoryStream obj_stream = new MemoryStream(100000000);
var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid() + ".xls");
obj_stream.Write(File.ReadAllBytes(tempFile), 0, File.ReadAllBytes(tempFile).Length);
File.Delete(tempFile);

Then I create the son memorystream from a compositeLink.ExportToXls

compositeLink.CreatePageForEachLink();
compositeLink.PrintingSystemBase.XlSheetCreated += PrintingSystemBase_XlSheetCreated;

using (MemoryStream stream = new MemoryStream(100000000))
{
    compositeLink.ExportToXls(stream, new XlsExportOptions() { ExportMode = XlsExportMode.SingleFilePageByPage });
    obj_stream.Write(stream.ToArray(), 0, stream.ToArray().Length);
}

Finally I write the stream to a Page.Response

if (Page == null || Page.Response == null)
    return;
string disposition = saveAsFile ? "attachment" : "inline";
Page.Response.Clear();
Page.Response.Buffer = false;
Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
Page.Response.AppendHeader("Content-Transfer-Encoding", "binary");
Page.Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat));
Page.Response.BinaryWrite(obj_stream.ToArray());
Page.Response.End();

What is happening I believe is that either one or the other gets overwritten by the other, depending on which one I do the "nameOfTheStream".write()

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 4
    You can't combine two spreadsheets by just concatenating two `.xls` files together. – Damien_The_Unbeliever Apr 01 '19 at 10:14
  • You have to consider file headers etc. If You're trying to concat two xls files, consider populating a third (new) xls file with the values of the two previous ones. – Vulpex Apr 01 '19 at 10:15
  • How do I merge multiple excel files into one: https://stackoverflow.com/questions/27285615/how-do-i-merge-multiple-excel-files-to-a-single-excel-file – Calin Vlasin Apr 01 '19 at 10:17

1 Answers1

0

The second time time you write to obj_stream, do not start writing from 0 index (specified in second parameter of Write method) again. There is some data in there already.

obj_stream.Write(stream.ToArray(), obj_stream.ToArray().Length, stream.ToArray().Length);

Method details here.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user10954641
  • 73
  • 1
  • 8