Forum Discussion
JavierCollRodri
13 years agoContributor
Hi Teelmu,
Let's see if I can help there. Here is my suggestion:
In your function, you open a Excel file and execute a SQL query for each one of them. You add the results to a variable and once you are finished, you save it to a XML file.
What you want is to write the results to an excel file, each result on a row, right?
Please try this: In your function, open another Excel file at the same time*. Then, in the for loop, for each result, just write in the same coordinates cell the result (I'm asuming you want to save DB results in a single row)
Let's see if I can help there. Here is my suggestion:
In your function, you open a Excel file and execute a SQL query for each one of them. You add the results to a variable and once you are finished, you save it to a XML file.
What you want is to write the results to an excel file, each result on a row, right?
Please try this: In your function, open another Excel file at the same time*. Then, in the for loop, for each result, just write in the same coordinates cell the result (I'm asuming you want to save DB results in a single row)
...
var excelQuerys = Excel.Workbooks.Open("c:\\MyExcel.xlsx");
var excelResults = Excel.Workbooks.Open("c:\\Results.xlsx");
...
for (i = 1; i < RowsCount; i++) {
searchSQL = excelQuerys.ActiveSheet.Cells(i, 1);
result = RunQuery(searchSQL);
excelResults.ActiveSheet.Cells(i, 1).Value = VarToStr(result);
}
I haven't tested it, but that's the general idea. If you want to store DB results as a table, not as a string, it will be a bit more tricky, but it can be done.
I hope it helped!
*If you can't open thow excel files at the same time, just open one, store the results to an array, close it, open a new one and store them later in a for loop.