Add multiple Excel worksheets to single Excel workbook – Dynamics NAV 2013

In Microsoft Dynamics NAV 2013 Excell Buffer table (370) changed from using Excel Automation variable to Open XML 2.0 DotNet interoperability classes. More information about that can be found on NAV Team blog in article Excel Buffer Using Open XML Instead of Excel Automation.

The one function that is needed, in real life, when exporting data to Excel workbook is to export data to the same Excel workbook but to different worksheets.

Let say you want to export Item and Item Ledger Entry table to single Excel workbook. I haven’t found any function in ExcelBuffer that does that – correct me if I am wrong.

So I decided to write function that enables just that, open single Excel workbook and just adds new worksheet one after another. In Excel Buffer table (370) I have created AddNewSheet function that receives only one parameter (SheetName).

<br />
AddNewSheet(SheetName : Text[250])<br />
IF SheetName = '' THEN<br />
  ERROR(Text002);</p>
<p>IF ISNULL(XlWrkBkWriter) THEN BEGIN<br />
  CreateBook(SheetName);<br />
  ActiveSheetName := SheetName; //first sheet activated<br />
END ELSE BEGIN<br />
  WHILE XlWrkBkWriter.HasWorksheet(SheetName) DO BEGIN<br />
    IF NOT FirstIncrement THEN BEGIN<br />
      SheetName := SheetName + '-01';<br />
      FirstIncrement := TRUE;<br />
    END ELSE BEGIN<br />
      SheetName := INCSTR(SheetName);<br />
    END;<br />
  END;<br />
  XlWrkShtWriter := XlWrkBkWriter.AddWorksheet(SheetName);<br />
  FirstIncrement := FALSE;<br />
  //ActiveSheetName := SheetName; //last sheet activated<br />
END;<br />
WriteSheet('',COMPANYNAME,USERID);<br />
DELETEALL;<br />
ClearNewRow;<br />

Additional variable that I have used is FirstIncrement (Boolean) – all other are global variables in Excel Buffer table.

Let me explain few key points in this code. First in line 5 we check if the WorkSheetWriter is initialized. If it is not initialized we call standard CreateBook function and set the ActiveSheetName to that “first” excel sheet.

In line 9 we check if there is sheet in workbook that has the same name as the one we have passed as parameter – which can happen in subsequent calls. If this is true then we append ‘-01’ so we can use INCSTR(SheetName) until we find unused SheetName – so we don’t overwrite data on existing sheet.

Then the main task of this function is executed  in line 17 when we append new worksheet to currently open workbook.

In line 19 I have commented out line of code that sets the last sheet that we call to be initially shown when we open Excel workbook. I really want the first worksheet to be shown but you might want it the other way.

Finally, we WriteSheet and delete all data currently in Excel Buffer.

When the code, that populates data in Excel Buffer for single sheet, is finished we have to call this function to write sheet and flush all the data so it is free for another sheet.

Also, there is a difference that we cannot use standard function CreateBookAndOpenExcel – because it is used for single worksheet case scenario – but we must “close” Excel manually by calling standard functions CloseBook, OpenExcel, GiveUserControl on Excel Buffer.

Test codeunit that simulates usage of this function follows.

<br />
OBJECT Codeunit 50000 Test AddNewSheet<br />
{<br />
  OBJECT-PROPERTIES<br />
  {<br />
    Date=27.08.14;<br />
    Time=10:58:00;<br />
    Modified=Yes;<br />
    Version List=;<br />
  }<br />
  PROPERTIES<br />
  {<br />
    OnRun=BEGIN<br />
            CreateItemSheet;<br />
            CreateItemLedgerEntrySheet;<br />
            CreateItemSheet;<br />
            CreateItemSheet;<br />
            CreateItemLedgerEntrySheet;<br />
            CreateItemLedgerEntrySheet;<br />
            CreateItemSheet;<br />
            CreateItemSheet;<br />
            CreateItemLedgerEntrySheet;<br />
            CreateItemLedgerEntrySheet;</p>
<p>            ExcelBuff.CloseBook;<br />
            ExcelBuff.OpenExcel;<br />
            ExcelBuff.GiveUserControl;<br />
          END;</p>
<p>  }<br />
  CODE<br />
  {<br />
    VAR<br />
      Item@1002 : Record 27;<br />
      ItemLedgerEntry@1001 : Record 32;<br />
      ExcelBuff@1000 : TEMPORARY Record 370;</p>
<p>    PROCEDURE CreateItemSheet@1();<br />
    BEGIN<br />
      ExcelBuff.AddColumn(FORMAT(Item.FIELDCAPTION(&quot;No.&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(Item.FIELDCAPTION(Description)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(Item.FIELDCAPTION(&quot;Base Unit of Measure&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(Item.FIELDCAPTION(Inventory)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.NewRow;</p>
<p>      Item.RESET;<br />
      IF Item.FINDSET THEN REPEAT<br />
        Item.CALCFIELDS(Inventory);<br />
        ExcelBuff.AddColumn(FORMAT(Item.&quot;No.&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.AddColumn(FORMAT(Item.Description),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.AddColumn(FORMAT(Item.&quot;Base Unit of Measure&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.AddColumn(FORMAT(Item.Inventory),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.NewRow;<br />
      UNTIL Item.NEXT = 0;</p>
<p>      ExcelBuff.AddNewSheet(Item.TABLECAPTION);<br />
    END;</p>
<p>    PROCEDURE CreateItemLedgerEntrySheet@2();<br />
    BEGIN<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(&quot;Item No.&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(&quot;Posting Date&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(&quot;Document No.&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(Quantity)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(&quot;Remaining Quantity&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.FIELDCAPTION(&quot;Entry No.&quot;)),FALSE,'',TRUE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
      ExcelBuff.NewRow;</p>
<p>      ItemLedgerEntry.RESET;<br />
      ItemLedgerEntry.SETCURRENTKEY(&quot;Item No.&quot;,&quot;Posting Date&quot;);<br />
      IF ItemLedgerEntry.FINDSET THEN REPEAT<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.&quot;Item No.&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.&quot;Posting Date&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Date);<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.&quot;Document No.&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Text);<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.Quantity),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Number);<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.&quot;Remaining Quantity&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Number);<br />
        ExcelBuff.AddColumn(FORMAT(ItemLedgerEntry.&quot;Entry No.&quot;),FALSE,'',FALSE,FALSE,FALSE,'',ExcelBuff.&quot;Cell Type&quot;::Number);<br />
        ExcelBuff.NewRow;<br />
      UNTIL ItemLedgerEntry.NEXT = 0;</p>
<p>      ExcelBuff.AddNewSheet(ItemLedgerEntry.TABLECAPTION);<br />
    END;</p>
<p>    BEGIN<br />
    {<br />
      ZORAN - Excel buffer AddNewSheet Test<br />
    }<br />
    END.<br />
  }<br />
}<br />

I hope this helps someone and please feel free to comment.

 

Leave a Reply

Your email address will not be published. Required fields are marked *