Import ASCII file into worksheet by using a ASCIMP struct that will define how to import the data
int ImportASCII( LPCSTR lpcszFilename, ASCIMP & stAscImp, ASCIMPRESULT * pResult = NULL, TreeNode & trInfo = NULL, DWORD dwCtrl = 0 )
0 on success, otherwise returns error codes
EX1
// Prompt for datafile to be imported into new worksheet int Datasheet_ImportASCII_Ex1() { int iStatus; ASCIMP ai; string strFile = GetOpenBox("*.dat"); if(AscImpReadFileStruct(strFile,&ai) == 0) { ai.iRenameWks = 1; //to rename worksheet from filename ai.iRenameCols = 1; //0 to keep default column names, 1 to rename columns using 1st line of header block Worksheet wks; wks.Create(); return wks.ImportASCII(strFile, ai); } else return -99; }
EX2
// This example to show how to set each sub headers to specified column label including uer defined parameter labels void Datasheet_ImportASCII_Ex2() { ASCIMP ai; string strFile = GetOpenBox("*.dat"); // or "*.txt" if(strFile.IsEmpty()) return;// user cancel if(AscImpReadFileStruct(strFile, &ai)==0) { ai.iAutoSubHeaderLines = 0; //set auto detected to false since will set sub header to col label by below codes ai.iSubHeaderLines = 4; // 1. LongName 2. Units 3. Expanded Discription(User defined) 4. Type Indication(User defined) // Notice when iAutoSubHeaderLines is 0, the index of ai.nLongName, ai.nUnits and ai.nFirstUserParams are begin from Main Header ai.nLongNames = ai.iHeaderLines; ai.nUnits = ai.iHeaderLines + 1; ai.nFirstUserParams = ai.iHeaderLines + 2; ai.nNumUserParams = 2; ai.iMaxLabels = 0; // Not set any header line to Comments label Worksheet wks; wks.Create(); if(0 == wks.ImportASCII(strFile, ai)) { // Set user parameter labels to specified names Grid grid; grid.Attach(wks); vector<string> vsUserLabels = {"Expanded Discription", "Type Indication"}; grid.SetUserDefinedLabelNames(vsUserLabels); wks.AutoSize(); // to resize worksheet. This method is very useful when data/text is longer than the width of cell } } else out_str("failed to read ascii file"); }
EX3
// This example to show how to do partial import void Datasheet_ImportASCII_Ex3() { Worksheet wks = Project.ActiveLayer(); if(!wks) { out_str("no acitve workbook window to import data"); return; } ASCIMP ai; string strFile = GetAppPath(1) + "Samples\\Curve Fitting\\Step01.dat"; if(!strFile.IsFile()) { strFile = GetOpenBox("*.dat"); // or "*.txt" if(strFile.IsEmpty()) return;// user cancel } if(AscImpReadFileStruct(strFile, &ai)==0) { ai.iMode = ASCIMP_MODE_APPEND_COLS; // append imported data to this worksheet ai.iRenameCols = 0; // 0 to keep default column name ai.iPartial = 1; // 1 to specify partial import ai.iPartialC1 = 1; // import from 2nd column (0 offset) ai.iPartialC2 = 4; // set the last column, so here just import 4 columns ai.iPartialR1 = 0; // set the import row from ai.iPartialR2 = 9; // set the import row to int nRet = wks.ImportASCII(strFile, ai); out_int("nRet = ", nRet); if(0 == nRet) wks.AutoSize(); // to resize worksheet. This method is very useful when data/text is longer than the width of cell } else out_str("failed to read ascii file"); }
EX4
// Prompt for datafile to be imported into new worksheet and print out the min max of each column void Datasheet_ImportASCII_Ex4() { ASCIMP ai; string strFile = GetOpenBox("*.dat"); if(strFile.IsEmpty()) return;// user cancel if(AscImpReadFileStruct(strFile, &ai)==0) { ASCIMPRESULT air; Worksheet wks; wks.Create(); int nRet = wks.ImportASCII(strFile, ai, &air); if( 0 == nRet ) { for(int ii = air.nC1; ii <= air.nC2; ii++) { double min, max; Column cc = wks.Columns(ii); if(cc) { vector v(cc); v.GetMinMax(min, max); printf("%d Col(%s):\tsize=%4d, min = %8.4g, max = %8.4g\n", ii+1, cc.GetName(), v.GetUpperBound()+1, min, max); } else printf("col(%d) invalid\n", ii+1); } } } else out_str("failed to read ascii file"); }
EX5
// same as Example 2, but shows how to do the same thing using DataRange by the 3th output argument pResult->uid // Prompt for datafile to be imported into new worksheet and print out the min max of each column void Datasheet_ImportASCII_Ex5() { ASCIMP ai; string strFile = GetOpenBox("*.dat"); if(strFile.IsEmpty()) return;// user cancel if(AscImpReadFileStruct(strFile, &ai)==0) { ASCIMPRESULT air; Worksheet wks; wks.Create(); int nRet = wks.ImportASCII(strFile, ai, &air); if( 0 == nRet ) { // the following code shows how to get the DataRange object created at the end of the import // you don't have to do this, but we want to show you anyway. DataRange dr; dr = (DataRange)Project.GetObject(air.uid); if(dr) // success and getting data into wks, so we can access it now { int r1, c1, r2, c2; Worksheet wksResult; // this will actually be the same as wks above // index = 0 because range can be multiple, and we just need the first // you can verify that c1,c2,r1,r2 are the same as those inside the air struct above // so getting them again via the DataRange is an over kill, but this is an example anyway. // DataRange is usful if you later need to access the info again, as it is stored in // the wkbook in the Files binary storage. if(dr.GetRange(0, r1, c1, r2, c2, wksResult) && wksResult.IsValid()) { for(int ii = c1; ii <= c2; ii++) { double min, max; Column cc = wksResult.Columns(ii); if(cc) { vector v(cc); v.GetMinMax(min, max); printf("%d Col(%s):\tsize=%4d, min = %8.4g, max = %8.4g\n", ii+1, cc.GetName(), v.GetUpperBound()+1, min, max); } else printf("col(%d) invalid\n", ii+1); } } } } } else out_str("failed to read ascii file"); }
Import ASCII file into worksheet by using a ASCIMP struct that will define how to import the data.
origin.h