Create Excel Supported XML in iPhone using Xcode.

AID: 4575
  • Status: Published

2510 points

  • Bynish_tr
  • TypeTutorial
  • Posted on2011-02-23 at 01:18:06
This article guides you how to create Excel readable XML format using xcode. Here are some points you must take care -

First create a NSMutableString variable

NSMutableString *excel = [[NSMutableString alloc] init];
and  then build up your string with a header, then the spreadsheet header, then data and finally a closing section.

1. Header - Below lines should be added to the start of XML string -

      [excel appendString:@"<?xml version=\"1.0\"?>\n<?mso-application progid=\"Excel.Sheet\"?>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" "];
[excel appendString:@"xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"];
[excel appendString:@"<LastAuthor>Kuzora</LastAuthor>"];
[excel appendString:[NSString stringWithFormat:@"<Created>%@</Created>",[NSDate date]]];
[excel appendString:@"<Version>11.5606</Version>\n</DocumentProperties>\n<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n<WindowHeight>6690</WindowHeight>\n<WindowWidth>14355</WindowWidth>"];
[excel appendString:@"<WindowTopX>360</WindowTopX>\n<WindowTopY>75</WindowTopY>\n<ProtectStructure>False</ProtectStructure>\n<ProtectWindows>False</ProtectWindows>\n</ExcelWorkbook>\n<Styles>"];
[excel appendString:@"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n<Alignment ss:Vertical=\"Bottom\"/>\n<Borders/>\n<Font/>\n<Interior/>\n<NumberFormat/>\n<Protection/>\n</Style>"];
[excel appendString:@"<Style ss:ID=\"s21\">\n<NumberFormat ss:Format=\"Medium Date\"/>\n</Style><Style ss:ID=\"s22\">\n<NumberFormat ss:Format=\"Short Date\"/>\n</Style></Styles>"];
            
2. Add the following code to create Worksheets and Table headings -
[excel appendString:@"<Worksheet ss:Name=\"User\">"];
                  [excel appendString:@"<Table ss:ExpandedColumnCount=\"8\" ss:ExpandedRowCount=\"2\" x:FullColumns=\"1\" x:FullRows=\"1\">"];
                  [excel appendString:@"<Row>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">TraineeDetailID</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">Name</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">DOB</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">HeightFeet</Data></Cell>"];    
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">HeightInches</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">Weight</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">ImageFile</Data></Cell>"];
                  [excel appendString:@"<Cell><Data ss:Type=\"String\">BMI</Data></Cell></Row>"];
"ss:Name" will be the name of your worksheet
"ss:ExpandedColumnCount=\"8\" - number of columns of the table.
"ss:ExpandedRowCount=\"2\" - number of rows.

Above, I have then defined the column headers (just for an example). They are the ones beginning with <Cell>

After this add your dynamic data for each column using loop or whatever way you prefer.  Like

[excel appendString:@"<Row>"];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"Number\">1</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"String\">Aaa</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"String\">1984-07-25</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"Number\">5</Data></Cell>"]];    
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"Number\">6</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"Number\">65</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"String\">myimage.jpg</Data></Cell>"]];
[excel appendString:[NSString stringWithFormat:@"<Cell><Data ss:Type=\"Number\">12.5</Data></Cell></Row>"]];
                  
Then add the following code to close your worksheet.

[excel appendString:@"</Table><WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"];
[excel appendString:@"<Selected/><LeftColumnVisible>4</LeftColumnVisible><Panes><Pane>"];
[excel appendString:@"<Number>3</Number><ActiveRow>2</ActiveRow><ActiveCol>2</ActiveCol>"];
[excel appendString:@"</Pane></Panes><ProtectObjects>False</ProtectObjects>"];
[excel appendString:@"<ProtectScenarios>False</ProtectScenarios></WorksheetOptions></Worksheet>"];

you can follow the above step for each spreadsheet (if you want to create more than one)

And in the end add the closing tag to close your workbook.

[excel appendString:@"</Workbook>"];

Save this "as file" and you can mail to any mail address. Sample "XML" File attached.

Enjoy coding.

Thanks
Nishant Trivedi
  • EE.xml
    • 2 KB

    Sample Xml file.

Asked On
2011-02-23 at 01:18:06ID4575
Tags

iPhone

,

Excel

,

Xcode

,

XML

Topic

iPhone

Views
1922

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top iPhone Experts

  1. alanhardisty

    94,876

    Master

    0 points yesterday

    Profile
    Rank: Genius
  2. Programmer-x

    55,818

    Master

    0 points yesterday

    Profile
    Rank: Guru
  3. akahan

    52,884

    Master

    0 points yesterday

    Profile
    Rank: Sage
  4. darbid73

    42,536

    2,000 points yesterday

    Profile
    Rank: Wizard
  5. thinkpads_user

    27,468

    0 points yesterday

    Profile
    Rank: Genius
  6. Peerbolte

    18,336

    0 points yesterday

    Profile
  7. pgnatyuk

    18,332

    0 points yesterday

    Profile
    Rank: Genius
  8. maximus5328

    15,478

    0 points yesterday

    Profile
    Rank: Wizard
  9. jason1178

    15,032

    0 points yesterday

    Profile
    Rank: Genius
  10. eoinosullivan

    14,600

    0 points yesterday

    Profile
    Rank: Genius
  11. MBobrek

    11,232

    0 points yesterday

    Profile
    Rank: Guru
  12. aarontomosky

    10,498

    0 points yesterday

    Profile
    Rank: Genius
  13. senthil1_kumar

    10,336

    0 points yesterday

    Profile
    Rank: Master
  14. apache09

    9,900

    1,500 points yesterday

    Profile
    Rank: Genius
  15. Anuroopsundd

    9,300

    0 points yesterday

    Profile
    Rank: Sage
  16. Harvester_of_Sorrow

    8,800

    0 points yesterday

    Profile
  17. ishcabittle

    8,668

    0 points yesterday

    Profile
    Rank: Master
  18. demazter

    7,850

    0 points yesterday

    Profile
    Rank: Genius
  19. RobMobility

    6,728

    0 points yesterday

    Profile
    Rank: Genius
  20. Darr247

    6,600

    0 points yesterday

    Profile
    Rank: Genius
  21. minder49

    6,400

    0 points yesterday

    Profile
  22. jmcmunn

    6,014

    0 points yesterday

    Profile
    Rank: Guru
  23. MacGuy47

    6,000

    0 points yesterday

    Profile
  24. SpeedyApocalypse

    5,600

    0 points yesterday

    Profile
    Rank: Wizard
  25. robertcerny

    5,500

    0 points yesterday

    Profile
    Rank: Guru

Hall Of Fame