wordpress hit counter
Read WorkShhet From Workbook - SpreadsheetML - Formats - OpenXML Developer

Read WorkShhet From Workbook

Formats

Discussions about working with different Open XML Formats

Read WorkShhet From Workbook

  • rated by 0 users
  • This post has 1 Reply |
  • 1 Follower
  • Hi, I'm exploring the open XML format and i'm having some trouble to achieve something that i think must be really easy. Here's the case:

    1 - Have one excel document, where the user fills in some fields (only the first sheet is manipulated - is it possible to get SheetByName?)

    2 - A converter (exe), reads the excel file, and extracts the xml with all the data that the user filled in (the first worksheet) and saves it.

    3 - The converter after saving the xml, applyies a transformation (xsl) and originates a new message (xml format) for integration (calling webservice for ex).

    // Access the main Workbook part, which contains all references.
    WorkbookPart wbPart     = xlPack.WorkbookPart;

    // Get the first worksheet.
    WorksheetPart wsPart    = wbPart.WorksheetParts.First();


    XmlDocument doc = new XmlDocument();

    //This Actually gets the sheet information,however fails if document has more or less sheet - want to get sheet by name
    //Seen some examples that navigate thg
    Stream data = xlPack.Parts.ElementAt(2).OpenXmlPart.Parts.ElementAt(4).OpenXmlPart.GetStream();


    doc.Load(data);
    doc.Save("sheet.xml");


    Best regards and thank you for your help.
    Manuel

    Best regards, Manuel
  • Hi Manuel

    This code should make it work by retrieving the WorksheetPart by sheet name (works with one or many sheets):

    Sheet sheet = wbPart.Workbook.Descendants().Where(s => s.Name == "MainSheet").FirstOrDefault();

    WorksheetPart wsPart = (WorksheetPart)wbPart.GetPartById(sheet.Id);

    XmlDocument doc = new XmlDocument();

    Stream data = wsPart.GetStream();

    doc.Load(data);
    doc.Save("sheet.xml");
Page 1 of 1 (2 items)