Simple Read/Write Excel Data

In a Nav forum there was a question, how to read data from an excel document, change the data in Nav, write data back to the excel document without changing the existing cell formatting. With table ExcelBuffer and the helper assemblies delivered with Nav this cannot be done, because the Write functions e.g. SetCellValueText work with decorators.

So i developed assembly XlsLib. With that assembly read and write actions from an excel document are done very easy. The provided methods are very simple. You can read the cell values, the basic number formats and save it to table Excel Buffer. When writing only the value as is is written back to the cell. No further formatting is read or written.That can also be used, when you work with templates.

Main functions:

XlsWorksheet.GetValue(rowId, colId, value, type):
– Get a value from cell rowId/colId,
– given value is of type text,
– type is of datatype integer, can be 0-3 according option field NumberFormat in table ExcelBuffer, Type=4 means Unknown

XlsWorksheet.SetValue(rowId, colId, value):
– Sets a value in cell rowId/colId. value can be of any type.

Here is a sample code:

TestXlsLib()// variables//  XlsWorkbook DotNet XlsLib.Workbook.'XlsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38613a2311532c9a'// XlsWorksheet DotNet XlsLib.Worksheet.'XlsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38613a2311532c9a'// value Text// type Integer// open excel workbookXlsWorkbook := XlsWorkbook.Workbook('c:\temp\test.xlsx');// select sheet 'Sheet2'XlsWorksheet := XlsWorksheet.Worksheet(XlsWorkbook,'Sheet2');// read cell values from row=1, column=1-3XlsWorksheet.GetValue(1, 1, value, type); // e.g. 1234XlsWorksheet.GetValue(1, 2, value, type); // e.g. 10.10.1990XlsWorksheet.GetValue(1, 3, value, type); // e.g. 99.12// Write some valuesXlsWorksheet.SetValue(1, 1, 5432);  // an integer value againXlsWorksheet.SetValue(1, 2, 010175D); // again a date valueXlsWorksheet.SetValue(1, 3, 34.45); // a decimal value// read values again to check changesXlsWorksheet.GetValue(1, 1, value, type);XlsWorksheet.GetValue(1, 2, value, type);XlsWorksheet.GetValue(1, 3, value, type);// Save: Save to loaded file filename//XlsWorkbook.Save();// SaveAs: create a copy, save result to new fileXlsWorkbook.SaveAs('c:\temp\test2.xlsx');// Close Excel App !!XlsWorkbook.Close();// if an error occurs ...IF XlsWorkbook.Error <> '' THEN  MESSAGE(XlsWorkbook.Error);// release memoryclear(XlsWorkbook);

You can download XlsLib here.

Hint: When downloading that file, it can be, that you have troubles using it because of security issues, e.g. dll is not visible in list of assemblies, you get an error message like “Cannot open xlslib, because one or more dependencies are not met”, methods are not visible or something else. then start windows explorer, goto folder were you copied the lib files, view file properties. if you see a notice at the bottom of the file properties window like “file is blocked because downloaded from internet, grant access?”, then click on “grant access”.

cheers