Writing data from NSArray to Excel file?
I want to write all the v开发者_JS百科alues present in an array to an excel sheet.Please suggest some way to do so.
Write the values out to a CSV file, import the CSV into Excel.
You have a few options:
- Do like @Jonah suggests (I voted for his answer because you had no details!)
- Study the XML interchange format from MS Office XML Formats and use NSXML... objects and methods
Depends on what it is you are trying to accomplish
Frank
I will explain writing values out to a CSV file more detail. For example you have an array of Numbers: NSArray *array = @[@1,@2, @3,@1.01];
In this case your csv file will looks like this (usual text file with a number on each string):
1
2
3
1.01
So first step is writting a CSV file:
// composing a string with numbers from array separated by "\n" (line-endian symbol)
NSString *string = [array componentsJoinedByString:@"\n"];
// getting data from resulting string
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// and writting data to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"yourFileName.csv"];
[data writeToFile:appFile atomically:YES];
and the last step is to import the CSV into Excel (just open "yourFileName.csv" with an Excel application and click OK).
But if you want to save array directly in XLS file (without importing into CSV format) you, probably, have to read first answer to this question: iPhone SDK - Export data to XLS (not via CSV)
精彩评论