Welcome to OpenXML Developer Sign in | Join | Help

My experience of generating spreadsheet on the fly with SDK 2.0

Last post 09-12-2008, 5:45 AM by maxi0361. 1 replies.
Sort Posts: Previous Next
  •  09-12-2008, 5:08 AM 3688

    My experience of generating spreadsheet on the fly with SDK 2.0

     

    Things that you must do is as follow:

    SpreadsheetDocument l_package = SpreadsheetDocument.Create(@"file path", SpreadsheetDocumentType.Workbook);

     

    //add a workbook.xml with runtime generated relationship id in /_rels/.rels
    var workbookPart = l_package.AddWorkbookPart();
    //save the workbook object return by the method to the workbook part

    // the return is actually a WorkBook object
    GenerateWorkbookPart().Save(workbookPart);

    //add a worksheet with relationship id rId1 in /xl/_rels/workbook.xml.rels
    //since the part is add to workbookpart1 which is /xl/workbook.xml
    var worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
    GenerateWorksheetPart().Save(worksheetPart1);

     

    The GenerateXXXXXXX() can be generate by DocumentReflector tool.

     

    There are two things I want to point out.

    1. it seems to me that [Content_Types].xml will be generated when you call SpreadsheetDocument.Dispose().

    so, remember to call it or use the 'using' keyword.

    2. the example in their manual "How to: Insert a New Worksheet into a Spreadsheet Document by Using the Open XML API " just don't work.

    the code as follow has a problem:

    uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }
    I am using VS2008, the complier will tell you that, no Count() method is being implemented.

    and when I want to extend the class by myself using OpenXmlElement.MoveNext() method to count the number of elements.

    the MoveNext() method seems to return true no matter what.

     

    I will investigate the MoveNext() method to find out what is going on.

    If anyone do know how to count the Sheet elements in Sheets, please tell me.

     

    Thank you.

  •  09-12-2008, 5:45 AM 3689 in reply to 3688

    Re: My experience of generating spreadsheet on the fly with SDK 2.0

    I've found myself a workaround

    foreach( <T> item in items.Elements<T>)

    {

       count += 1;

    }

    since OpenXmlElement.Elements<T>() return IEnumerable object.

    I now have my own Count() method.

View as RSS news feed in XML