views:

42

answers:

2

I need to generate some kind of reports in excel via web system. My current code is as follow (simplified):

//[javascript inside .aspx page]

ExcelApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");

ExcelSheet.Windows(1).WindowState = 2;
ExcelSheet.Application.Visible = false;

for (i=1; i< [elementNumber]; i++)
{
   ExcelSheet.ActiveSheet.Cells(i,1).Value = myXML.documentElement.childNodes(i).text;
}

ExcelSheet.Application.Visible = true;

Even if I populate only few hunded rows it takes about 10 second. Is there any way to speed up this process like create entire sheet in memory instead of populate cell by cell?

A: 

You could try disabling recalculation to see if that helps

Application.Calculation = xlCalculationManual

Application.Calculation = xlCalculationAutomatic

Other than that this page has a few good tips to squeeze as much performance out of excel

http://www.cpearson.com/excel/optimize.htm

Kevin Ross
When I added those calculation switches into javascript I recived empty sheet ( no errors). I looked deeper into web and it looks like I cannot make it faster in any easy way.
zgorawski
+1  A: 

Define an array of object[,] first. You will no longer see a time delay, no matter how big it is. Btw, the array can also hold formulas.

e.g. something like this function i use in my project:

    public static void AddValueArrayToSheet(_Worksheet ws, Range rangeTopLeft, object[,] values) {
        Range cellRange = ws.Cells.get_Range(rangeTopLeft, rangeTopLeft[values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1]);
        cellRange.Value = values;
    }
Rolf
I think that it is a proper solution, but I cannot manage to get it work in JavaScript :-/. When I am trying to assign object array to my cellRange I get " ';' char expected" error, but I dont miss any, my indexes are right etc.
zgorawski
It looks like it is something with the character ';', but can't help you on this, JavaScript is not really my world. - The array is zero based? And you don't have any formulas in the text msg, otherwise the ; is the seperator in some cultures e.g. German.
Rolf