如何从iOS应用程序中打开Excel文件

时间:2018-03-13 00:39:37

标签: ios swift uidocumentinteraction uidocumentinteractioncontroller

我想从我的应用程序打开一个Excel文件,而无需连接互联网。这可能吗?如果是的话,我该怎么做呢?

我可以通过以下方式打开带有互联网连接的文件:

@objc static func openExcel() {
    let originalString = "http://s000.tinyupload.com/download.php?file_id=23290165129849240725&t=2329016512984924072514118"
    let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let encodedURLString = "ms-excel:ofe|u|" + encodedString! + "|n|TestDoc.xlsx|a|App"

    if let url = NSURL(string: encodedURLString), UIApplication.shared.canOpenURL(url as URL) {
        UIApplication.shared.openURL(url as 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)
    }
}

1 个答案:

答案 0 :(得分:-1)

let originalString = "http://s000.tinyupload.com/download.php?file_id=23290165129849240725&t=2329016512984924072514118"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let encodedURLString = "ms-excel:ofe|u|" + encodedString! + "|n|TestDoc.xlsx|a|App"

if let url = NSURL(string: encodedURLString), UIApplication.shared.canOpenURL(url as URL) {
    UIApplication.shared.openURL(url as 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)
}