Git Product home page Git Product logo

xlsxwriter.swift's Introduction

xlsxwriter.swift

xlsxwriter.swift is a powerful Swift wrapper around Libxlsxwriter, enabling the creation of Excel XLSX files with ease. This library allows developers to generate sophisticated Excel files with various formatting options, text, numbers, formulas, hyperlinks, and much more.

Getting Started

Before using xlsxwriter.swift, you need to install the Libxlsxwriter C library on your system. Please refer to the Getting Started guide for instructions.

Alternatively, you can use the SPM branch, which compiles the library along with the Swift Package Manager.

Swift Package Manager

The Swift Package Manager is a convenient tool for managing Swift code distribution. To use xlsxwriter.swift with SPM:

  1. Add the following line to your Package.swift file in the dependencies array:
dependencies: [
    .package(url: "https://github.com/damuellen/xlsxwriter.swift", branch: "main")
]

or, if you want to use the SPM branch:

dependencies: [
    .package(url: "https://github.com/damuellen/xlsxwriter.swift", branch: "SPM")
]
  1. Build your project using Swift Package Manager.

For *nix systems:

$ swift build

For Windows:

$ swift build -Xswiftc -LC:/vcpkg/installed/x64-windows/lib/ -Xcc -IC:/vcpkg/installed/x64-windows/include/

Please note: You need to install the libxlsxwriter C library first; it is not included in the build.

Usage

With xlsxwriter.swift, creating Excel XLSX files is straightforward and efficient. The example below demonstrates how to create a new workbook, add a worksheet, and write some data with formatting:

import xlsxwriter

// Create a new workbook and add a worksheet.
var wb = Workbook(name: "demo.xlsx")
defer { wb.close() }
let ws = wb.addWorksheet()

// Add a format.
let format = wb.addFormat()

// Set the bold property for the format
format.bold()

// Write some simple text.
ws.write(.string("Hello"), [0, 0])

// Text with formatting.
ws.write(.string("World"), [0, 1], format: format)

About Libxlsxwriter

Libxlsxwriter is a powerful C library that allows developers to write text, numbers, formulas, hyperlinks, and more to multiple worksheets in an Excel 2007+ XLSX file. Some of its key features include compatibility with Excel XLSX files, full Excel formatting support, merged cells, defined names, charts, data validation, and more.

The library is released under the FreeBSD license and works on various platforms, including Linux, FreeBSD, OpenBSD, macOS, iOS, and Windows. It has minimal dependencies and offers high performance even with large files.

The source code for Libxlsxwriter is available on GitHub, making it a flexible and reliable choice for creating Excel XLSX files programmatically.

With xlsxwriter.swift, developers can harness the power of Libxlsxwriter in a Swift environment, enabling seamless integration of Excel file generation into their Swift applications.

xlsxwriter.swift's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xlsxwriter.swift's Issues

Cocoapod support !

Please can u add support for cocoapod?, this is an awesome project but is hard to devs like me that's don't use SPM and instead use cocoapod. Thanks for your hard work !

Compile error related to xlsxwriter.h

Hi I've created a new project and import the library with SPM. But It is not compiling due to find xlsxwriter.h

#ifdef _WIN32
#include "C:/vcpkg/installed/x64-windows/include/xlsxwriter.h"
#elif __APPLE__
#include "/opt/homebrew/include/xlsxwriter.h" //   <- Here is indicating '/opt/homebrew/include/xlsxwriter.h' file not found
#elif __linux__
#include "/usr/local/include/xlsxwriter.h"
#endif

For sure I don't have home-brew installed on my machine. But I guess if I had installed it would work on my machine only not on the real hardware.
Is there any way to accomplish this requirement?

"System error = Operation not permitted" on workbook_close()

open /var/mobile/Containers/Data/Application/45AA94F9-7192-4B74-8025-277A3038AB8A/Documents [ERROR] workbook_close(): Error creating '/var/mobile/Containers/Data/Application/45AA94F9-7192-4B74-8025-277A3038AB8A/DocumentsDoc.xlsx'. System error = Operation not permitted lxw_error(rawValue: 2)

have this error each time when I try workbook_close(workbook)

Basically, I need to share the composed document through AirDrop. How to do it?

Merge SPM branch in to main

Hi! Great work on this lib. I was wondering if there is a reason why the SPM branch shouldn't be merged in to main?
Reason I'm asking is these branches appear to have diverged more than just the way the xlxswriter library is included and it would be great to have a single source of truth.
I'd be happy to work on a PR to help things along.

Support For Writing Rich Strings

I've added support for writing rich strings to the wrapper, but my custom fork has very extensive changes in style and formatting, so a direct PR isn't possible. I'm putting my code here so that you can adapt it to your style and add it to the wrapper.

Fragment Struct

This simple object holds information about one fragment of a rich string. In your module, ExcelFormat is named just Format, I think:

///
///  Represents one fragment of a rich string: a substring and, optionally, a format to apply to that substring.
///
///  Warning: Libxlsxwriter will throw an error if two consecutive fragments of a rich string have the same format object.
///
struct ExcelRichStringFragment
{
    var format: ExcelFormat?
    var string: String
    
    init(_ string: String, format: ExcelFormat? = nil)
    {
        self.string = string
        self.format = format
    }
}

Worksheet Addition

This method accepts an array of ExcelRichStringFragment and writes the rich string to a particular cell:

    ///
    ///  Writes a rich string to a given cell by assembling the rich string fragments. The final format parameter should contain
    ///  attributes that apply to the cell, but not the string (border type/color, background color, etc.) Including such attributes 
    ///  in the formats applied to each string fragment will fail to apply them to the cell.
    ///
    ///  WARNING: It is an error for two consecutive fragments to share the same format object, or for a fragment to have an empty string.
    ///
    @discardableResult func writeRichString(fragments: [ExcelRichStringFragment], at cell: ExcelCell, format: ExcelFormat? = nil) throws -> ExcelWorksheet
    {
        guard !fragments.isEmpty else {
            return self
        }

        let mainFormat = format?.lxw_format
        
        var rawTuples: [lxw_rich_string_tuple] = []
        for fragment: ExcelRichStringFragment in fragments
        {
            let f = fragment.format?.lxw_format
            let s = UnsafeMutablePointer<Int8>(mutating: (fragment.string as NSString).utf8String)
            
            let t = lxw_rich_string_tuple(format: f, string: s)
            rawTuples.append(t)
        }
        
        rawTuples.withUnsafeMutableBufferPointer { p in
            
            guard let arrBaseAddress = p.baseAddress else { return }

            var pointersToEachArrayElement: [UnsafeMutablePointer<lxw_rich_string_tuple>?] = Array(arrBaseAddress ..< arrBaseAddress.advanced(by: p.count))
            pointersToEachArrayElement.append(nil)
            
            _ = pointersToEachArrayElement.withUnsafeMutableBufferPointer { q in
                worksheet_write_rich_string(lxw_worksheet, cell.row, cell.col, q.baseAddress, mainFormat)
            }
        }
        
        return self
    }

Example Usage

let f1: ExcelFormat = book.addFormat()
f1.fontColor(.red)
        
let f2: ExcelFormat = book.addFormat()
f2.fontColor(.blue)

let frag1 = ExcelRichStringFragment("This is", format: f1)
let frag2 = ExcelRichStringFragment(" a test.", format: f2)
        
try sheet.writeRichString(fragments: [frag1, frag2], at: "D10")

Merging cells

Hello. Does this library support merging cells? If so, how ?

Selector Cell and read existing files

Good afternoon.
I have some doubts about this library.

  1. How can I read an existing xlsx file and modify it?
  2. How can I create cells with selectors and set the allowed values in an array?

cocoapods version

It would be cool if you could also publish the package via cocoapods.

Incorrect URL Writing Causes Failure

This line is incorrect:

error = url.path.withCString { s in worksheet_write_url(lxw_worksheet, r, c, s, f) }

Using .path() will return an empty string for URLs such as: mailto:[email protected]. This empty string will cause libxlsxwriter to fail with a very cryptic error: "ignoring NULL function parameter".

mailto: URLs are listed as explicitly supported by libxlsxwriter, so this wrapper should handle them correctly. To do that, use .absoluteString() on the URL instead of .path(). This works for all well-formed URIs that libxlswriter supports, as shown:
Screen Shot 2022-08-26 at 00 49 14

The behavior for other URIs, such as local filesystem URLs, is undefined.

SPM Issues

xlsxwriter.h:1:10: 'xlsxwriter.h' file not found with include; use "quotes" instead

Cell_Range: Could not build Objective-C module 'Cxlsxwriter'

Failing to compile Xcode 15.0

error: cannot assign value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer') to type 'UnsafePointer' (aka 'UnsafePointer')

Screenshot 2023-10-09 at 4 29 33 PM

Screenshot 2023-10-09 at 4 29 46 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.