Quantcast
Channel: Microsoft Dynamics AX Forum - Recent Threads
Viewing all articles
Browse latest Browse all 175888

RE: Import Excel file through batch job in AX 2012 R3

$
0
0

Hi Kavitha,

I have copied over three methods from the class which I have used to export data to excel in Batch.

Run on property must be set to server for this class.

You can add methods to class for running in batch and modify the execute method accordingly to make it work for you.

public static void main(Args args)

{

   AssetTrans assetTrans;

   SysExcelApplication application;

   SysExcelWorkBooks workbooks;

   SysExcelWorkBook   workbook;

   SysExcelWorksheets worksheets;

   sysExcelWorksheet   worksheet;

   SysExcelCells            cells;

   SysExcelCell              cell;

   int                             row;

   DimensionAttributeValueSetItemView dimAttrSet;

   DimensionAttribute dimAttr;

   str                            dimAttrStr;

   Map                         dims;

   int                            dimNum;

   ExcelExport Export = ExcelExport::newImportServer();

   ;

   Export.init();

   if (Export.prompt())

   {

       Export.run();

   }

}

public void run()

{

       setPrefix(ExcelExport::description());

       if (this.validate())

       {

           this.execute();

           info ("@SYS70406");

       }

}

private void execute()

{

   boolean                             fileFound;

   boolean                             fileOk;

   AssetTrans                         assetTrans;

   int                                      row;

   DimensionAttributeValueSetItemView  dimAttrSet,dimAttrSetMapVar;

   DimensionAttribute         dimAttr,dimAttrMapVar;

   str                                     dimAttrStr;

   Map                                  dims;

   int                                     dimNum;

   str                                     pathstr;

   anytype                            amountstr;

   str                                     ak;

   str                                    MarketSalesRegion, costCenter, Product, SalesRep,Project;

   Map                                 map;

   MapEnumerator              mapEnumerator;

   NameAlias                       nameAlias;

   int                                    counter = 1;  // start map with 1

   // Below custom table contians dimension names

   AssetTransExcelExport   AssetTransExcelExport,AssetTransExcelExport_del, AssetTransExcelExport_DimName, AssetTransExcelExport_DisplayVal;

   #define.ReadWritePermission('RW')

   #define.ExcelColumnWidth(08) // was 15

   #define.ExcelCellFontSize("Microsoft.Dynamics.AX.Fim.Spreadsheets.CellFontSize")

   #define.Size9("Size9")

   Microsoft.Dynamics.AX.Fim.Spreadsheets.Spreadsheet spreadsheet;

   Microsoft.Dynamics.AX.Fim.Spreadsheets.CellProperties cellProperties;

   Microsoft.Dynamics.AX.Fim.Spreadsheets.ColumnProperties columnProperties;

   void addColumn(str _name)

   {

       columnProperties = new Microsoft.Dynamics.AX.Fim.Spreadsheets.ColumnProperties();

       columnProperties.set_Width(#ExcelColumnWidth);

       spreadSheet.InstantiateColumn(columnProperties);

       cellProperties = new Microsoft.Dynamics.AX.Fim.Spreadsheets.CellProperties();

       cellProperties.set_FontSize(CLRInterop::parseClrEnum(#ExcelCellFontSize, #Size9));

       cellProperties.set_Bold(true);

       spreadSheet.AddStringCellToWorkbook(_name, cellProperties);

   }

   map = new Map(Types::String, Types::Integer);

   select firstOnly * from assetParameters;

   filepath = this.Modifyfilename(assetParameters.AssetTransExcelExportPath);

   new FileIOPermission(filepath, #ReadWritePermission).assert();

   spreadSheet = new Microsoft.Dynamics.AX.Fim.Spreadsheets.Spreadsheet();

   if (!spreadSheet.CreateSpreadsheet(filepath))

   {

       spreadSheet.Dispose();

       throw error(strFmt("@SYS72245", filepath));

   }

   addColumn("Voucher");

   addColumn("Transaction date");

   addColumn("Fixed asset number");

   addColumn("Transaction type");

   addColumn("Amount");

   addColumn("Fixed asset group");

   ttsBegin;

   delete_from AssetTransExcelExport_del;

   ttsCommit;

   ttsBegin;

   select forUpdate AssetTransExcelExport_DimName;

   while  select  dimAttrSetMapVar

         join Name from dimAttrMapVar

               group by name

               where dimAttrMapVar.RecId == dimAttrSetMapVar.DimensionAttribute

   {

         nameAlias = dimAttrMapVar.Name;

         if (!map.exists(nameAlias))

         {

            map.insert(nameAlias, counter);

            counter++;

         }

         AssetTransExcelExport_DimName.Name = dimAttrMapVar.Name;

         AssetTransExcelExport_DimName.doInsert();

    }

   ttsCommit;

   mapEnumerator = map.getEnumerator();

   while (mapEnumerator.moveNext())

   {

      addcolumn(mapEnumerator.currentKey());

   }

  while select * from assetTrans

  {

     spreadSheet.MoveToNextRowInWorkbook();

     cellProperties = new Microsoft.Dynamics.AX.Fim.Spreadsheets.CellProperties();

     cellProperties.set_FontSize(CLRInterop::parseClrEnum(#ExcelCellFontSize, #Size9));

     spreadSheet.AddStringCellToWorkbook(assetTrans.Voucher, cellProperties);

     spreadSheet.AddStringCellToWorkbook(date2str(assetTrans.TransDate,123,2,DateSeparator::Slash,2,DateSeparator::Slash,4), cellProperties);

     spreadSheet.AddStringCellToWorkbook(assetTrans.AssetId,cellProperties);

     spreadsheet.AddStringCellToWorkbook(enum2str(assetTrans.TransType), cellProperties);

     spreadSheet.AddStringCellToWorkbook(num2str(assetTrans.AmountCur,0,2,1,2),cellProperties);

     spreadsheet.AddStringCellToWorkbook(assetTrans.AssetGroup, cellProperties);

     while select dimAttrSet

           where dimAttrSet.DimensionAttributeValueSet == assetTrans.DefaultDimension

               join Name from dimAttr

                   order by Name

                   where dimattr.RecId == dimAttrSet.DimensionAttribute

     {

                select forUpdate firstOnly AssetTransExcelExport_DisplayVal

                   where AssetTransExcelExport_DisplayVal.Name ==  dimAttr.Name;

               {

                    ttsBegin;

                    AssetTransExcelExport_DisplayVal.DisplayValue= dimAttrSet.DisplayValue;

                    AssetTransExcelExport_DisplayVal.doUpdate();

                   ttsCommit;

               }

      }

       while select * from AssetTransExcelExport

       {

            spreadsheet.AddStringCellToWorkbook(AssetTransExcelExport.DisplayValue, cellProperties);

       }

       // clear table for next record

       ttsBegin;

      while select forUpdate * from AssetTransExcelExport_del

       {

           AssetTransExcelExport_del.DisplayValue = "";

           AssetTransExcelExport_del.doUpdate();

       }

       ttsCommit;

  }

   spreadSheet.WriteFile();

   spreadSheet.Dispose();

   CodeAccessPermission::revertAssert();

}


Viewing all articles
Browse latest Browse all 175888

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>