3.7.1 Export ASCII



Version Info

Minimum Origin Version Required: Origin 8 SR1

Exporting Worksheet Data as ASCII File

The following example illustrates how to export the active worksheet data to an ASCII file. Labels are included are included in the resulting file and the missing values are treated as blank.

void export_worksheet_as_ascii()
{
  // get active worksheet
  Worksheet wks = Project.ActiveLayer();  
  if (!wks)
  {
    out_str("Please activate a worksheet!\n");
    return;
  }

  // file type filter
  StringArray saFileTypes;  
  saFileTypes.SetSize(4);
  saFileTypes[0] = "[*.dat] *.dat";
  saFileTypes[1] = "[Text File (*.txt)] *.txt";
  saFileTypes[2] = "[Comma Delimited *.csv] *.csv";
  saFileTypes[3] = "[All Files (*.*)] *.*";
	
  // default file name
  string strFileName;  

  // worksheet name
  wks.GetName(strFileName);  

  // default file path
  string strFilePath; 

  // user files path
  strFilePath = GetAppPath(FALSE);  

  // full file path,including file name
  string strFullPath; 

  // open SaveAs dialog
  strFullPath = GetSaveAsBox(saFileTypes, strFilePath, strFileName);  
	
  if (strFullPath.IsEmpty())
    out_str("User has cancelled the SaveAs dialog box!\n");
  else
  {
   // export worksheet data, including data, labels, stored missing value as blank
   int nRet = wks.ExportASCII(strFullPath, WKS_EXPORT_ALL | WKS_EXPORT_LABELS | 
             WKS_EXPORT_MISSING_AS_BLANK);

   if (nRet == -1)
     out_str("Failed to export!\n");
   else
     printf("The exported file size is %d.\n", nRet);
  }
}