How to convert Excel file in csv and read data from iphone - ios

I want to make an application that read excel file from url and convert data in to csv file and then i can read the csv file and display the data. how can i do this. please help me.

If you have a .xls file, you can use the open source DHlibxls library to read the file into your app. This is an ObjectiveC framework that wraps a C-based library. The library is quite mature.

How to convert Excel file in csv
Click here
and read data from iphone
//******** Get Data fron CSV file  **********//
NSString *path = [[NSBundle mainBundle] pathForResource:#"NameYourCsvFile" ofType:#"csv"];
NSError* error;
dataStr=[NSString stringWithContentsOfFile:path encoding:NSStringEncodingConversionAllowLossy error:&error ];

Related

How to get file in a library iOS

I am trying to construct a library in IOS.
In my library ,I use a method -(void)loadNewData to load some data EX:test.csv.
But when I export my library and let the other app use my library to excute the loadNewData method.
I found it can not be load correctly. It seems that because there is not such a file test.csv in the app.
How should I revise my code? Or where should I notice when I export the library?
Thank you for watch my question.
And sorry for my poor English.
Here is the a part of code of the loadNewData:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *Rawsurveys=[resourcePath stringByAppendingPathComponent:#"test.csv"];
NSString *RawsurveyResults;
if([[NSFileManager defaultManager]fileExistsAtPath:Rawsurveys])
{
NSLog(#"find file");
NSFileHandle *RawfileHandle=[NSFileHandle fileHandleForReadingAtPath:Rawsurveys];
RawsurveyResults=[[NSString alloc]initWithData:[RawfileHandle availableData] encoding:NSUTF8StringEncoding];
[RawfileHandle closeFile];
}
You could probably create a bundle within your library and add you test.csv there.
Then you can access your test.csv from your own bundle.
Example of how I use images from my bundle. [UIImage imageNamed:#"myBundle.bundle/myImage"]. You could use the somewhat same for your file

ObjectiveC - Reading ePub File Gotten From Dropbox

So I'm pretty lost with this one and really new to epub files. I've done a bit of searching but can't seem to put everything together in my head.
My app uses DropBox's Chooser API to get a file from a user's DropBox folder. In this case, I want to open up a .epub file. So when the user chooses a file, the DropBox API gives me back an NSURL object to that file. For example:
https://dl.dropboxusercontent.com/1/view/e8bmxpkree6nc67/The%20Art%20of%20War.epub
And now, I've tried a couple different tools to try to read this file. Originally, I tried using KFEpubKit. But when I called:
epubURL; // The url from DropBox (shown above)
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:epubURL andDestinationFolder:documentsURL];
self.epubController.delegate = self;
[self.epubController openAsynchronous:YES]
I would get back an error that the file couldn't be unzipped. The error reads as:
Epub Error: Error Domain=KFEpubKitErrorDomain Code=1 "Could not extract epub file." UserInfo=0x170275400 {NSLocalizedDescription=Could not extract epub file.}
I looked into the code and narrowed down the problem a little bit. The KFEpubKit uses the SSZipArchive utility to unzip files. And from this point on, I'm a bit stuck. The [SSZipArchive unzipFileAtPath: toDestination:] call seems to be failing when used with the epubURL.path. I'm not sure if this has something to do with the fact that my file is a .epub extension and not a .zip extension. Or maybe there's some stuff to do after getting the URL from DropBox and before giving it to the KFEpubKit tool?
In the end, I'm expecting to have to display the text of the book with a UIWebView. But I'm just not sure how to handle this .epub file. What should I do with the file from Dropbox? Any help is much appreciated.
A quick glance indicates that that SSZipArchive wants a local file URL, not a remote HTTP URL. Try downloading the file first (NSData with contents of URL, then write to some temp file) then create a file URL that points to the temp file, and send that into the KFEpubController:
// Download the file from dropbox
epubURL; // The url from DropBox (shown above)
NSData * epubData = [NSData dataWithContentsOfURL:epubURL];
NSString * tempPath = [NSTemporaryDirectory() stringByAddingPathComponent:#"temp.epub"];
[epubData writeToFile:tempPath atomically:YES];
NSURL *tempURL = [NSURL URLWithString:tempPath];
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:tempURL andDestinationFolder:documentsURL];
// etc.
(Coding from memory.) All normal caveats apply here-- you'll want to do proper progress/error handling on the download, get rid of the temp file, etc, etc.

Got NSCocoaErrorDomain code=261 when reading xml file

I am trying to read a COLLADA file (xml formatted, utf-8 encoding) with following code:
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"astroBoy_walk_Max" ofType:#"dae"];
NSError *error;
NSString *xmlContent = [NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:&error];
The returned xmlContent is nil and error #"NSCocoaErrorDomain" - code: 261.
Then I tried with other encoding like NSASCIIStringEncoding and the xmlContent is not nil but contained string "bplist00Ô\x01\x02\x03\x04\x05\x06\a\bZzippedData_\x10\x1creaderMinimumRequiredVersionVzippedWversionO\x12..." which obviously not original file content.
And later I renamed the file's extension to dat, and read with almost the same code:
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"astroBoy_walk_Max" ofType:#"dat"];
NSError *error;
NSString *xmlContent = [NSString stringWithContentsOfFile:xmlPath encoding:NSASCIIStringEncoding error:&error];
Everything looks good, the xmlContent contains correct file content string...
What is the problem when I reading the raw .dae file?
The collada files are compressed on compilation time
When you include a scene file in Collada or Alembic format in your
Xcode project, Xcode automatically converts the file to SceneKit’s
compressed scene format for use in the built app. The compressed file
retains its original .dae or .abc extension.
from Apple Documentation
If you open the app bundle and read the collada file as text you'll realize it's different from the one you added to your project

Objective-Zip: Store content when unzip file (iOS)

I just want to create a file with the data of unzipped file in iOS.
I am using the Objective-Zip library, and I can zip files rightly.
Now, I am trying to extract the files of this zip file.
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];
//Checking files inside zip
NSArray *infos= [unzipFile listFileInZipInfos];
for (FileInZipInfo *info in infos)
NSLog(#"Test 1: - %# %# %d (%d)", info.name, info.date, info.size, info.level);
[unzipFile goToFirstFileInZip];
ZipReadStream *read= [unzipFile readCurrentFileInZipWithPassword:password];
How I can pass the ZipReadStream to a NSData object? Then I would convert NSData to the right file, is not?
Thanks in advance.
Sorry, it seems to be duplicate: unzip NSData using objective zip
Looking at the headers for the ZipReadStream class, it looks like there is a method readDataOfLength. It appears that you call that method repeatedly until you have all the data from the file. I would expect a method to find out the length of the unzipped file, but did not see such a method from a quick glance.
As a side-note, I've never heard of the Objective-Zip library until a few minutes ago, and I found the 'readDataOfLength:' method in less than a minute. Perhaps you should spend some time looking through the library's header and sample code?

Reading data from .xls/.csv files into iOS

I'm new to iOS and am trying to read the contents of a spreadsheet into an iOS array.
The spreadsheet that I'm using is a simple 3 x 2 array of numbers for the first column and text for the second. I've tried reading it in with & without column headers, in .xls, .xlsx, . cdv, .txt (unicode and delimited) but without success. The file is called "funds", and the code I'm using is:
NSData *databuffer;
NSFileHandle * file;
NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)[0];
NSString *fileDir = [docDir stringByAppendingPathComponent:#"funds.xls"];
file = [NSFileHandle fileHandleForReadingAtPath:fileDir];
if (file == nil) NSLog (#"Failed to open file");
[file seekTOOffset: 10];
databuffer = [file readDataOfLength: 5];
[file closeFile];
It finds the directory of the file but gives up at [NSFileHandle fileHandleForReadingAtPath:fileDir].
What should I be doing? I need help with confirming what file type I should use as the source, and how to read it into an array.
When this has been solved, I'm likely to need to read in large amount of data, several columns, 4k rows, a mixture of text & non-integer numbers.
Is there a format or method I should be using when the volume of data is getting to that size?
Be sure to export the data from Excel in CSV format.
It's easy to use NSFileManager to access the file:
NSString *pathName = ...; /// fill in the file's pathname
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:pathName]) {
// read the file contents, see below
}
For small files, you could just say:
NSString *lines = [NSString stringWithContentsOfFile:filePath];
and then proceed to split the string into lines, and process each line.
For large files, better have a look at some more sophisticated techniques, like in Objective-C: Reading a file line by line.
As for parsing the CSV lines, use the NSString method componentsSeparatedByString to tokenize each line, something like:
NSArray *fields = [line componentsSeparatedByString:#","];
Actually, that's also what you would use to split the file contents you read into individual lines. Just pay attention to the line feeds (CR LF or LF). You will find something in how to split a string with newlines.
There is a very popular CocoaPod / Github project that both will write and parse CSV Files. https://github.com/davedelong/CHCSVParser
You can find here: Where can I find a CSV to NSArray parser for Objective-C? quite similar problem, btw. converting CSV to JSON should help you parsing data, since JSON could be easily converted to the NSDictionary or NSArray.
I use Numbers create csv and I have problem when I add "," to table.
I use some trick to fix it.
Sorry I try to add sample code here but it too long.
https://github.com/LovePick/CSVParser-Swift

Resources