首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在项目中使用excel资源打开excel应用程序?(无法打开文件错误)

如何在项目中使用excel资源打开excel应用程序?(无法打开文件错误)
EN

Stack Overflow用户
提问于 2018-05-18 19:23:15
回答 1查看 2.4K关注 0票数 1

因此,我创建了一个贷款计算器,它生成了一组信息和一个摊销表。我想添加一个功能来创建一个在Excel上的摊销表。

因此,我的计划是有一个按钮来打开excel,使用我在我的项目中的资源excel文件。

我用这个荚写到excel文件

https://cocoapods.org/pods/XlsxReaderWriter

我遵循了这个指南,一切似乎都很顺利。

writer

我认为问题在于打开excel文件的代码行。

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 3
        {
            if indexPath.row == 0
            {
                let path: String = Bundle.main.path(forResource: "AmortizationTable", ofType: "xlsx")!
                let urlString: String = "ofe|u|" + path
                let encodedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                let encodedURLString = "ms-excel:" + encodedString! //+ "|n|AmortizationTable.xlsx|a|App"

                if let url = URL(string: encodedURLString), UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.openURL(url)
                } else if let itunesUrl = NSURL(string: "https://itunes.apple.com/us/app/microsoft-excel/id586683407?mt=8&uo=4"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
                    UIApplication.shared.openURL(itunesUrl as URL)
                }
            }
        }
    }

在用户计算时,我还将调用此函数。它很好,但我想我应该把它加进去。

代码语言:javascript
复制
func createExcel() {
        // Set the path to the path of wherever you put your Excel file.
        // The path in this demo code is the path to the demo.xlsx file.
        let path: String = Bundle.main.path(forResource: "AmortizationTable", ofType: "xlsx")!

        // Open the spreadsheet, get the first sheet, first worksheet, and first cell A1.
        // This is solely demo code to show basics; your actual code would do much more here.
        let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
        let sheet: BRASheet = spreadsheet.workbook.sheets[0] as! BRASheet
        let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
        let cell: BRACell = worksheet.cell(forCellReference: "A1")
        cell.value = "It's working!"
        spreadsheet.save()
        // Print some info to show the code works.
        print(cell.value) // print "It's working"
        print(cell.stringValue()) // print "Alpha"
    } 

当我单击该按钮时,它将传输到excel应用程序,并得到以下错误:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-20 02:34:29

很抱歉,今年大家都迟到了回复。这是我今天的全部功能。

代码语言:javascript
复制
func createExcel(amortizationArray: [amortizationObject], loanName: String) -> Date
    {
    // Post notification that createExcel has started
    NotificationCenter.default.post(name: .isStart, object: nil)

    // Set the path to the path of wherever you put your Excel file.
    let path: String = Bundle.main.path(forResource: "AmortizationTable", ofType: "xlsx")!

    // Open the spreadsheet, get the first sheet, first worksheet, and first cell A1.
    let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
    let worksheetToCopy: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
    let worksheetName = "Amortization Table"
    let worksheet: BRAWorksheet = spreadsheet.workbook.createWorksheetNamed(worksheetName, byCopying: worksheetToCopy)

    var cell: BRACell
    for i in 0..<amortizationArray.count
    {
        let amortObj = amortizationArray[i]

        var color: UIColor
        if  amortObj.shade
        {
            color = UIColor(rgb: 0xf9f9f9)
        }
        else
        {
            color = UIColor(rgb: 0xf2f2f2)
        }
        cell = worksheet.cell(forCellReference: ("A" + String(describing: (i+2))), shouldCreate: true)

        cell.setCellFillWithForegroundColor(color, backgroundColor: .white, andPatternType: kBRACellFillPatternTypeDarkTrellis)

        let period = amortObj.amortiaztionValuesArray[0]
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short
        dateFormatter.locale = Locale.current

        if let date = Calendar.current.date(byAdding: .month, value: Int(period), to: Date()) {
            let value = dateFormatter.string(from: date)
            cell.setStringValue(value)
        }
        let cellColumns = ["B", "C", "D","E","F","G"]

        for j in 0..<cellColumns.count
        {
            let column = cellColumns[j]
            cell = worksheet.cell(forCellReference: (column + String(describing: (i+2))), shouldCreate: true)
            let value = currencyInput(index: j+1, amortObj: amortObj)
            cell.setStringValue(value)
            cell.setCellFillWithForegroundColor(color, backgroundColor: .white, andPatternType: kBRACellFillPatternTypeDarkTrellis)
        }
    }

    var paths: Array = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as Array

    let fullPath: String = paths[0] + "/" + loanName + ".xlsx"
    spreadsheet.workbook.removeWorksheetNamed("Sheet1")
    spreadsheet.save(as: fullPath)

    let date = Date()
    UserDefaults.standard.set(fullPath, forKey: String(describing: date))

    return date
}

我认为最重要的信息就是这里的代码片段。

代码语言:javascript
复制
// Set the path to the path of wherever you put your Excel file.
    let path: String = Bundle.main.path(forResource: "AmortizationTable", ofType: "xlsx")!

    // Open the spreadsheet, get the first sheet, first worksheet, and first cell A1.
    let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
    let worksheetToCopy: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
    let worksheetName = "Amortization Table"
    let worksheet: BRAWorksheet = spreadsheet.workbook.createWorksheetNamed(worksheetName, byCopying: worksheetToCopy)

保存后一定要删除空工作表。

代码语言:javascript
复制
var paths: Array = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as Array

let fullPath: String = paths[0] + "/" + loanName + ".xlsx"
spreadsheet.workbook.removeWorksheetNamed("Sheet1")
spreadsheet.save(as: fullPath)

如果这对任何人有帮助就告诉我。我会回答任何问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50418010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档