How to Write an Excel File from the COBOL Program

by Charles Carswell

COBOL, in its eloquence, has many capabilities and versatilities. Among them is the ability to output various file types and structures. You can produce an Excel file from your COBOL program fairly quickly and easily. Because COBOL has the easy-to-follow flow of the English language, the steps involved here are simple enough to produce and at the same time you can even enhance them. As an example, you can produce multiple Excel files from one COBOL program structured in various ways such as reports and tables. The Excel files you create can also be Comma Separated Value (CSV) files, space delimited, tab delimited and so forth. This is useful for preparing data to be exported to databases such as MS Access.

1

Set up the file structure you wish to export to MS Excel and define it in the "File-Control" of your COBOL program's "Input-output section". See Tip. The CSV extension allows Excel to identify the file as a Comma Separated Value file and allow viewing and editing.

2

Create the file and record definitions in the "File Section" of the "Cobol Data Division". Your "FD" statement will define the "Extract-File" as standard and the "Record Contains Characters" will be set to the length in bytes of your output file. Ensure that the field descriptions exactly match the fields of your input record(s). At the end of each field description, there should be and extra "Filler" that declares pic (x) value ','. This is the separator for each field. You are now ready to write the new file.

3

Type your "Extract-File" statement as output in the "Procedure Division" along with the other declarations you have.

4

Code your loop logic to sequentially read and write each file as you have it defined until the input is exhausted. When the file is written, you can view it using Microsoft Excel as it will appear as an Excel file.

Tip

  • check As an example, if you are producing an Excel version of a name and address file with phone numbers and email addresses, your Excel file statement would look like this: select extract-file assign to "g:\your dir\extract.csv."

About the Author

Charles Carswell has worked as a technical writer/programmer and illustrator for many fortune 500 companies since 1980. Currently he holds a Bachelor of Science degree in management information systems and certificates in graphics and photography.

Photo Credits

  • photo_camera data and data security image by Albert Lozano from Fotolia.com