Error in converting .xls file into .csv file
I wanted to read excel file so i did following steps. First creating .csv file of the .xls file. But .csv file is opening in excel.
NSBundle *bundle = [NSBundle mainBundle];
NSString *xlsFilePath = [bundle pathForResource:@"sampleXLSFile" ofType:@"xls"];
NSString *csvFilePathLoc = [[xlsFilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"NewSample.csv"];
NSData *data = [NSData dataWithContentsOfFile:xlsFilePath];
NSLog(@"data value: %@", data);
NSFileManager *csvFile = [NSFileManager defaultManager];
if([csvFile createFileAtPath:csvFilePathLoc contents:data attributes:nil])
{
NSLog(@"File created");
}
else {
NSLog(@"File not created");
}
NSString *contentsOfCSVFile = [NSString stringWithContentsOfFile:csvFilePathLoc];
NSLog(@"contentsOfCSVFile value: %@",contentsOfCSVFile);
NewSample.csv file is created at application folder location. But when its not comma seperated it still opens in excel format.And when i NSLog the contentsOfCSVFile all data is encrypted. Can any body help? How to apply encoding a开发者_如何学JAVAnd decoding?
All you are doing is copying the exact content of the xls
file to a new file with .csv
on the end. Changing the file extension does not convert the file format automatically. You need to put a bit in between
NSData *data = [NSData dataWithContentsOfFile:xlsFilePath];
and
if([csvFile createFileAtPath:csvFilePathLoc contents:data attributes:nil])
that parses the data as an Excel file and turns it into a CSV file. This is probably a non trivial exercise. Best of luck.
精彩评论