X $ C $Ç - 创建CSV / S preadsheet文件 [英] Xcode - Create csv/spreadsheet file

查看:163
本文介绍了X $ C $Ç - 创建CSV / S preadsheet文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数组。第一阵列包含字符串,相同的第二,但是第三个含有的NSNumber 的。不管怎样,回到主题...我怎么能创建一个CSV(这是一个Excel /号)文件中像A S preadsheet包含此数据。这可能吗?

这是我的三个数组:

locationArray - 包含许多字符串
dateArray - 包含许多字符串
milesArray - 包含许多的

的NSNumber

不过,每个阵列包含(在表视图为例)对象相同数量的,所以你可以在的NSDictionary 拼凑起来的数据。它就像一个链条。

现在,它看起来像这样:

 的NSDictionary * firstFlight = [NSDictionary的dictionaryWithObjectsAndKeys:[locationArray objectAtIndex:0],@FlightName,[dateArray objectAtIndex:0],@FlightDate,[milesArray objectAtIndex:0] @FlightMiles零]

和该字典将包含一个飞行(本测试应用程序的功能是记录飞行)。所以,如果我列举这个使用循环,并取代 objectAtIndex:与循环数,我能得到的许多词典航班。

现在,你明白我的数据结构是如何工作的。我怎样才能创建一个Excel /数字/ S preadsheet CSV文件看起来像这样,例如(只是从抓屏它可能是什么样子):

(MILES数据只是由)


解决方案

 
NSArray的* firstArray,* secondArray,* thirdArray;*的NSMutableString CSV = [的NSMutableString stringWithString:@的名称,日期,万里行];NSUInteger数= [firstArray计数]
//提供所有阵列具有相同的长度的
对于(NSUInteger我= 0; I<计数;我++){
    [CSV appendFormat:@\\ n \\%@ \\%@ \\%d个\\,
        [firstArray objectAtIndex:我]
        [secondArray objectAtIndex:我]
        [thirdArray objectAtIndex:我] i​​ntegerValue]
     ];
    //而不是integerValue可使用的intValue或者其他,这取决于阵列是如何创建的
}* NSString的yourFileName = @的文件名;
NSError *错误;
BOOL解析度= [CSV将writeToFile:yourFileName原子:是编码:NSUTF8StringEncoding错误:错误]如果(!RES){
    的NSLog(@错误%@同时写入文件%@,[错误localizedDescription],yourFileName);
}

I have three arrays. The first array contains strings, same as the second, however the third one contains NSNumber's. Anyways, back on topic... how can I create a csv (which is an excel/numbers) file like a spreadsheet to contain this data. Is it possible?

These are my three arrays:

locationArray - contains many strings dateArray - contains many strings milesArray - contains many NSNumber's

But each array contains the same amount of objects so (for example in a table view) you can piece the data together in an NSDictionary. It is like a chain.

So then it would look like this:

NSDictionary *firstFlight = [NSDictionary dictionaryWithObjectsAndKeys:[locationArray objectAtIndex: 0], @"FlightName", [dateArray objectAtIndex: 0], @"FlightDate", [milesArray objectAtIndex: 0], @"FlightMiles", nil];

And that dictionary would contain a flight (the functionality of this test app is to log flights). So if I enumerated this using a for loop and replaced objectAtIndex: with the looped number I could get many dictionaries of the flights.

Now that you understand how my data structure works. How can I create an excel/numbers/spreadsheet CSV file to look like this for example (just a screencapture from what it could look like):

(MILES DATA IS JUST MADE UP)

解决方案


NSArray *firstArray, *secondArray, *thirdArray;

NSMutableString *csv = [NSMutableString stringWithString:@"Name,Date,Miles"];

NSUInteger count = [firstArray count];
// provided all arrays are of the same length
for (NSUInteger i=0; i<count; i++ ) {
    [csv appendFormat:@"\n\"%@\",%@,\"%d\"",
        [firstArray objectAtIndex:i],
        [secondArray objectAtIndex:i],
        [[thirdArray objectAtIndex:i] integerValue]
     ];
    // instead of integerValue may be used intValue or other, it depends how array was created
}

NSString *yourFileName = @"your filename";
NSError *error;
BOOL res = [csv writeToFile:yourFileName atomically:YES encoding:NSUTF8StringEncoding error:&error];

if (!res) {
    NSLog(@"Error %@ while writing to file %@", [error localizedDescription], yourFileName );
}

这篇关于X $ C $Ç - 创建CSV / S preadsheet文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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