2012-12-06 72 views
0

我有一個包含一些數據(具有多列和多行)的excel文件。我想將這些數據導入基於核心數據的數據庫。我努力導入數據,因爲單元格中的文本包含換行符。如何在單元格文本中導入包含換行符的excel文件

我已經試過如下:

1)導出Excel爲製表符分隔文本文件

2)寫的iOS導入路由使用以下讀取製表符分隔文本文件代碼:

NSCharacterSet *tabCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"\t"]; 

NSArray *rows = [dataString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 
NSArray *columns = [row componentsSeparatedByCharactersInSet:tabCharacterSet]; 

問題:我有1000 rows與每個15 columns。解析例程返回多於1000個rows並且少於15個columns。解析程序沒有正確處理單元格中的換行符。

我得到同樣的結果,如果我用

[NSCharacterSet characterSetWithCharactersInString:@"\r\n"];

[NSCharacterSet characterSetWithCharactersInString:@"\r"];

,而不是

[NSCharacterSet newlineCharacterSet]

但Ç如果我使用

[NSCharacterSet characterSetWithCharactersInString:@"\n"];

我怎樣才能正確地分析Excel的數據ompletely失敗?

也許我可以使用正則表達式來獲取線組件?任何想法/指針?

UPDATE(樣品XLSX和導出文件):爲我工作

Sample XLSX File Sample TAB Delimited Text File

+0

你能告訴什麼樣的TSV文件看起來像換行** **裏面一排的例子嗎? – borrrden

+0

值中有換行符的CSV/TSV值將用引號引起來。包含字段分隔符的值也將用引號引起來。您無法通過簡單的分隔符分割文本來正確解析CSV/TSV文件。你需要處理引用的值。正確解析CSV/TSV文件涉及大多數人的想法。 – rmaddy

+0

已編輯,包括示例XLSX和導出文件屏幕截圖。 – Mustafa

回答

0

的解決方案包括使用NSScanner類。

- (NSArray *)parseCSVFileString { 
    NSMutableArray *rows = [NSMutableArray array]; 

    // Get newline character set 
    NSMutableCharacterSet *newlineCharacterSet = (id)[NSMutableCharacterSet whitespaceAndNewlineCharacterSet]; 
    [newlineCharacterSet formIntersectionWithCharacterSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]]; 

    // Characters that are important to the parser 
    NSMutableCharacterSet *importantCharactersSet = (id)[NSMutableCharacterSet characterSetWithCharactersInString:@",\""]; 
    [importantCharactersSet formUnionWithCharacterSet:newlineCharacterSet]; 

    // Create scanner, and scan string 
    NSScanner *scanner = [NSScanner scannerWithString:self]; 
    [scanner setCharactersToBeSkipped:nil]; 

    while (![scanner isAtEnd]) { 

     @autoreleasepool { 
      BOOL insideQuotes = NO; 
      BOOL finishedRow = NO; 
      NSMutableArray *columns = [NSMutableArray arrayWithCapacity:10]; 
      NSMutableString *currentColumn = [NSMutableString string]; 
      while (!finishedRow) { 
       NSString *tempString; 

       if ([scanner scanUpToCharactersFromSet:importantCharactersSet intoString:&tempString]) { 
        [currentColumn appendString:tempString]; 
       } 

       if ([scanner isAtEnd]) { 

        if (![currentColumn isEqualToString:@""]) [columns addObject:currentColumn]; 

        finishedRow = YES; 

       } else if ([scanner scanCharactersFromSet:newlineCharacterSet intoString:&tempString]) { 

        if (insideQuotes) { 
         // Add line break to column text 
         [currentColumn appendString:tempString]; 

        } else { 
         // End of row 
         if (![currentColumn isEqualToString:@""]) [columns addObject:currentColumn]; 

         finishedRow = YES; 
        } 

       } else if ([scanner scanString:@"\"" intoString:NULL]) { 

        if (insideQuotes && [scanner scanString:@"\"" intoString:NULL]) { 
         // Replace double quotes with a single quote in the column string. 
         [currentColumn appendString:@"\""]; 

        } else { 
         // Start or end of a quoted string. 
         insideQuotes = !insideQuotes; 
        } 

       } else if ([scanner scanString:@"," intoString:NULL]) { 

        if (insideQuotes) { 
         [currentColumn appendString:@","]; 

        } else { 
         // This is a column separating comma 
         [columns addObject:currentColumn]; 
         currentColumn = [NSMutableString string]; 
         [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL]; 
        } 
       } 
      } 

      if ([columns count] > 0) [rows addObject:columns]; 
     } 
    } 

    return rows; 
} 

參考:http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data

相關問題