使用ExcelIo类操作读取excel文件

static void NJ_MF_Test_ExcelIoTest(Args _args)
{
    ExcelIo io;
    #Excel
    dialogField dialogFilename;
    Dialog dialog = new Dialog("Excel Upload");
    container con;
    ;
    dialogFilename = dialog.addField(typeId(FilenameOpen));
    dialog.filenameLookupTitle("Upload from Excel");
    dialog.filenameLookupFilter(["@SYS28576",#XLSX, "@SYS28576",#XLS]);
    if (!dialog.run())
        return;
    io = new ExcelIo(dialogFilename.value(), 'r');
    setprefix("Excel Import");
    while (io.status() == IO_Status::Ok)
    {
        con = io.read();
        info(con2str(con));
    }
    io.close(); //To quit Excel
}

-------------------------------------------------------------------

Exportfile for AOT version 1.0 or later
Formatversion: 1

***Element: CLS

; Microsoft Dynamics AX Class: ExcelIo udl鎠t
; --------------------------------------------------------------------------------
  CLSVERSION 1
  CLASS #ExcelIo
    PROPERTIES
      Name                #ExcelIo
      Extends             #
      RunOn               #Called from
    ENDPROPERTIES
    METHODS
      Version: 3
      SOURCE #classDeclaration
        #class ExcelIo
        #{
        #    SysExcelApplication application;    // Excell application
        #    SysExcelCells cells;                // Excell cell object
        #    int maxRow;                         // Max number of rows, 1 based
        #    int maxCol;                         // Max number of columns, 1 based
        #    int curRow;                         // Current row number
        #}
      ENDSOURCE
      SOURCE #close
        #public void close()
        #{
        #    if (application)
        #        application.quit();
        #}
      ENDSOURCE
      SOURCE #maxCol
        #public Column maxCol()
        #{
        #    return maxCol;
        #}
      ENDSOURCE
      SOURCE #maxRow
        #public Row maxRow()
        #{
        #    return maxRow;
        #}
      ENDSOURCE
      SOURCE #new
        #public void new(str _filename, str _mode)
        #{
        #    SysExcelWorkBooks   workBooks;
        #    SysExcelWorkSheets  workSheets;
        #    SysExcelWorkSheet   workSheet;
        #    COM com1, com2;
        #    #File
        #    ;
        #    try
        #    {
        #        application = SysExcelApplication::construct();
        #        application.visible(false);
        #        workBooks   = application.workbooks();
        #        workBooks.open(_filename, 0, _mode == #io_read);
        #
        #        workSheets  = workBooks.item(1).worksheets();
        #        workSheet   = workSheets.itemFromNum(1);
        #        cells       = workSheet.cells();
        #
        #        com1   = workSheet.comObject();
        #        com1   = com1.usedRange();
        #        com2   = com1.rows();
        #        maxRow = com2.count();
        #
        #        com2   = com1.columns();
        #        maxCol = com2.count();
        #    }
        #    catch
        #    {
        #        exceptionTextFallThrough();
        #    }
        #}
      ENDSOURCE
      SOURCE #read
        #public container read()
        #{
        #    container ret;
        #    int col;
        #    ;
        #    curRow++;
        #    for (col = 1; col <= maxCol; col++)
        #    {
        #        ret += new CCADOField(cells.item(curRow,col).comObject()).value();
        #    }
        #    return ret;
        #}
      ENDSOURCE
      SOURCE #status
        #public IO_Status status()
        #{
        #    return !cells ? IO_Status::ErrorOpen : curRow < maxRow ? IO_Status::Ok : IO_Status::FileTooShort;
        #}
      ENDSOURCE
      SOURCE #writeExp
        #public boolean writeExp(container _data)
        #{
        #    throw error(Error::wrongUseOfFunction(funcName()));
        #}
      ENDSOURCE
    ENDMETHODS
  ENDCLASS

***Element: END

posted @ 2012-02-15 16:49  perock  阅读(1459)  评论(0编辑  收藏  举报