Parsing Excel Data in Apple Swift

后端 未结 5 786
说谎
说谎 2020-12-14 05:17

My current workflow involves using Applescript to essentially delimit Excel data and format it into plain text files. We\'re pushing towards an all Swift environment, but I

5条回答
  •  北海茫月
    2020-12-14 05:33

    There is no need to export Excel files to CSV for Swift as you can use an existing open-source library for parsing XLSX files. If you use CocoaPods or Swift Package Manager for integrating 3rd-party libraries, CoreXLSX supports those. After the library is integrated, you can use it like this:

    import CoreXLSX
    
    guard let file = XLSXFile(filepath: "./file.xlsx") else {
      fatalError("XLSX file corrupted or does not exist")
    }
    
    for path in try file.parseWorksheetPaths() {
      let ws = try file.parseWorksheet(at: path)
      for row in ws.sheetData.rows {
        for c in row.cells {
          print(c)
        }
      }
    }
    

    This will open file.xlsx and print all cells within that file. You can also filter cells by references and access only cell data that you need for your automation.

提交回复
热议问题