0

嘿,我正在编写一个应用程序,我需要将一个 .xls 文件转换为 xml 以将其提供给 Web 服务。我所有的谷歌搜索都涉及手动转换。我一直无法找到任何关于如何通过 iOS 自动执行此操作的教程,有没有人知道一个很好的教程甚至是包含这个的书?

谢谢,威廉

4

2 回答 2

0

你有两个选择;将其转换为 .xlsx 并为您完成大部分工作,或者如果您打算在另一端简单地“解包” .xls,您可以执行 .xls<data type="application/vnd.ms-excel">(Base64encoded file contents)</data>或类似的操作。

于 2010-10-06T12:43:32.957 回答
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

}

感谢大家的帮助

于 2010-10-08T09:32:05.207 回答