2010-10-06 81 views
0

嗨,我正在編寫一個應用程序,我需要將.xls文件轉換爲xml以將其提供給Web服務。我所有的谷歌搜索都涉及到手動轉換。我一直無法找到任何有關如何通過iOS自動執行此操作的教程,有誰知道一個很好的教程,甚至是一本包含此教程的書嗎?將.xls文件轉換爲xml以用於iOS的webservice

謝謝,威廉

回答

0

你有兩個選擇,將其轉換爲.xlsx,並且大部分工作都是爲您完成的,或者如果您計劃在另一端簡單地「拆包」.xls,則可以執行<data type="application/vnd.ms-excel">(Base64encoded file contents)</data>或類似的操作。

+0

感謝您的答覆,是新的網絡服務和傳輸數據,並從Web服務。我怎麼base64encode通過文件? – williamb 2010-10-06 15:55:11

+0

很明顯取決於您的平臺,但以下是.NET中的方法:http://msdn.microsoft.com/en-us/library/dhx0d524.aspx – Flynn1179 2010-10-06 18:05:13

0

經過多次搜索,我設法找到了一個解決方案,將.xls文件發送爲建議的Flynn1179之類的.xls文件。我感到困惑,認爲我必須將該文件作爲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 

} 

感謝大家的幫助