所以我创建了一个贷款计算器,可以生成一堆信息和一个摊销表。我想添加一个功能来在Excel上创建一个分期付款表。
所以我的计划是使用我在项目中使用的资源excel文件打开excel的按钮。
我使用此窗格写入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应用程序,我收到此错误:
答案 0 :(得分:1)
很抱歉,大家迟到了一年之久。这是我今天的全部功能。
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)
让我知道这是否对任何人都有帮助。我会回答任何问题