0

I have to export very large files as an "excel export". Since .NET can't export excel files, I went with simple html tables.

It works fine, but it's slow.

Is it possible to context.response.write each line as they're being created instead of building some super huge string and trying to export the whole thing once it's done?

I could care less what function is used to do this, but I hope you know what I mean. I don't want to build a string into memory and then try to send it all at once. I'd rather export as I build the table.

Is this possible?

Thanks in advance!

  • It is not exactly clear what is your problem: does anyone force you to "build string into memory" and you need help with that? Side note: ".Net can't export Excel files" is questionable statement... so don't build too much of your reasoning on it. – Alexei Levenkov Nov 12 '12 at 22:34
  • Thanks for helping! I found that the only way to export a true blue excel file is to use Microsoft.Office.Interop.Excel. The docs recommend against this and say that it will cause "system instability" if used on a large scale. Currently, I'm building a large string from database data. Instead of building a large string and sending the result when finished, I'd like to send every line to the client as they're being built. Is this possible? –  Nov 12 '12 at 22:43

1 Answers1

2

Yes, using context.Response.Write on each line is just fine. If the reason for not wanting to build a large string is server memory use, then you'll need to turn off response buffering like so:

context.Response.BufferOutput = false;

Otherwise, .NET will just buffer your writes in memory until the end, anyway.

If the reason is execution time, then you may be experiencing performance hits from multiple string concatenations. In that case, you could use the StringBuilder class to construct the table instead.

For example...

StringBuilder sb = new StringBuilder();
for each (<row in database>) {
    sb.AppendLine(<current table row>);
}
context.Response.Write(sb.ToString());

More info on response buffering: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.bufferoutput.aspx

More info on the StringBuilder class: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

boynoid
  • 116
  • 3