有什么方法可以将tablesorter表导出为Excel?

时间:2019-04-01 10:52:17

标签: tablesorter

我正在使用tablesorter库来显示表。 tablesorter中有什么功能可以将表格导出到excel?

1 个答案:

答案 0 :(得分:0)

//导出excel和电子邮件

enter code here
@IBAction func exportAction(_ sender: UIButton) {
    // 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.
    path = Bundle.main.path(forResource: "demo", ofType: "xlsx")!

    // Open the spreadsheet, get the first sheet, first worksheet, and first cell A1.
    // This is solely demoed code to show basics; your actual code would do much more here.
    let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
    let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet

    for row in 0..<self.data.count {
        let producerName = self.data[row][0]
        let clientName = self.data[row][1]
        let revenue = self.data[row][2]
        let date = self.data[row][3]
        let aCell = "A" + "\(row+2)"
        let bCell = "B" + "\(row+2)"
        let cCell = "C" + "\(row+2)"
        let dCell = "D" + "\(row+2)"

        worksheet.cell(forCellReference: aCell, shouldCreate: true)?.setStringValue(producerName)
        worksheet.cell(forCellReference: bCell, shouldCreate: true)?.setStringValue(clientName)
        worksheet.cell(forCellReference: cCell, shouldCreate: true)?.setStringValue(revenue)
        worksheet.cell(forCellReference: dCell, shouldCreate: true)?.setStringValue(date)
    }

    // save as demoAs.xlsx excel file
    let paths: Array = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) as Array
    let appendingStr = "/Report " + self.getTodayDateForExport() + ".xlsx"
    let fullPath: String = (paths[0]).appendingFormat(appendingStr)

    spreadsheet.save(as: fullPath)
    path = fullPath
    // Print some info to show the code works.

    print("")
    let fileURL = NSURL(fileURLWithPath: path!)
    let activityVC = UIActivityViewController(activityItems: [fileURL as Any], applicationActivities: nil)
    activityVC.popoverPresentationController!.sourceView = self.view
    activityVC.completionWithItemsHandler = { activity, success, items, error in
        if success {
            self.view.makeToast("reported!")
        } else {
            self.view.makeToast("cancelled!")
        }
    }
    self.present(activityVC, animated: true, completion: nil)
}