gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:34:40 28 4
gpt4 key购买 nike

所以我创建了一个贷款计算器,它可以生成一堆信息和一个摊销表。我想添加一项功能以在 Excel 上创建摊销表。

所以我的计划是使用项目中的资源 excel 文件创建一个打开 excel 的按钮。

我正在使用这个 pod 写入 excel 文件

https://cocoapods.org/pods/XlsxReaderWriter

我遵循了本指南,一切似乎都运行良好。

https://github.com/joelparkerhenderson/demo_swift_excel_xlsx_reader_writer

我认为问题出在打开 excel 文件的代码行上

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)
}
}
}
}

我也有这个函数在用户计算时调用。它工作正常,但我认为我应该添加它。

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 应用程序,我收到此错误:

Excel Error

最佳答案

抱歉大家一年来迟到的回复。这是我今天的全部功能。

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
}

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

// 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 paths: Array = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as Array

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

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

关于ios - Swift - 如何在项目中使用资源 excel 文件打开 excel 应用程序? (无法打开文件错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418010/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com