web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

WorkBooks.openText

(0) ShareShare
ReportReport
Posted on by 90

Hi,

I'm trying to open a txt file and convert it to an Excel File.

Everything goes right but when I try to set the format of the cells (parameters FieldInfo of the openTxt mehtod - 13) it doesn't work.

It has to be an Array but I tryied a lot of unsuccefull combinations.

Can anyony help me?

This is my code :

    #Excel
    SysExcelApplication excel;
  SysExcelWorkBooks books;
  Filename excelFileName;
  Filename csvFileName;
  ComVariant arrayCOM = new ComVariant();
  container cont;

    int i, result;
    COM com;
  

  ;
  csvFileName = '\\\route\\File.txt';
  excelFileName = '\\\\route\\Test.xls';
  excel = SysExcelApplication ::construct( );
  excel.displayAlerts (false);


    // Insert 10 values in the array
    for (i = 1; i <= 10; i++)
    {
        cont = conins(cont,i,2);
    }

    arrayCOM.container(cont);

    books = excel.workbooks( );
    books.comObject().openText(csvFileName,    //1 - Filename
                               #xlWindows,          //2 - Origin
                               1,                   //3 - StartRow
                               1,                   //4 - DataType
                               1,                   //5 - TextQualifier
                               false,               //6 - ConsecutiveDelimiter
                               false,               //7 - Tab
                               false,               //8 - Semicolon
                               false,               //9 - Comma
                               false,               //10 - Space
                               true,                //11 - Other
                               "|",                 //12 - Other Char
                               arrayCOM);                  //13 - FieldInfo (xlTextFormat)

  books.item(1).saveAs(excelFileName);
  excel.quit() ;

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Bilal Issa Profile Picture
    4,370 on at
    Re: WorkBooks.openText

    Hi Eloy,

    FieldInfo should be two dimensional array and in x++ Array only support one dimensional array , so I think you need use another way by opening the text file and loop on the data and write it in excel file.

    msdn.microsoft.com/.../bb223513.aspx

  • Eloy Ledo Profile Picture
    90 on at
    Re: WorkBooks.openText

    Hi Bilal,

    I need to send the array because I want to change the default (general) cell format, that's why I need the FieldInfo parameter, I want to set Text format to all cells by default when I open the .txt file.

  • Bilal Issa Profile Picture
    4,370 on at
    Re: WorkBooks.openText

    Hi Eloy,

    if you want only to save it as XLS, you don't need to pass an array, just use the following:

     

    books.comObject().openText( csvFileName,

     

    //1 - Filename

    #xlWindows,

     

    //2 - Origin

     

     

     

    1, //3 - StartRow

     

     

     

    1, //4 - DataType

     

     

     

    1, //5 - TextQualifier

     

     

     

    false, //6 - ConsecutiveDelimiter

     

     

     

    false, //7 - Tab

     

     

     

    false, //8 - Semicolon

     

     

     

    false, //9 - Comma

     

     

     

    false, //10 - Space

     

     

     

    true, //11 - Other

     

     

     

    "|") ; //12 - Other Char ;

  • Eloy Ledo Profile Picture
    90 on at
    Re: WorkBooks.openText

    Hi Bilal,

    Thanks for your code solution but it's not what I'm expecting to do.

    I'm trying to open a .txt file delimited by pipes to convert it to XLS file, just open as .txt and save as .xls.

    The problem is on FieldInfo parameter which is expecting a Array field, I don't know what kind of array I can send to the openTxt method, I've tried COMVariant Arrays, Array class and containers but it's not working.

    Thx a lot!

  • Bilal Issa Profile Picture
    4,370 on at
    Re: WorkBooks.openText

    Hi ,

     

    please check the code below.

     

    static void ReadExcelFile (Args _args)
    {
    SysExcelApplication excel;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row;
    CustAccount account;
    CustName name;
    #define.filename(@'C:\temp\customers.xlsx')
    excel = SysExcelApplication::construct();
    workbooks = excel.workbooks();
    try
    {
    workbooks.open(#filename);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    type = cells.item(row+1, 1).value().variantType();
    while (type != COMVariantType::VT_EMPTY)
    {
    row++;
    account = cells.item(row, 1).value().bStr();
    name = cells.item(row, 2).value().bStr();
    info(strFmt('%1 - %2', account, name));
    type = cells.item(row+1, 1).value().variantType();
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans