Differences between revisions 5 and 6
Revision 5 as of 2014-11-09 13:33:40
Size: 5198
Comment:
Revision 6 as of 2014-11-10 12:19:50
Size: 8389
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
~-<<SeeSaw(section="table-of-contents", show="true", seesaw="false", toshow="<<(Show>> table-of-contents)", tohide="<<(Hide>> table-of-contents)", speed="Slow")>>-~
Line 3: Line 4:
Line 6: Line 6:
Line 14: Line 13:
When saving to a PDF file the following assemblies are also required:
 * Syncfusion.ExcelToPDFConverter.Base.dll
 * Syncfusion.Pdf.Base.dll
Line 15: Line 17:
=== Instanciating the class === === Instanciating and terminating the class: ===
Line 17: Line 19:
  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←⎕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.
Line 20: Line 24:
=== Writing APL data to the spreadsheet === === Writing APL data to the spreadsheet: ===
Line 22: Line 26:
  '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
  '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.
Line 29: Line 33:
  '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
  '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.
Line 34: Line 38:
The method '''.!ImportDataTable''' is very fast for importing a large set of number and text. However each columns must be of the same type: 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:
Line 36: Line 40:
   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)
   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).
Line 43: Line 47:
=== Reading APL data from the spreadsheet === === Reading data from the spreadsheet to APL: ===
Line 45: Line 49:
   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
   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.
Line 50: Line 54:
Again the methods '''.!GetNumber''' and '''.!GetText''' are faster when only numbers or characters are in the range: The methods '''.!GetNumber''' and '''.!GetText''' are faster when only numbers or characters are in the range:
Line 52: Line 56:
   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
   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.
Line 61: Line 70:
=== Methods for Formatting a range === === Methods for Formatting a range: ===
Line 63: Line 72:
  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.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.
Line 67: Line 76:
  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 (ie.: 'yy/m/d h:mm', '0.00', etc.)
  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.).
Line 72: Line 81:
  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)
  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).
Line 75: Line 84:
    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 (base 1).
    xl.LastRow ⍝ Get the last row of a worksheet (base 1).
    xl.UsedRange ⍝ Get the range used by the active worksheet in RC notation (if empty = 0 0).
   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:<<BR>>
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
=== Methods related to the worksheet: ===
{{{
    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
}}}
=== Methods related to the workbook: ===
{{{
    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.<<BR>>
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 [[attachment:sfExcel_v1.txt]] and save it to your disk.
 1. Open '''sfExcel_v1.txt''' with Notepad.
 1. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
 1. In your workspace execute ')ed ○sfExcel'
 1. Paste the text of the Notepad into the Dyalog editor
 1. 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.
{{{
Line 84: Line 144:

=== Methods related to the worksheet ===
{{{
    xl.AddWorksheet sheetName ⍝ Add a new worksheet
    xl.DeleteWorksheet sheet ⍝ Delete a worksheet. sheet = SheetName or Index (base 0) of the worksheet

}}}

Introduction

(Hide table-of-contents) 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)