1.11.1 Exporting Worksheets

The Worksheet class has the ExportASCII method for saving worksheet data to a file. The method has arguments for specifying the starting row and column and the ending row and column. It also allows you to specify how to handle missing data values and whether column labels should be exported or not.

All of the examples below assume wks is a valid Worksheet object and strFileName is a string object containing the full path and name of the target file.

The first example will save all the data in the worksheet to a file using the tab character as the delimiter and blanks for missing values.

wks.ExportASCII(strFileName,
    WKS_EXPORT_ALL|WKS_EXPORT_MISSING_AS_BLANK);

The next example will save all the data in a worksheet to a file, with a comma as the delimiter and blanks for missing values. In addition the column labels are also saved.

wks.ExportASCII(strFileName,
    WKS_EXPORT_ALL|WKS_EXPORT_LABELS|WKS_EXPORT_MISSING_AS_BLANK,
    ',');

The final example will save the first two columns of data in a worksheet to a file, using a comma as the delimiter and blanks for missing values. In addition, the column labels are also saved. Row and column indices start with zero. The end row and column indices can also be -1 to indicate the last row or last column, respectively.

wks.ExportASCII(strFileName,
    WKS_EXPORT_ALL|WKS_EXPORT_LABELS|WKS_EXPORT_MISSING_AS_BLANK,
    '\t',
    0, 0,   // start with first row, first column
    -1, 1); // end with last row, second column