Differences between revisions 35 and 36
Revision 35 as of 2015-05-14 18:51:31
Size: 11154
Comment: Explanation of .ExportDataSet Added
Revision 36 as of 2015-05-15 17:25:42
Size: 11234
Comment: Method ExportDataSet corrected for empty worksheet
Deletions are marked like this. Additions are marked like this.
Line 175: Line 175:
 1. Download [[attachment:sfExcel.v1.4.txt]]  1. Download [[attachment:sfExcel.v1.5.txt]]
Line 218: Line 218:

 Version 1.5 May 2015
   Method ExportDataSet corrected for empty worksheet

sfExcel

Overview

sfExcel is a Dyalog cover class for using the Syncfusion XlsIO namespace that is a 100% native .NET library that generates fully functional Microsoft Excel Spreadsheets in native Excel format without depending on Microsoft Excel.

Syncfusion XlsIO is a perfect solution for the users who need to read and write Microsoft Excel files. It does not require Microsoft Excel to be installed in the Report generation machine or server.

Installation and deployment

The following assemblies (version 12.3 or newer) are required in the subdirectory /Syncfusion/4.5 of the Dyalog APL version 14 (or newer) directory:

  • Syncfusion.Core.dll
  • Syncfusion.Compression.Base.dll
  • Syncfusion.XlsIO.Base.dll
  • Syncfusion.Tools.Wpf.dll
  • Syncfusion.Spreadsheet.Wpf.dll
  • Syncfusion.Shared.Wpf.dll
  • Syncfusion.Grid.WPF.dll

When saving to a PDF file the following assemblies are also required:

  • Syncfusion.ExcelToPDFConverter.Base.dll
  • Syncfusion.Pdf.Base.dll

Typical Usage

Instanciating and Disposing of the class:

   xl←⎕NEW sfExcel    ⍝ Instanciate a New Workbook with the default amount of Worksheets (3).
or xl←⎕NEW sfExcel 1  ⍝ Instanciate a New Workbook with 1 Worksheet.

   xl.Dispose         ⍝ To do when finish using the class to release the memory.

Writing APL data to the spreadsheet:

  'A2'     xl.SetValue (⊂'Hello World')  ⍝ Set the value of a single cell in A1 notation.
   2 1     xl.SetValue (⊂'Hello World')  ⍝ Set the value of a single cell in RC notation.
  'D5:F8'  xl.SetValue 4 3⍴⍳12           ⍝ Set the value of a range in A1 notation.
   5 4 8 6 xl.SetValue 4 3⍴⍳12           ⍝ Set the value of a range in RC notation.

If the data is only Numbers or only Text use instead .SetNumber and .SetText that are faster:

  'A2'     xl.SetText (⊂'Hello World')   ⍝ Set the value of a single cell in A1 notation.
   2 1     xl.SetText (⊂'Hello World')   ⍝ Set the value of a single cell in RC notation.
  'D5:F8'  xl.SetNumber 4 3⍴⍳12          ⍝ Set the value of a range in A1 notation.
   5 4 8 6 xl.SetNumber 4 3⍴⍳12          ⍝ Set the value of a range in RC notation.

The method .ImportDataTable is very fast for importing a large set of number and text to the spreadsheet. However each columns must be of the same type (see also netDataTable for more info on DataTable):

   dt←xl.AplToDT 10 10⍴⍳100              ⍝ Get a .Net DataTable from the APL data.
   (row col) xl.ImportDataTable dt       ⍝ Import the DataTable at position [row;col] (upper left corner).

For dates use the method .TsToOADate that gives a number compatible with the Excel date format.

  'A3' xl.SetNumber (xl.TsToOADate ⎕TS)  ⍝ To convert ⎕TS to an OADate compatible with Excel.

For writing an array after the last row or column of the current Worksheet.

       xl.AddAfterLastRow array
       xl.AddAfterLastColumn array

Reading data from the spreadsheet to APL:

   xl.GetValue 'A2'       ⍝ Get the value of a single cell in A1 notation.
   xl.GetValue 2 1        ⍝ Get the value of a single cell in RC notation.
   xl.GetValue 'D5:F8'    ⍝ Get the value of a range in A1 notation.
   xl.GetValue 5 4 8 6    ⍝ Get the value of a range in RC notation.

The methods .GetNumber and .GetText are faster when only numbers or characters are in the range:

   xl.GetText 'A2'        ⍝ Get the value of a single cell in A1 notation.
   xl.GetText 2 1         ⍝ Get the value of a single cell in RC notation.
   xl.GetNumber 'D5:F8'   ⍝ Get the value of a range in A1 notation.
   xl.GetNumber 5 4 8 6   ⍝ Get the value of a range in RC notation.

The method .ExportDataTable is recommended for large range.

   dt←xl.ExportDataTable 'J10:S19'  ⍝ Export the spreadsheet data as a .Net data table.
      xl.DTtoApl dt                 ⍝ Convert a .Net DataTable to APL.
   ds←xl.ExportDataSet              ⍝ Export the Workbook as a DataSet (collection of DataTables).

To convert dates use .OADateToTs that convert the OA Date back to ⎕TS.

   xl.OADateToTs (xl.GetNumber 'A3')  ⍝ To convert an Excel date to ⎕TS

Methods for Formatting a range:

   range xl.SetFontBold bit         ⍝ Set a range font in bold. Bit is 0 or 1.
   range xl.SetFontItalic bit       ⍝ Set a range font in italic. Bit is 0 or 1.
   range xl.WrapText bit            ⍝ Set if text in range is wrapped. Bit is 0 or 1.

   range xl.SetFontColor color      ⍝ Set a range font color. Color can be a name or the R G B values.
   range xl.SetFontName fontName    ⍝ Set a range font name.
   range xl.SetFontSize fontSize    ⍝ Set a range font size in points.
   range xl.SetFormat format        ⍝ Set a range format (ex.: 'yy/m/d h:mm', '0.00', etc.).

   range xl.SetHAlignment alignment ⍝ Set range Horizontal alignment (Center, CenterAcrossSelection,
                                    ⍝  Distributed, Fill, General, Justify, Left, Right).
   range xl.SetVAlignment alignment ⍝ Set range Vertical alignment (Bottom, Center, Distributed, Justify, Top).

   xl.Merge range               ⍝ Merge cells in range.
   xl.AutoFitColumns range      ⍝ Adjust width of columns in range to the widest value.
   xl.AutoFitRows range         ⍝ Adjust hight of rows to the highest value.
   xl.FreezeRows noRows         ⍝ noRows = number of rows to freeze from top of worksheet.
   xl.LastColumn                ⍝ Get the last column of a worksheet.
   xl.LastRow                   ⍝ Get the last row of a worksheet.
   xl.UsedRange                 ⍝ Get the range used by the active worksheet in RC notation.

   xl.ConvertA1toRC             ⍝ Convert from A1 notation to Row and Column Index notation (RC).
                                ⍝ 'B5'     → 5 2
                                ⍝ 'D2:E10' → 2 4 10 5

   xl.ConvertRCtoA1             ⍝ Convert from Row and Column Index notation to Excel A1 notation.
                                ⍝ 5 2      → 'B5'
                                ⍝ 2 4 10 5 → 'D2:E10'

Remark for methods with a range as argument:

The method .ParseRange is used internally to obtain a Syncfusion range object with the following parameters:

  • range can be in A1 notation (ex.: 'D5:F8' or 'B2').
  • range can be in RC notation (ex.: 'D5:F8' is 5 4 8 6) or row col lastRow lastCol.
  • range can be a single positive number as the Column index base 1.
  • range can be a single negative number as the Row index base 1.
  • range and dimension() of the apl variable must match all the time.

      xl.AddWorksheet sheetName      ⍝ Add a new worksheet.
      xl.DeleteWorksheet sheet       ⍝ Delete a worksheet. sheet = SheetName or Index of the worksheet.

      xl.GetActiveWorksheetName      ⍝ Get the active sheet name of active workbook.
      xl.SetActiveWorksheet sheet    ⍝ Set active a worksheet. sheet = SheetName or Index of the worksheet.
      xl.GetWorksheetsNames          ⍝ Get all the worksheets names of active workbook.

index xl.SetWorksheetName sheetName  ⍝ Set the worksheet name. index = index of worksheet

    xl.ExportDataSet         ⍝ Export the Workbook as a DataSet (collection of DataTables).
    xl.Show                  ⍝ Show the active workbook.
    xl.Print sheet           ⍝ Print worksheet. sheet = SheetName or Index of the worksheet.
    xl.LoadFile fileName     ⍝ Load an Excel workbook file.
    xl.SaveAsCSV fileName    ⍝ Save to file the specify worksheet with the CSV format.
    xl.SaveAsStream          ⍝ Save the Current WorkBook As a Stream in .Xlsx format.
    xl.LoadFromStream        ⍝ Load an Excel Workbook from a Stream obtained by the method 'SaveAsStream'
    xl.SaveAsHtml fileName   ⍝ Save the current worksheet as fileName in .Html format.
    xl.SaveAsXls fileName    ⍝ Save the current workbook as fileName in .Xls format.
    xl.SaveAsXlsx fileName   ⍝ Save the current workbook as fileName in .Xlsx format.
    xl.SaveAsPdf fileName    ⍝ Save the current worksheet as fileName in .Pdf format.

    ⍝ Note: fileName = filename with full path and extension

There is many more methods and options available from Syncfusion.

Their complete online documentation is at http://help.syncfusion.com/wpf and look for XlsIo at the bottom of the left pane.

Known limitations

XlsIo will work fine on most simple cases but it is not a 100% perfect replacement of Microsoft Excel. For example the shapes of Excel are not supported in XlsIo.

Dyalog APL version 14.0 is shipping with version 12.1 of Syncfusion assemblies, you need to upgrade them to 12.3 otherwise not everything will work correctly.

How to install sfExcel in your workspace

  1. Download sfExcel.v1.5.txt

  2. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
  3. In your workspace execute )ed ○ sfExcel

  4. Paste (Ctrl+V) the text into the Dyalog editor
  5. Press Escape and ')save' your workspace

Test Run

If you load the sample worksheet from the book Mastering Dyalog APL from Bernard Legrand you get the following:

      xl←⎕NEW sfExcel

      xl.LoadFile 'd:\XLDemo.xls'

      7 7↑ xl.GetValue (xl.UsedRange)
       2009  [Null]   [Null]  Sales forecast  [Null]  [Null]    [Null]
 Updated on  [Null]  January              17    2008  [Null]    Source
    Country  Coffee      Tea       Chocolate    Soda   Sugar  Biscuits
    Germany    1089      783            5217    2309     643       304
      Spain  [Null]   [Null]            5420    4380     650       320
      Italy    1050      800            5500    3210     660       330
     Canada    1080      800            5620    2560  [Null]       380

      xl.Show

XLDemo.png

Version Information

 Version 1.0 November 2014, Pierre Gilbert

 Version 1.1 November 2014, Typo corrections suggested by Daniel Baronnet

 Version 1.2 November 2014, Typo corrections suggested by Daniel Baronnet

 Version 1.3 May 2015
   Methods Improved: GetNumber, GetText, GetValue, GetDisplayText
   Methods Added:    SaveAsCSV, SaveAsStream, LoadFromStream, AddAfterLastRow
                     AddAfterLastColumn, ExportDataSet
   Methods Modified: ExportDataTable, ImportDataTable

 Version 1.4 May 2015
   Methods Improved: GetNumber, GetText, ParseRange

 Version 1.5 May 2015
   Method ExportDataSet corrected for empty worksheet

Original author:

Pierre Gilbert

Responsible:

PierreGilbert

Email:

<apgil AT SPAMFREE videotron DOT ca>


CategoryDyalog - CategoryDyalogDotNet - CategorySyncfusionWpfExamples - CategoryDotNet

sfExcel (last edited 2018-05-05 12:02:42 by PierreGilbert)