Swift 写入Excel表格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qin_shi/article/details/82772668

需求

项目中需要把数据写入Excel表格中。

引入流程

  1. 下载Excel的开发包 http://www.libxl.com/download.html
  2. 把下载的LibXL.framework文件放入xcode工程中
    在这里插入图片描述
  3. 修改xcode的配置信息
    在这里插入图片描述
    BitCode为No,Other Linker Flages 追加-lstdc++
  4. 桥接文件引入头文件
#import <LibXL/libxl.h>

初步实现

 let book = xlCreateXMLBookCA()
        let sheet = xlBookAddSheetA(book, "sheet1", nil)
        let boldFormat = FormatHandle.init(bitPattern: 0)
        //生成第一行第一列的数据,命名为“id”
        xlSheetWriteStrA(sheet, 1, 0, "id", boldFormat)
		。。。。。。。
		//获取保存路径
		let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("Letters.xlsx")
        
        print("excel::: \(jsonPath)")
        //保存表格
        xlBookSaveA(book, jsonPath.cString(using: .utf8))
        xlBookReleaseA(book)

这个开发库是收费的,生成的项目里会第一行会追加一行数据
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qin_shi/article/details/82772668