我如何精确导出从swift写的iOS的csv文件? [英] How do i exactly export a csv file from iOS written in swift?

查看:834
本文介绍了我如何精确导出从swift写的iOS的csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导出一个cvs文件。

I am trying to export an cvs file.

使用以下代码,我设法获取文件

With the following code i manage to get the file

let fileName = "sample.csv"//"sample.txt"        
    @IBAction func createFile(sender: AnyObject) {
            let path = tmpDir.stringByAppendingPathComponent(fileName)
            let contentsOfFile = "No,President Name,Wikipedia URL,Took office,Left office,Party,Home State\n1,George Washington,http://en.wikipedia.org/wiki/George_Washington,30/04/1789,4/03/1797,Independent,Virginia\n2,John Adams,http://en.wikipedia.org/wiki/John_Adams,4/03/1797,4/03/1801,Federalist,Massachusetts\n3,Thomas Jefferson,http://en.wikipedia.org/wiki/Thomas_Jefferson,4/03/1801,4/03/1809,Democratic-Republican,Virginia\n4,James Madison,http://en.wikipedia.org/wiki/James_Madison,4/03/1809,4/03/1817,Democratic-Republican,Virginia\n5,James Monroe,http://en.wikipedia.org/wiki/James_Monroe,4/03/1817,4/03/1825,Democratic-Republican,Virginia\n6,John Quincy Adams,http://en.wikipedia.org/wiki/John_Quincy_Adams,4/03/1825,4/03/1829,Democratic-Republican/National Republican,Massachusetts"
                //"Sample Text repacement for future cvs data"content to save

            // Write File

            do {
                try contentsOfFile.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
                print("File sample.txt created at tmp directory")
            } catch {

                print("Failed to create file")
                print("\(error)")
            }

        }

// Share button
    @IBAction func shareDoc(sender: AnyObject) {
        print("test share file")

         docController.UTI = "public.comma-separated-values-text"
            docController.delegate = self//delegate
            docController.name = "Export Data"
            docController.presentOptionsMenuFromBarButtonItem(sender as! UIBarButtonItem, animated: true)

        //}
    }

当我点击模拟器中的共享文件按钮时,我看到以下内容:

When i click the share file button in the simulator i see the following:

,快速查看

所以我做的下一件事是测试我的iphone 5,我试图发送电子邮件sample.csv,但我只收到邮件正文,而不是csv文件?

So the next thing i did was testing with my iphone 5 and i tried to email sample.csv but i am only getting the message body and not the csv file???


  1. 如何实际发送.csv文件?

  2. 哪些导出的可能性有? / li>
  1. how can i actually email the .csv file?
  2. which export possibilities are there?


推荐答案

为了发送.csv文件,您可以执行以下操作:

In order to email the .csv file, you can do the following:


  1. 将此导入添加到课堂顶部。它允许您使用MFMailComposeViewController,这是一种发送电子邮件的方式。

  1. Add this import to the top of the class. It allows you to use MFMailComposeViewController, which is a way to send emails.

import MessageUI


  • 生成您的数据,我所做的一个示例是:

  • Generate your data, a sample I have done is:

    // Creating a string.
    var mailString = NSMutableString()
    mailString.appendString("Column A, Column B\n")
    mailString.appendString("Row 1 Column A, Row 1 Column B\n")
    mailString.appendString("Row 2 Column A, Row 2 Column B\n")
    
    // Converting it to NSData.
    let data = mailString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    
    // Unwrapping the optional.
    if let content = data {           
        print("NSData: \(content)")
    }
    

    / li>

  • 生成MFMailComposeViewController

  • Generate the MFMailComposeViewController

    // Generating the email controller.
        func configuredMailComposeViewController() -> MFMailComposeViewController {
            let emailController = MFMailComposeViewController()
            emailController.mailComposeDelegate = self
            emailController.setSubject("CSV File")
            emailController.setMessageBody("", isHTML: false)
    
            // Attaching the .CSV file to the email.
            emailController.addAttachmentData(data!, mimeType: "text/csv", fileName: "Sample.csv")
    
            return emailController
        }
    
    // If the view controller can send the email.
    // This will show an email-style popup that allows you to enter
    // Who to send the email to, the subject, the cc's and the message.
    // As the .CSV is already attached, you can simply add an email 
    // and press send.
        let emailViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(emailViewController, animated: true, completion: nil)
        }
    


  • 在你的情况下,你已经创建了这个文件,通过将CSV附加到邮件的行直接更改为:

    In your case, as you have already created the file, you can simply attach that directly by changing the line where the CSV is attached to the mail for:

        emailController.addAttachmentData(NSData(contentsOfFile: "YourFile")!, mimeType: "text/csv", fileName: "Sample.csv")
    

    答案基于:将csv附加到电子邮件xcode
    在Swift中创建CSV文件并写入文件

    这篇关于我如何精确导出从swift写的iOS的csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆