嘿,我正在编写一个应用程序,我需要将.xls文件转换为xml以将其提供给Web服务。我的所有谷歌搜索都涉及手动转换。我还没有找到任何关于如何通过iOS自动执行此操作的教程,有没有人知道一个好的教程,甚至包含这个的书?
谢谢,威廉
答案 0 :(得分:0)
你有两个选择;将它转换为.xlsx并完成大部分工作,或者如果您打算在另一端简单地“解压缩”.xls,您可以执行<data type="application/vnd.ms-excel">(Base64encoded file contents)</data>
或类似的操作。
答案 1 :(得分:0)
经过多次搜索,我设法找到了一个解决方案,我将.xls文件作为.xls文件发送,如Flynn1179建议的那样。我很困惑地认为我必须将文件作为xml发送到Web服务。以下是我使用的代码:
-(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
}
感谢大家的帮助