Importing CSV Files

myanilkumarmyanilkumar Member Posts: 108
Hi All,

I just need to Import CSV files. I cannot use dataports because it must be run by Job Queue through NAS.

So, I need a write a Codeunit for importing the file.

If any one of have a sample code please share.



Thanks.
Anil

Comments

  • euekimeuekim Member Posts: 21
    Hi,

    Try these:

    Use common diaglog to get the file path
    txtDir := cduCDMgmt.OpenFile('Select File','',4,'*.csv,0);

    Use Record Table to store the file info
    RecFile.SETRANGE(Path, GetFolderDir(txtDir));
    RecFile.SETFILTER(Name, GetFileName(txtDir));

    Use Excel Buffer table to store and read the data
    //to get the txtFileName use COPYSTR function in RecFile.Name //sample txtFileName := COPYSTR(RecFile.Name, 1, 5)
    //to get the txtSheetName use COPYSTR in RecFile.Name
    recExcelBuffer.OpenBook(txtFileName, txtSheetName);
    recExcelBuffer.ReadSheet();

    :thumbsup: rock on!!!
    Thinking, programming, coding, developing == Music!
  • myanilkumarmyanilkumar Member Posts: 108
    Hi,

    Thanks a lot for reply. The Excelbuffer.Readsheet will update the records in Excel Buffer Tables by Rows & Columns?

    Please let me know.

    Regards,
    Anil
  • euekimeuekim Member Posts: 21
    After opening the csv file in code below:

    recExcelBuffer.OpenBook(txtFileName, txtSheetName); //opening the csv file using excel to easily read the data ;p

    the ExcelBuffer table will be updated and conatins the data on your csv file.

    The recExcelBuffer.ReadSheet will Read all records in ExcelBuffer table.
    Thinking, programming, coding, developing == Music!
  • myanilkumarmyanilkumar Member Posts: 108
    Hi,

    Once again Thanks for the reply. Is there any way to get the Name of the sheet?

    Please advise.
  • euekimeuekim Member Posts: 21
    Your Welcome My Friend

    The sheet name is the csv file name itself, try to open ur csv file then you will see the sheet name.

    Use COPYSTR function to remove the file extension then you can get the sheetname.

    :thumbsup: psycholesterol
    Thinking, programming, coding, developing == Music!
  • myanilkumarmyanilkumar Member Posts: 108
    Thanks a lot for help.
  • CarloMCarloM Member Posts: 5
    My file is a .txt file but the values are separated by ;
    If I open it with Excel I get the choice for a separator symbol and then it splits up in columns.
    If I use your code I get the complete file in cell A1. Is there a way to set the separator character before opening the file or reading the sheet.
    CMY
  • BernardJBernardJ Member Posts: 57
    have you tried to (copy and) rename your file?
  • CarloMCarloM Member Posts: 5
    No I did not rename the file.
    The case is solved so thank you for your time.
    I'm going to post my solution.
    CMY
Sign In or Register to comment.