![]() |
|
|||||||||||||||||||||||||||
|
|
Example 2. Writing data to an Excel spreadsheet (Note: This is the same Example 1, but adapted to work with f90SQL-lite)
A common situation for Fortran programmers is the need to put the results of numeric-intensive applications into a Microsoft Excel spreadsheet for graphic representation. In this example we will see how f90SQL can be use for this task. Let's start by creating an Excel spreadsheet that will contain the numeric output of our Fortran program:
The ODBC driver for Excel, considers each spreadsheet to be a table, the columns and rows of the spreadsheet are the columns and rows of the table. Excel always adds a $ symbol to the spreadsheet names when accessed through the ODBC-API. So the name of the first table (spreadsheet) is Sheet1$. Let's add the labels X and Y to the first two columns:
These labels will be used by the Excel ODBC driver to identify the columns of the table. We will write the output of the Fortran program to these two columns. We also want to chart the output, so let's create a scatter plot and insert it into the page. The range for the scatter plot is from row 2 to row 25. These rows are currently empty so the scatter plot does not show anything:
After you have added and modified the chart to your taste, the spreadsheet should look similar to this::
The next step is to save the spreadsheet to a directory. Let's call the spreadsheet Sine.xls and save it to directory c:\temp. If you want to see the program in operation, do not close the spreadsheet, when you run the program you will see the new numbers being added to the cells (note, however that keeping the spreadsheet open will slow down the program, but it is fun to see the numbers and the chart being upgraded as the program progresses). Because f90SQL-lite can only access data that has been registered as a DSN, we need to open the ODBC Driver Manager and create a DSN for the spreadsheet. To do this, go to Window's Control Panel and double click the ODBC Administrator:
In the ODBC Administrator, open the tab System DSN and click on the Add button:
And create a new DSN using the Excel ODBC Drivers. Call it "Sine". Click on the Select Workbook button, and select Sine.xls. Make sure you uncheck the Readonly option, otherwise the program will not be able to write new data to the spreadsheet:
Click the OK button and close the ODBC Administrator. Now, we want to write the Fortran program that will generate and write the data for the spreadsheet. As you might have guessed, the program will generate the X,Y pairs of a sine function. This is the print out of the program:
Now, we are ready to compile and link the program. If you installed the f90SQL library and modules in their default directories (i.e. f90SQL.DLL in the windows system directory, f90SQL.LIB in the compiler's library directory and f90SQL.MOD, f90SQLConstants.mod and f90SQLStructures.mod in the compiler include directory) then open a command window in the directory where you saved the program and enter the following command:
If you are using Absoft's Pro-Fortran, the command is exactly the same, the output messages from the compiler will be different. Now we are ready to run the program and see the results. Execute your program and enter the name and path of the spreadsheet you just created:
That's it. Check the spreadsheet, it should look similar to the following:
|
|||||||||||||||||||||||||||
|
Download a PostScript version of this example |
||||||||||||||||||||||||||||
|
Copyright
© 1998-2000 Canaima Software
For questions regarding this site, send an e-mail to webmaster@canaimasoft.com |
||||||||||||||||||||||||||||