Converting .xls file to xml for webservice with iOS
Hey, am writing an application and I need to convert a .xls file to xml to feed it into a web service. All my google searches involve converting it by hand. I've not been able to find any tutorials on how to do this automatically through iOS,开发者_StackOverflow社区 does anyone know of a good tutorial or even a book that includes this?
Thanks, William
You've got two choices; convert it to .xlsx and most of the work's done for you, or if you plan on simply 'unpacking' the .xls at the other end, you could just do <data type="application/vnd.ms-excel">(Base64encoded file contents)</data>
, or something similar.
After much searching I managed to find a solution where I send the .xls file as an .xls file like Flynn1179 suggested. I got confused into thinking that I had to send the file as xml as it was to a web service. Below is the code I used:
-(void)uploadFile:(NSString*)filename withExtension:(NSString*)extension{
NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *urlString = @"http://server/path/to/webservice/call";
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"%@.%@\"\r\n", filename, extension] dataUsingEncoding:NSUTF8StringEncoding]]; //"uploadedfile" should be substituted for whatever variable the backend script expects the file variable to be
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"returned string: %@", returnString); //Just for display purposes
}
Thanks for everyones help
精彩评论