Differences between revisions 7 and 8
Revision 7 as of 2014-11-10 12:35:06
Size: 8412
Comment:
Revision 8 as of 2014-11-10 12:39:04
Size: 8268
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<TableOfContents>>
Line 2: Line 3:
~-<<SeeSaw(section="table-of-contents", show="true", seesaw="false", toshow="<<(Show>> table-of-contents)", tohide="<<(Hide>> table-of-contents)", speed="Slow")>>-~

Introduction

sfExcel is a 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 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

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

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

Typical Usage

Instanciating and terminating 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:

   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)

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.

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 (if empty = 0 0).
   
   xl.ConvertA1toRC             ⍝ Convert from Excel 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 method 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

    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 specific 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.Show         ⍝ Show the active workbook.
    xl.Print        ⍝ Print the active worksheet. 
    xl.LoadFile     ⍝
    xl.SaveAsXls
    xl.SaveAsXlsx
    xl.SaveAsXml
    xl.SaveAsPdf
    xl.SaveAsHtml

There is many more methods and options available from Syncfusion.
Their online documentation is at http://help.syncfusion.com/wpf and look for XlsIo in the left pane.

Knowned 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.

How to install sfExcel in your workspace

  1. Download sfExcel_v1.txt and save it to your disk.

  2. Open sfExcel_v1.txt with Notepad.

  3. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
  4. In your workspace execute ')ed ○sfExcel'
  5. Paste the text of the Notepad into the Dyalog editor
  6. Press Escape and ')save' your workspace

Share your snipplets of code !

You can share the methods that were inspired by this class here.<<br>> Just edit this section yourself with your method(s) or alternatively you can send me an email and I will add them to this section.

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