简体   繁体   中英

How to use C library in Xcode Project?

The library is written in C https://github.com/jmcnamara/libxlsxwriter

The pod file that I am using is

  pod 'libxlsxwriter', '0.3.1'

I import the header into the Bridging Header

#import <libxlsxwriter/xlsxwriter.h>

I am getting this

'libxlsxwriter/xlsxwriter.h' file not found

It is actually quite simple, yet poorly documented (IMHO):

Install the library with CocoaPods :

  1. Add pod 'libxlsxwriter', '~> 0.9' to your pod file.
  2. Run pod install

You do not need a Swift-Bridging-Header, simply use the library like so:

import UIKit
import xlsxwriter

...

    override func viewDidLoad() {
        super.viewDidLoad()

        createExcelFile()
    }

    override func viewWillAppear(_ animated: Bool) {

    }

    /*@objc*/ func createExcelFile(){
        // Create a new workbook.
        let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
        let fileURL = documentDirectory.appendingPathComponent("demo.xlsx")

        //Ditch first 6 characters, because they are of the form file://
        let workbook = workbook_new((fileURL.absoluteString.dropFirst(6) as NSString).fileSystemRepresentation)

        // Add a worksheet with a user defined sheet name.
        let worksheet1 = workbook_add_worksheet(workbook, "Demo")

        // Add a worksheet with Excel's default sheet name: Sheet2.
        let worksheet2 = workbook_add_worksheet(workbook, nil)

        // Add some cell formats.
        let myformat1 = workbook_add_format(workbook)
        let myformat2 = workbook_add_format(workbook)

        // Set the bold property for the first format.
        format_set_bold(myformat1)

        // Set a number format for the second format.
        format_set_num_format(myformat2, "$#,##0.00")

        // Widen the first column to make the text clearer.
        worksheet_set_column(worksheet1, 0, 0, 20, nil)

        // Write some unformatted data.
        worksheet_write_string(worksheet1, 0, 0, "Peach", nil)
        worksheet_write_string(worksheet1, 1, 0, "Plum",  nil)

        // Write formatted data.
        worksheet_write_string(worksheet1, 2, 0, "Pear",  myformat1)

        // Formats can be reused.
        worksheet_write_string(worksheet1, 3, 0, "Persimmon",  myformat1)

        // Write some numbers.
        worksheet_write_number(worksheet1, 5, 0, 123,       nil)
        worksheet_write_number(worksheet1, 6, 0, 4567.555,  myformat2)

        // Write to the second worksheet.
        worksheet_write_string(worksheet2, 0, 0, "Some text", myformat1)

        // Close the workbook, save the file and free any memory
        workbook_close(workbook)
        loadingSpinner.stopAnimating()
        self.shareExcelFile(filepath: fileURL)
    }

    func shareExcelFile(filepath: URL){
        //Share the newly created file
        var filesToShare = [Any]()
        filesToShare.append(filepath)
        let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: [])
        activityViewController.popoverPresentationController?.sourceView = self.view
        activityViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.maxX, y: self.view.bounds.minY, width: 0, height: 0)
        activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)

        self.present(activityViewController, animated: true)

        activityViewController.completionWithItemsHandler = { activity, completed, items, error in
            if !completed {
                // handle task not completed
                return
            }
            self.dismiss(animated: false, completion: nil)
        }
    }
}

This should create an example .xlsx file, save it to the ~Documents directory and share it. I did not test the code, so expect needing to change some bits.

I will setup a demo repository, once I get the time to do it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM