ios - 解析 XLSX 期间找不到 Xcode 文件错误

标签 ios swift

我正在解析 XLXS 文件。当我执行代码时出现错误

"Fatal error: XLSX file corrupted or does not exist".

我仔细检查了项目中可用的 test.xlsx 文件。代码有什么问题。任何帮助将不胜感激

guard let file = XLSXFile(filepath: "./test.xlsx") else {
            fatalError("XLSX file corrupted or does not exist")
        }

        do{

            for path in try file.parseWorksheetPaths() {
                let ws = try file.parseWorksheet(at: path)
                for row in ws.data?.rows ?? [] {
                    for c in row.cells {
                        print(c)
                    }
                }
            }
        }catch{
            print("error", error)
        }

最佳答案

文件路径无法从包目录访问,需要传递正确的路径:

guard let filePath = Bundle.main.path(forResource: "test", ofType: "xlsx", inDirectory: nil) else {
    fatalError("XLSX file not exist")
}
guard  let file = XLSXFile(filepath: filePath) else {
    fatalError("XLSX file corrupted")
}

还有一件事需要检查,在将 XLSX 文件添加到项目时,它必须添加到目标并复制到捆绑目录:

enter image description here

关于ios - 解析 XLSX 期间找不到 Xcode 文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57110703/

相关文章:

ios - 核心文本 - 从 ctrunref 获取 nsstring

ios - Swift - UIImagePickerController - 如何使用它?

swift - 在执行代码之前等待用户关闭模态视图(Swift 2.0)

ios - 使用 Swift 3 解析 JSON 响应

ios - 转换后的 tableview 图像产生 "unexpectedly found nil while unwrapping optional"

ios - BLE 不是有效的外设

ios - 当其他元素太宽时,在水平 UIStackview 中看不到标签

iphone - 如何将自定义字体设置为 UILabel

iphone - UIManagedDocument 和核心数据

ios - 将底部单元格添加到动态表格 View