1. 产品
  2.   电子表格
  3.   Swift
  4.   XlsxReaderWriterSwift
 
  

免费 Swift 库,用于读取和写入 Excel XLSX 文件

开源且强大的 Swift 5 库,帮助开发者免费创建和读取 XLSX 文件、复制、删除工作表、应用样式和格式等。

什么是 XlsxReaderWriterSwift?

厌倦了在 Swift 项目中与复杂的电子表格格式搏斗吗?想象一下,您的应用需要生成详细报告、从模板导入用户数据,甚至构建动态财务模型——全部直接在 iOS 生态系统中完成。手动解析 CSV 文件受限,并且依赖外部服务处理 XLSX 文件效率低下。这正是 XlsxReaderWriterSwift——一个强大且原生的开源库——发挥作用的地方。它为 Swift 开发者提供直观且强大的工具包,实现无缝读取和写入 Microsoft Excel 现代 .xlsx 格式,直接在您的 iOS、macOS 和 visionOS 应用中使用。

XlsxReaderWriterSwift 是一个强大的 Swift 5 库,使软件开发者能够创建和读取 XLSX 文件——现代 Excel 电子表格的标准。该库提供了多项重要功能,用于操作 Microsoft Excel XLSX 文件,例如读取 XLSX 文件、创建新 XLSX 文件、对单元格应用样式和格式、在单元格中添加和管理图像、生成报告、嵌入电子表格等。它基于 C 库 libxlsxwriter 构建,为 Apple 生态系统带来了完整的功能集。这意味着您可以直接在 iOS 或 macOS 应用中生成功能完整、100% 兼容的 Excel 文件。

Previous Next

XlsxReaderWriterSwift 入门指南

推荐的安装 XlsxReaderWriterSwift 方式是使用 CocoaPods。请使用以下命令进行顺利安装。

通过 CocoaPods 安装 XlsxReaderWriterSwift

 pod "XlsxReaderWriterSwift"

pod install

通过 GitHub 安装 XlsxReaderWriterSwift

git clone https://github.com/mehulparmar4ever/XlsxReaderWriterSwift.git

您可以直接从 GitHub 下载。

通过 Swift 库创建并写入新的 XLSX 文件

从头创建新文件并填充数据也非常简单。开源的 XlsxReaderWriterSwift 库为在 Swift 应用程序中创建和管理新的 Excel XLSX 电子表格提供了完整的支持。软件开发人员可以创建新工作表、重命名现有工作表、复制工作表数据、将文档保存到特定位置等。下面的示例展示了软件开发人员如何使用 Swift 库创建新的 Excel XLSX 工作表。

如何通过 Swift 库创建新的 Excel XLSX 工作表?

import XlsxReaderWriterSwift

// 1. Create a new, empty document
let document = BRAOfficeDocumentPackage()

// 2. Get the first worksheet and give it a name
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
    return
}
worksheet.name = "User Data"

// 3. Write different types of data to cells
worksheet.cell(forCellReference: "A1", shouldCreate: true).stringValue = "Name"
worksheet.cell(forCellReference: "B1", shouldCreate: true).stringValue = "Score"
worksheet.cell(forCellReference: "A2", shouldCreate: true).stringValue = "Alice"
worksheet.cell(forCellReference: "B2", shouldCreate: true).integerValue = 95
worksheet.cell(forCellReference: "A3", shouldCreate: true).stringValue = "Bob"
worksheet.cell(forCellReference: "B3", shouldCreate: true).integerValue = 87

// 4. Define a file path to save the document
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsDirectory.appendingPathComponent("NewReport.xlsx")

// 5. Save the document to the specified path
do {
    let data = document.save()
    try data?.write(to: fileURL)
    print("Spreadsheet saved successfully at: \(fileURL.path)")
} catch {
    print("Failed to save spreadsheet: \(error)")
}

使用 Swift 应用高级单元格样式

电子表格不仅仅是数据,呈现同样重要。开源的 XlsxReaderWriterSwift API 提供了对单元格样式的广泛控制。它超越了简单的数据输入。您可以应用各种格式化选项,包括单元格对齐和数字格式、创建新单元格、创建新样式、访问现有样式、对一系列单元格应用边框等。这里有一个简单示例,展示软件开发人员如何使用 Swift 命令对 Excel 单元格应用高级样式。

如何使用 Swift 库为 Excel 单元格应用高级样式?

// ... (after creating a worksheet and cell)

// Get or create a cell
let headerCell = worksheet.cell(forCellReference: "A1", shouldCreate: true)
headerCell.stringValue = "Sales Report"

// Access the cell's style
let style = headerCell.cellStyle()

// Configure the style
style.font.bold = true
style.font.size = 18
style.fill.foregroundColor = .blue // Background color
style.fill.patternType = .solid
style.alignment.horizontal = .center

// Apply a border to a range of cells
let borderStyle = BRABorderStyle()
borderStyle.lineStyle = .medium
borderStyle.color = .darkGray

let dataRange = worksheet.cells(inRows: 1...3, columns: 1...2)
for cell in dataRange {
    (cell as? BRACell)?.cellStyle().bottomBorder = borderStyle
}

通过 Swift 库读取 XLSX 文件

开源的 XlsxReaderWriterSwift 库允许软件开发人员使用 Swift API 对 Excel 电子表格中的数据进行操作。该库可以执行各种数据操作,如排序、过滤以及在 Excel 工作表中计算数值。这使其成为数据分析和报告等任务的理想工具。此外,软件开发人员还能使用 Swift API 轻松地从 Excel 工作表的特定单元格、行或列中提取数据。这在处理大型数据集且仅需要特定信息时非常有用。

如何使用免费 Swift API 读取 Excel XLSX 文件中的数据?

import XlsxReaderWriterSwift
// 1. Locate the .xlsx file in your app's bundle
guard let filePath = Bundle.main.path(forResource: "SampleData", ofType: "xlsx") else {
    print("File not found!")
    return
}

// 2. Create a BRAOfficeDocumentPackage object from the file
let document = BRAOfficeDocumentPackage.open(filePath)

// 3. Get the first worksheet
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
    print("No worksheets found!")
    return
}

// 4. Read data from specific cells
if let cellA1 = worksheet.cell(forCellReference: "A1") {
    let stringValue = cellA1.stringValue() // Gets the value as a String
    print("A1: \(stringValue)")
}

if let cellB2 = worksheet.cell(forCellReference: "B2") {
    let numberValue = cellB2.floatValue() // Gets the value as a Float
    print("B2: \(numberValue)")
}

// 5. Iterate over a range of cells
for row in 1...5 {
    for column in 1...3 {
        let cellReference = "\(UnicodeScalar(64 + column)!)\(row)" // Creates refs like A1, B1, etc.
        if let cell = worksheet.cell(forCellReference: cellReference),
           let value = cell.stringValue() {
            print("\(cellReference): \(value)")
        }
    }
}

 中国人