Jiaolong's Blog Jiaolong's Blog
首页
分类
归档
Wiki
摘录
导航
关于

Jiaolong

Solo Developer
首页
分类
归档
Wiki
摘录
导航
关于
  • 基础

    • 关于SwiftUI
  • 进阶

    • 基本类型转换
    • 保存图片到本地
    • 调用自带分享功能
    • 生成导出CSV文件
    • 隐藏状态栏
    • 测试代码执行时间
    • 异步进程
    • 地区价格
    • app内获取版本号
  • Swift

    • 基本数据类型
    • Array
    • Date
  • custom

    • MyToggleStyle
  • __SwiftUI
  • 进阶
Jiaolong
2022-08-16

生成导出CSV文件

func exportForCSV() {
    do {
        let csv = try! CSVWriter(stream: .toMemory())
        // Write a row
        try! csv.write(row: ["row1", "row2", "row3"])
        // Write fields separately
        let folders = folderStore.getAll()
        for folder in folders {
            let groups = folder.getAllGroup()
            for group in groups {
                let items = group.getAllItems()
                for item in items {
                    csv.beginNewRow()
                    try! csv.write(field: "\(folder.name!)")
                    try! csv.write(field: "\(group.name!)")
                    try! csv.write(field: "\(item.name!)")
                    try! csv.write(field: "\(item.price!)")
                    try! csv.write(field: "\(item.detail!)")
                    try! csv.write(field: "\(item.rate)")
                    try! csv.write(field: "\(item.mainDate!.toString())")
                    try! csv.write(field: "\(item.url!)")
                    try! csv.write(field: "\(item.getTagNames())")
                    if item.goalName != "" {
                        try! csv.write(field: "\(item.goalName!)")
                        try! csv.write(field: "\(item.goalDate!.toString())")
                    }else{
                        try! csv.write(field: "")
                        try! csv.write(field: "")
                    }
                    try! csv.write(field: "\(item.memo!)")
                    
                    try! csv.write(field: "\(TimeLineHelper.getTimeData(data: item.timeLine!))")
                    try! csv.write(field: "\(item.isLike)")

                    try! csv.write(field: "\(item.isArchive)")
                    try! csv.write(field: "\(item.archiveMemo ?? "")")
                    try! csv.write(field: "\(item.isHide)")

                }
            }
        }

        
        csv.stream.close()
        
        // Get a String
        let csvData = csv.stream.property(forKey: .dataWrittenToMemoryStreamKey) as! Data
        let csvString = String(data: csvData, encoding:  .utf8)!
        
        
        print(csvString)
        let enc = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.GB_18030_2000.rawValue))

        let fileName = "MyThings.csv"
        let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)

        do {
            try csvString.write(to: path!, atomically: true, encoding:  String.Encoding(rawValue: enc))
        } catch {
            print("Failed to create file")
            print("\(error)")
        }
        print(path ?? "not found")

        var filesToShare = [Any]()
        filesToShare.append(path!)


        let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
        
        let viewController = Coordinator.topViewController()
        activityViewController.popoverPresentationController?.sourceView = viewController?.view
        viewController?.present(activityViewController, animated: true, completion: nil)
      
    } catch {
        print("Failed to create file")
        print("\(error)")
    }
    
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Last updated: 2022/12/08, 06:27:06

← 调用自带分享功能 隐藏状态栏→

Copyright © 2022-2023 | Jiaolong Wang
  • 跟随系统
  • 浅色模式
  • 深色模式