Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:33:00+00:00 2026-06-17T14:33:00+00:00

I have an excel file with some data (with multiple columns and multiple rows).

  • 0

I have an excel file with some data (with multiple columns and multiple rows). I want to import this data into my core-data based database. I’m struggling to import the data, because the text in the cells contain line breaks.

I’ve tried the following:

1.) Export excel to Tab-delimited text file

2.) Write an import route in iOS that reads the tab-delimited text file using the following code:

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

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

Problem: I have 1000 rows with 15 columns each. The parsing routine returns more than 1000 rows and less than 15 columns. The line breaks with-in the cell are not being handled properly by the parsing routine.

I get the same results if I use

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

or

[NSCharacterSet characterSetWithCharactersInString:@"\r"];

instead of

[NSCharacterSet newlineCharacterSet]

but it completely fails if I use

[NSCharacterSet characterSetWithCharactersInString:@"\n"];

How can I parse the excel-data properly?

Perhaps I can use Regex expressions to get line components? Any idea/pointers?

UPDATE (Sample XLSX and Export File):

Sample XLSX File
Sample TAB Delimited Text File

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:33:02+00:00Added an answer on June 17, 2026 at 2:33 pm

    The solution that worked for me involves using NSScanner class.

    - (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;
    }
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions about importing data from Excel/CSV File into SQL Server. Let
I have an Excel file with data in it. I want to write some
I have an excel file with some data, I need to import all the
I want to have my excel file filled with some data which I get
I have some C#/Linq code used to merge data from excel file into db,
I have an excel file with 10,000 rows in column A some values are
I have an Excel file that I need to import into a (new) Oracle
I have some data that I'd like to save in an excel file. How
I have a Excel file with multiple tabs. I have a worksheet with some
I have a few hundred Excel files, where each file has some data on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.