美文网首页
swift---怎么在xcode内部读取Excel的文件,并把内

swift---怎么在xcode内部读取Excel的文件,并把内

作者: 这个姑凉儿 | 来源:发表于2017-08-11 14:02 被阅读0次
  • 先把excel的文件另存为csv格式的文件,然后在另存为txt格式的
  • 把txt格式的文件脱入Xcode

text文件的内容:
lottery_code,issue_no,single_letter,open_time
1000,1,30,2017-01-01 00:05:00.000
1000,2,30,2017-01-01 00:10:00.000
1000,3,30,2017-01-01 00:15:00.000
1000,4,30,2017-01-01 00:20:00.000
1000,5,30,2017-01-01 00:25:00.000
1000,6,30,2017-01-01 00:30:00.000
1000,7,30,2017-01-01 00:35:00.000

  • 本地数据库已经创建完成,直接进行txt文件的使用和存储
let dataBase = DataBaseManager()
        //读取txt文件
        let path = Bundle.main.path(forResource: "openTimes", ofType: "txt")
        let cfEnc = CFStringEncodings.GB_18030_2000
        let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
        do {
            let ret = try NSString.init(contentsOfFile: path!, encoding: enc)
            let datas = ret .components(separatedBy:"\r")//\r代表换行,通过换行分割成一条条的数据,放入数组
            for i in datas{
                let arrs =  i .components(separatedBy:",")
                openTimArr.append(arrs) 
            }
            openTimArr.removeFirst()
            for i in openTimArr{
                let openDict = ["lotterycode":i[0],"issue_no":i[1],"single_letter":i[2],"open_time":i[3]]
        //插入数据库
                dataBase.insert(tableName: "tc_lottery_open_time", dataDic: openDict as NSDictionary)
            }
        }catch let error as NSError {  
            QL1(error)
        }

(欢迎大家对不合适的地方进行指正,看完觉得有帮到你给点个赞👍吧)

相关文章

网友评论

      本文标题:swift---怎么在xcode内部读取Excel的文件,并把内

      本文链接:https://www.haomeiwen.com/subject/uqwlrxtx.html