libxlsxwriter
|
Here are some instructions to get you up and running with the libxlsxwriter library on different OSes.
If you prefer to assemble Ikea furniture first and only read the instructions when you have parts left over then the following minimal set of commands should get you up and running on a Debian like system:
sudo apt-get install -y zlib1g-dev git clone https://github.com/jmcnamara/libxlsxwriter.git cd libxlsxwriter make sudo make install
If you read the instructions first and then assemble the furniture you will know how to proceed.
The only non-optional dependency when building libxlsxwriter is Zlib.
You can install zlib
from source as follows:
curl -O -L http://www.zlib.net/zlib-1.2.11.tar.gz tar zxf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make sudo make install
Alternatively, you can use your OS packager to install the zlib
development libraries. For example:
sudo apt-get install -y zlib1g-dev
The zlib version must be >= 1.2.8 to avoid compilation issues.
To get the latest version of the source code you can clone the libxlsxwriter repository from GitHub:
git clone https://github.com/jmcnamara/libxlsxwriter.git
Alternatively you can get a tarball of the latest source code as follows:
curl -O -L http://github.com/jmcnamara/libxlsxwriter/archive/master.tar.gz
Build the source code as follows:
cd libxlsxwriter make
This will create a static and dynamic library in the local ./lib
directory:
ls lib libxlsxwriter.a libxlsxwriter.so
To see a verbose summary of the compilation steps use V=1
:
make V=1
With CMake you can build the library as follows:
cd cmake # Or another sub-directory. cmake .. cmake --build .
If there weren't any warnings or errors in the previous step (and there shouldn't have been) then you can build the programs in the examples
directory and try one of them out:
# With Make: make examples # or CMake: cd cmake cmake .. -DBUILD_EXAMPLES=ON cmake --build . # Then: ./examples/hello
This will create a hello_world.xlsx
file in your current directory. Open the file in a spreadsheet application. The output should look like this:
Libxlsxwriter supports a simplified installation scheme for a static and dynamic/shared library and header files.
sudo make install
The files are installed to /usr/local
by default but this can be overridden by using the PREFIX
environmental variable:
make install PREFIX=/usr/third_party
A staging directory can be set with DESTDIR
which is prepended to all install paths. This is a an occasionally useful feature for packaging:
make install PREFIX=/usr/third_party DESTDIR=./staging/
This would build and link the code with /usr/third_party
as the installation location but actually install to ./staging/usr/third_party
.
With CMake you can install the library as follows:
cd cmake cmake .. cmake --build . --target install
Using your source code editor create a file like the following called myexcel.c
:
If you executed the install commands in the previous section then you should be able to compile the program as follows:
cc myexcel.c -o myexcel -lxlsxwriter
In some OS environments, or if you changed the PREFIX
location, you may have to provide explicit include
and lib
paths:
cc myexcel.c -o myexcel -I/usr/local/include -L/usr/local/lib -lxlsxwriter
You can also use pkg-config (after installation of the library) to automatically determine the required arguments and paths:
$ pkg-config --cflags --libs xlsxwriter -I/usr/local/include -L/usr/local/lib -lxlsxwriter -lz
You can add this information to your compilation as follows:
cc myexcel.c -o myexcel `pkg-config --cflags --libs xlsxwriter`
This will create an executable that you can run to generate an Excel spreadsheet:
./myexcel xdg-open myexcel.xlsx
If the installation didn't work for you then you can link against the static library you created in the "Build the source code" step:
cc myexcel.c -o myexcel -I /path/to/libxlsxwriter/include \ /path/to/libxlsxwriter/lib/libxlsxwriter.a -lz
The easiest way to install libxlsxwriter for Xcode and iOS is to use the CocoaPods method shown in the next section.
To install libxlsxwriter from the macOS commandline requires the Xcode "commandline tools". You can then follow the same instructions for compiling and installing on Linux, as shown above.
For commandline access to libxlsxwriter you can also use brew/homebrew, see below.
For iOS and macOS projects in Xcode you can install libxlsxwriter using CocoaPods.
Add the following entry to your Podfile
:
pod 'libxlsxwriter', '~> 0.9'
if you are using Swift, you can now add an import:
import xlsxwriter
And call its C functions like this:
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) let fileURL = documentDirectory.appendingPathComponent("hello_world.xlsx") let workbook = workbook_new((fileURL.absoluteString.dropFirst(6) as NSString).fileSystemRepresentation) let worksheet = workbook_add_worksheet(workbook, nil) worksheet_write_string(worksheet, 0, 0, "Hello", nil) worksheet_write_number(worksheet, 1, 0, 123, nil) workbook_close(workbook)
For a sample Xcode project that uses the libxlsxwriter cocoapod for iOS and macOS with Objective-C and Swift see libxlsxwriter Cocoa Examples or LibXlsxWriterSwiftSample.
On macOS you can also use brew/homebrew:
brew install libxlsxwriter
Once installed you can compile and run a libxlsxwriter program as follows:
cc myexcel.c -o myexcel -I/usr/local/include -L/usr/local/lib -lxlsxwriter ./myexcel
There are several ways to compile libxlsxwriter on and for Windows, see below.
The most convenient way to get the latest release version of libxlsxwriter and integrate it into your Visual Studio build environment is to use the vcpkg tool:
vcpkg is a free C/C++ package manager for acquiring and managing libraries. Choose from over 1500 open source libraries to download and build in a single step or add your own private libraries to simplify your build process. Maintained by the Microsoft C++ team and open source contributors.
Install vcpkg and libxlsxwriter as follows in Windows CMD or Powershell:
git clone https://github.com/microsoft/vcpkg.git cd vcpkg bootstrap-vcpkg.bat vcpkg install libxlsxwriter
You should then see libxlsxwriter installed as follows (note that the required zlib dependency has also been installed):
vcpkg list libxlsxwriter:x86-windows 0.8.6-1 Libxlsxwriter is a C library that ... zlib:x86-windows 1.2.11-5 A compression library
You can also install libxlsxwriter for other build targets like x64
vcpkg install libxlsxwriter:x64-windows vcpkg list libxlsxwriter:x64-windows 1.1.4 Libxlsxwriter is a C library that ... libxlsxwriter:x86-windows 1.1.4 Libxlsxwriter is a C library that ... zlib:x64-windows 1.2.11#13 A compression library zlib:x86-windows 1.2.11#13 A compression library
To use libxlsxwriter from within Visual Studio you can "integrate" it into your environment:
vcpkg integrate install Applied user-wide integration for this vcpkg root.
All MSBuild C projects can now include libxlsxwriter directly. Linking will also be handled automatically. For example, create a new Win32 Console (or other C/C++) application in Visual Studio:
File -> New -> Project Visual C++ -> Win32 -> Win32 Console Application
Replace the empty main with a libxlsxwriter example from the distro. For example:
Change the target to "Release" and the architecture to "x86" or "x64" (depending on the version you installed above). You can now "Build Solution". The resulting executable will be put in the output directory with the required "xlsxwriter.dll" and "zlib1.dll" files.
For a more manual installation, and for older versions of Microsoft Visual Studio or Windows, you can use CMake as shown below.
Open a Windows CMD or Command Window and set up your MSVC environment, if required. Something like this:
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" # Or: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
Then create a work directory and an install directory that the include and library files will be installed to. Set the follow variables to point to the directories:
set WORK_DIR=C:/Users/Username/tmp set INSTALL_DIR=C:/Users/Username/tmp/install_dir
Build the Zlib library:
cd %WORK_DIR% git clone https://github.com/madler/zlib.git cd zlib mkdir build cd build cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/zlib" cmake --build . --config Release --target install
Build the libxlsxwriter library:
cd %WORK_DIR% git clone https://github.com/jmcnamara/libxlsxwriter.git cd libxlsxwriter mkdir build cd build cmake .. -G "Visual Studio 14 Win64" -DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%/libxlsxwriter" -DZLIB_ROOT:STRING="%INSTALL_DIR%/zlib" cmake --build . --config Release --target install
Create a new Win32 Console (or other C/C++) application in Visual Studio:
File -> New -> Project Visual C++ -> Win32 -> Win32 Console Application
Change the ARCH in the main Dialog to "x64" and the Configuration to "Release" (or to match the parameters to cmake).
Replace the empty main with a libxlsxwriter example from the distro. Make sure to include "stdafx.h" and "xlsxwriter.h":
Edit the application properties:
Project -> ConsoleApplication Properties
Set the libxlsxwriter include path to match the path used above:
Configuration Properties -> C/C++ -> General -> Additional Include Directories Set it to the following (or similar path used above): C:\Users\Username\tmp\install_dir\libxlsxwriter\include
Set the linker directories to match the path created above:
Configuration Properties -> Linker -> General -> Additional Library Directories Add the following (or similar paths used above): C:\Users\Username\tmp\install_dir\libxlsxwriter\lib\x64\Release C:\Users\Username\tmp\install_dir\zlib\lib
Set the linker additional libraries to match the zlib and xlsxwriter libs created above:
Configuration Properties -> Linker -> Input -> Additional Dependencies Add the following: xlsxwriter.lib zlib.lib
Build the solution and run the output executable. It should create a hello_world.xlsx file in the same directory you ran it from.
The libxlsxwriter library can also be compiled on Windows using the Mingw-w64 "Minimalist GNU for Windows" toolchain. These tools can be run from the Windows cmd.exe
but it is recommended to use the MSYS2 "Minimal System" Bourne Shell.
Here are some instructions on how to compile libxlsxwriter with Mingw-w64 and MSYS2:
# Install MSYS2 64 or 32 bit from http://msys2.github.io/ # Install the dev tools for libxlsxwriter. pacman -S git gcc make zlib-devel # Clone and build libxlsxwriter. git clone https://github.com/jmcnamara/libxlsxwriter.git cd libxlsxwriter/ make
By default the library is installed in /usr/local
on MinGW/MSYS systems. If you know how to extend your build environments to use that directory then you can just run make install
. However, it is generally causes less compile/link issues if you install them in the /usr
directory like this:
make install PREFIX=/usr
After compilation you can follow the instructions in the Using the library section above. When compiling with the library you may also need to link against the zlib library using -lz
:
gcc myexcel.c -o myexcel -lxlsxwriter -lz
It is also possible to use Cygwin and the older MinGW and MSYS. Libxlsxwriter has been confirmed to compile and work in all of these environments.
See also Specifying a TEMP directory for libxlsxwriter.
The following external guide shows how to Build libxlsxwriter inside Qt-Creator for Windows with step by step instructions.
Installation on FreeBSD and OpenBSD is mainly the same as on Linux. To compile the library get the source code and build it using gmake (not make):
cd libxlsxwriter gmake
Then follow the instructions in the Linux section to install and use the library.
Both FreeBSD and OpenBSD come with the zlib development libraries pre-installed so there are no additional dependencies. However, if you have any issues then follow the instructions to install zlib.
As shown in the previous sections Libxlsxwriter provides both a Make and CMake based build system. The Make build is a straightforward system for building the library and running tests on Unix like systems. The CMake system offers support for more operating systems, cross compilation, and integration with larger CMake builds. In particular it enables building on Windows.
The following are various compilation targets and options for both build systems:
Make | CMake | Description |
---|---|---|
examples | -DBUILD_EXAMPLES=ON | Build the example |
test | -DBUILD_TESTS=ON | Build the tests |
USE_DTOA_LIBRARY=1 | -DUSE_DTOA_LIBRARY=ON | Use alternative double in sprintf |
USE_MEM_FILE=1 | -DUSE_MEM_FILE=ON | Use fmemopen() /open_memstream() instead of temp files |
USE_OPENSSL_MD5=1 | -DUSE_OPENSSL_MD5=ON | Use OpenSSL for MD5 digest |
USE_NO_MD5=1 | -DUSE_NO_MD5=ON | Don't use a MD5 digest |
USE_SYSTEM_MINIZIP=1 | -DUSE_SYSTEM_MINIZIP=ON | Use system minzip library |
USE_STANDARD_TMPFILE=1 | -DUSE_STANDARD_TMPFILE=ON | Use system tmpfile() function |
USE_BIG_ENDIAN=1 | -DUSE_BIG_ENDIAN=ON | Build on big endian systems |
universal_binary | -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" | Create a macOS "Universal Binary" |
-DBUILD_SHARED_LIBS=ON | Build shared library (default on) | |
-DUSE_STATIC_MSVC_RUNTIME=ON | Use static msvc runtime library | |
-DCMAKE_BUILD_TYPE=Release | Set the build type. |
The compilation options would be used as follows:
# Make make examples USE_DTOA_LIBRARY=1 # CMake cd cmake cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=ON -DUSE_DTOA_LIBRARY=ON cmake --build . --config Release
Each of the options are explained below:
examples/BUILD_EXAMPLES
: Builds the example programs.test/BUILD_TESTS
: Builds the tests (see Running the Test Suite). With Make the tests are also run once they are compiled. With CMake you can run them using ctest
.USE_DTOA_LIBRARY
: See using a double formatting library.USE_MEM_FILE
: Use fmemopen()/open_memstream() instead of temporary files. This option isn't on by default since it isn't supported on Windows.USE_OPENSSL_MD5
: Uses OpenSSL to provide a MD5 digest of image files in order to avoid storing duplicates. See MD5 functionality for handling duplicate images.USE_NO_MD5
: Don't use a MD5 digest of image files in order to remove duplicates. This can be used if you aren't handling image files and don't need the additional function in the library. See MD5 functionality for handling duplicate images.USE_SYSTEM_MINIZIP
: Uses a system minizip library, rather than the included copy, to create the xlsx zip container. See Linking against system minizip.USE_STANDARD_TMPFILE
: Uses the standard library tmpfile()
function to handle temp files instead of tmpfileplus
. See Specifying a TEMP directory for libxlsxwriter.USE_BIG_ENDIAN
: Compiles libxlsxwriter on a big endian system. See Compiling on Big Endian Architecture.universal_binary/CMAKE_OSX_ARCHITECTURES
: Builds a "universal binary" for both Apple silicon and Intel-based Macs. See Compiling a universal binary on macOS.BUILD_SHARED_LIBS
: Builds a dynamically loading version of the library (.so
, .dll
or .dylib
depending on the operating system).USE_STATIC_MSVC_RUNTIME
: Sets flags for Microsoft Visual C to use a static MSVC runtime.CMAKE_BUILD_TYPE
: Sets the build type (generally Release
or Debug
).You can view, and set, your CMake options from within your build directory using the ccmake
tool:
ccmake .
Excel uses an IEEE 754 doubles for all numeric values. These values are stored in standard sprintf(...,"%.16G",...)
formatting as numbers like "1234.56" or "456E+123". However in some locales, such as "de_DE" these numbers can be stored with the locale specific decimal place like "1234,56" which causes Excel to give an error when it loads the file.
It some cases this issue can be resolved by using the setlocale()
or uselocale()
functions in your application. Alternatively you can compile libxlsxwriter with support for a third party dtoa()
(decimal to ascii) function. Currently libxlsxwriter uses the Milo Yip DTOA library as an optional compilation. This avoids the locale sprintf issue and it is also 40-50% faster than the standard dtoa for raw numeric data.
Libxlsxwriter uses a an MD5 digest to avoid including duplicate image files in the xlsx file. By default it uses a third party library, Openwall MD5, which is a fast portable implementation of the MD5 Algorithm and which uses the same function prototypes as OpenSSL MD5 digest. See License.
The Openwall MD5 code is included in the libxlsxwriter repo and compiled in by default. If you don't want to use this code, and the additional license, you can use OpenSSL's MD5 functions dynamically by using the USE_OPENSSL_MD5
option:
make USE_OPENSSL_MD5=1 # or: cmake .. -DUSE_OPENSSL_MD5=ON
This requires that you have the OpenSSL development libraries installed and on paths known to your compiler.
If this MD5 functionality isn't required it is possible to compile libxlsxwriter without image de-duplication by using the USE_NO_MD5=1
option:
make USE_NO_MD5=1 # or: cmake .. -DUSE_NO_MD5=ON
Libxlsxwriter uses the minizip
component of Zlib to create the xlsx zip file container. The source files for minizip
are included in the src tree of libxlsxwriter and are statically linked by default.
If you have a lminizip
library already installed on your system and prefer to dynamically link against that you can use the following compilation option:
make USE_SYSTEM_MINIZIP=1 # or: cmake .. -DUSE_SYSTEM_MINIZIP=ON
The libxlsxwriter library creates temporary files in the system TEMP
directory during assembly of an xlsx file. On Windows this directory may not be writeable by a libxlsxwriter application (although it will try several TEMP
locations before returning an error). To work around this you can set the tmpdir
parameter of the lxw_workbook_options struct and pass it to workbook_new_opt()
:
This can also be used on Unix systems where the TEMP
directory isn't writeable.
The TEMP file handling with optional temporary directory support is provided by the Tmpfileplus library which is included in the source tree. If you wish to use the standard library tmpfile()
function instead you can compile without tmpfileplus
as follows:
make USE_STANDARD_TMPFILE=1 # or: cmake .. -DUSE_STANDARD_TMPFILE=ON
tmpfile()
the tmpdir
parameter, shown above, is ignored.Libxlsxwriter can be compiled on a big endian system as follows:
make USE_BIG_ENDIAN=1 # or: cmake .. -DUSE_BIG_ENDIAN=ON
With Xcode 12.2 and later you can compile libxlsxwriter as a "universal binary" for both Apple silicon and Intel-based Macs, i.e., arm64 and x86_64.
You can compile a universal binary with standard make as follows:
$ make universal_binary # Which gives: $ lipo -archs lib/libxlsxwriter.a x86_64 arm64 $ lipo -archs lib/libxlsxwriter.dylib x86_64 arm64
Or with CMake:
cd cmake cmake .. -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" make
As explained in the previous sections Libxlsxwriter includes 3 additional libraries within the third_party
directory:
md5
: Used for MD5 hashing to avoid including duplicate images. See MD5 functionality for handling duplicate images.minizip
: Uses creating the xlsx zip container (this is part of the zlib code base but generally packaged separately). See Linking against system minizip.tmpfileplus
: Used mainly to overcome temp file issues on Windows but also used for changing the default temp directory. See Specifying a TEMP directory for libxlsxwriter.These components are included in the libxlsxwriter repository to ensure that the library compiles and runs on as many OSes as possible with the least amount of additional dependencies.
However, all of these are optional and a minimal version of libxlsxwriter can be compiled without them by relying on external libraries as follows:
# Make: make USE_OPENSSL_MD5=1 USE_SYSTEM_MINIZIP=1 USE_STANDARD_TMPFILE=1 # CMake: cd cmake cmake .. -DUSE_OPENSSL_MD5=ON -DUSE_SYSTEM_MINIZIP=ON -DUSE_STANDARD_TMPFILE=ON cmake --build .
Once you get libxlsxwriter built and working the next sections will show you how to create some more in-depth examples.