Forum > Beginners

program import CSV & perform calculation

<< < (2/2)


--- Quote from: BigChimp on June 29, 2014, 10:44:20 am ---Also TSDFDataset may happen to work for your CSVs but it is in no way a CSV file format parser...
--- End quote ---

Ahh.  True.  I work in an industry for whom basic programming specifications are a mystery yet to be solved.  One of the software packages (used extensively in the North Sea *NOT* written by me or any company I've worked for as a developer) breaks if the users enters commas, quotes or carriage returns into any text field, as does another software package out in Asia.  Both these software packages backend to respectable databases (MSSQL & MySQL) but use CSV as an intermediate format for sharing data with other software packages...  Consequently reports generated from these are a grammatical nightmare :)  I have my rfc4180 rant email on hand so each year I can just re-forward it to the relevant dev teams...

I've only used TSDFDataset for simple CSV files, log files really.  I honestly thought TSDFDataset had had the rfc4180 fixes incorporated.  I've followed the link you provided (and from there into the bugtracker) and can see that this isn't the case.

@Polly: Assuming your data are in the first sheet of an xls file, you have a header row and two columns A and B which will have to be added in your program, then this is the way to get you started (no guarantee, did not run it...):

--- Code: ---type
  TDataRec = record ColAValue, ColBValue, SumValue: Double; end;
  data: array of TDataRec;
  workbook: TsWorkbook;
  worksheet: TsWorksheet;
  row, lastrow: Integer;
  workbook := TsWorkbook.Create;
    workbook.ReadFromFile(AFileName, sfExcel8); 
      // assuming an Excel8 file, but you can drop this parameter, but you must be aware of some
      // exceptions in the debugger then, they don't occur without the debugger.
    worksheet := workbook.GetWorksheetByIndex(0);  // 1st worksheet
    lastrow := worksheet.GetLastRowIndex;
    SetLength(data, lastrow);        // normally: "lastrow+1", but we drop the first row!
    for row:=1 to lastrow do begin  // we skip the first row because of the headers!
      data[row].ColAValue := worksheet.ReadAsNumber(row, 0);
      data[row].ColBValue := worksheet.ReadAsNumber(row, 1);
      data[row].SumValue := data[row].ColAValue + data[row].ColBValue;

--- End code ---

thank you all for your replies.
Apologies for the delay, but I have been otherwise occupied with work and have not been able to give the programming any time.

I will have a look at the information provided and hopefully be able to use it.

Many thanks.


Fred vS:

fpGUI has just added a new csv unit...



[0] Message Index

[*] Previous page

Go to full version