From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Directly read Excel .xlsx files using unzip to byte stream and xml processing

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

    Programming Language

  • LabVIEW G

Code and Documents

Attachment

Description

Overview

Demonstrates how to use libXML2 functions and OpenG LabVIEW ZIP Library to read the contents of an xlsx file directly.  It does not need or use MS Excel, Report Generation Toolkit, ActiveX or .Net.

Description

I got tired of using ADODB, ActiveX or .NET to read excel spreadsheets in LabVIEW.  The xlsx file is a zipped xml document.  Once you know the structure all you need to do is unzip it and parse the xml inside.  It is possible to do this purely in LabVIEW with LabVIEW primitives but it would be slow and would always require dumping the file contents to disk first.  By using the OpenG LabVIEW ZIP Library it is possible to extract the parts of the archive I need, when I need them, to a byte stream and process it in memory instead of unzipping to disk first.

Next, LabVIEW XML parsing is way too slow, so I use libxml which has been demonstrated to be hundreds of times faster.

The result is an example of how to read xlsx file quickly in LabVIEW (without having to open an instance of Excel in the background etc.).

Hardware and Software Requirements

LabVIEW development system
libXML wrapper library (https://decibel.ni.com/content/docs/DOC-30377)
OpenG Zlib library (https://vipm.jki.net/package/oglib_lvzip)

Steps to Implement or Execute Code

1. Open Excel-Read-XML_direct.vi and select an xlsx file that you wish to read in the "Spreadsheet Path" file path control.
2. Run the VI.
3. The contents of your spreadsheet should now be loaded in the "Sheets" indicator.

Additional Information or References

libxml2 http://www.xmlsoft.org/  XPATH https://www.w3.org/TR/1999/REC-xpath-19991116/

 

Additional Information

v1.1 Fixed bug with cell column reference calculation

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
林先生
Member
Member
on

thanks for share

Contributors