我创建了一个Xcode项目,该项目在项目文件夹中有一个电子表格,并使用xlsxreaderwriter连接到Excel工作表。
我的项目可以从Excel电子表格中读取并在程序运行时更新它。我的问题是该程序不保存与新数据连接的Excel电子表格。我的控制台显示它正在保存,但是当我打开我的实际电子表格文件时,它看起来就像我创建的原始版本。
下面是我创建的两个函数,用于设置单元格引用和保存我放在viewDidLoad中的数据。
private func getExcelCells() {
let path: String = Bundle.main.path(forResource: "DatabaseV1", ofType: "xlsx")!
//Open the spreadsheet, get get the first worksheet, and select cell B5
let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
let sheet: BRASheet = spreadsheet.workbook.sheets[0] as! BRASheet
let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
// Creates variables that are type CellModel
var cat1 = CellModel(categoryTitle: "Best DJ", firstCell: worksheet.cell(forCellReference: "B5"), secondCell: worksheet.cell(forCellReference: "B6"), thirdCell: worksheet.cell(forCellReference: "B7"), fourthCell: worksheet.cell(forCellReference: "B8"))
let cat2 = CellModel(categoryTitle: "Best SGA", firstCell: worksheet.cell(forCellReference: "E5"), secondCell: worksheet.cell(forCellReference: "E6"), thirdCell: worksheet.cell(forCellReference: "E7"), fourthCell: worksheet.cell(forCellReference: "E8"))
let cat3 = CellModel(categoryTitle: "Best Greek Organization", firstCell: worksheet.cell(forCellReference: "H5"), secondCell: worksheet.cell(forCellReference: "H6"), thirdCell: worksheet.cell(forCellReference: "H7"), fourthCell: worksheet.cell(forCellReference: "H8"))
// Add the variables created above to the array excelCells
excelCells += [cat1, cat2, cat3]
cat1.cellOne.setStringValue(nominees.optionA)
let data = worksheet.cell(forCellReference: "A5").stringValue()
print(cat1.cellOne.stringValue())
print(data)
}
private func saveExcelFile() {
let path: String = Bundle.main.path(forResource: "DatabaseV1", ofType: "xlsx")!
let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
let sheet: BRASheet = spreadsheet.workbook.sheets[0] as! BRASheet
let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
worksheet.save()
spreadsheet.save()
}
以下是我回到我的控制台的结果,但是当我从计算机文件中打开电子表格时,它又没有更新。
some("2014-15")
Optional("Nominee 1 ")
Best DJ : Best DJ
Best SGA : Best SGA
Best Greek Organization : Best Greek Organization
TVC Data has nominees 2014-15
2018-05-16 12:51:48.375702-0400 xlsxreaderwriter[18952:668623] Saving target : worksheets/sheet1.xml
2018-05-16 12:51:48.432046-0400 xlsxreaderwriter[18952:668623] Saving target : [Content_Types].xml
2018-05-16 12:51:48.437702-0400 xlsxreaderwriter[18952:668623] Saving target : docProps/app.xml
2018-05-16 12:51:48.440162-0400 xlsxreaderwriter[18952:668623] Saving target : docProps/core.xml
2018-05-16 12:51:48.442317-0400 xlsxreaderwriter[18952:668623] Saving target : styles.xml
2018-05-16 12:51:48.471786-0400 xlsxreaderwriter[18952:668623] Saving target : theme/theme1.xml
2018-05-16 12:51:48.474184-0400 xlsxreaderwriter[18952:668623] Saving target : worksheets/sheet1.xml
2018-05-16 12:51:48.519212-0400 xlsxreaderwriter[18952:668623] Saving target : sharedStrings.xml
2018-05-16 12:51:48.526377-0400 xlsxreaderwriter[18952:668623] Saving target : _rels/workbook.xml.rels
2018-05-16 12:51:48.530246-0400 xlsxreaderwriter[18952:668623] Saving target : xl/workbook.xml
2018-05-16 12:51:48.540307-0400 xlsxreaderwriter[18952:668623] Saving target : _rels/.rels
2014-15 2015-16 2016-17 2017-18