none
how to write data in Multiple sheets

    问题

  • Dear Experts 

    I am new to open XML SDK development . I have to write the data in multiple sheets using open XML SDK . Here is the code which i am using to create the workbook and work sheet.

    Public bool Writer( String filename, String sheetName, int row , int colID )

    {

    using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(filename, true))
                {
                    WorkbookPart workbookPart = myDoc.WorkbookPart;
                    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();  

    }

    above code selects the first sheets however i have more than 13 worksheets in my workbook is there any method where i can find multiple sheets or sheets of array

    Best Regards


    Tabbasi

    2012年4月28日 7:33

答案

  • Hi dev00,

    Thanks for posting in the MSDN Forum.

    I created some snippets based on your description, I hope it can help you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = false;
                ofd.Filter = "Excel Document|*xlsx";
                ofd.ShowDialog();
                string path = ofd.FileName;
                using (SpreadsheetDocument ssd = SpreadsheetDocument.Open(path, true))
                {
                    WorkbookPart wbp = ssd.WorkbookPart;
                    WorksheetPart wsp = FindeWorkSheetPart("Sheet1", wbp, ssd);
                    //To-do something
                }
            }
    
            private static WorksheetPart FindeWorkSheetPart(string sheetname, 
                WorkbookPart wbp, SpreadsheetDocument ssd)
            {
                Sheet sheet = wbp.Workbook.Descendants<Sheet>().Where(S => S.Name == sheetname).FirstOrDefault();
                return ssd.GetPartById(sheet.Id) as WorksheetPart;
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 dev00 2012年5月2日 15:32
    • 取消答案标记 dev00 2012年5月10日 15:19
    • 已标记为答案 dev00 2012年5月10日 15:19
    2012年4月30日 3:32

全部回复

  • Hi dev00,

    Thanks for posting in the MSDN Forum.

    I created some snippets based on your description, I hope it can help you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = false;
                ofd.Filter = "Excel Document|*xlsx";
                ofd.ShowDialog();
                string path = ofd.FileName;
                using (SpreadsheetDocument ssd = SpreadsheetDocument.Open(path, true))
                {
                    WorkbookPart wbp = ssd.WorkbookPart;
                    WorksheetPart wsp = FindeWorkSheetPart("Sheet1", wbp, ssd);
                    //To-do something
                }
            }
    
            private static WorksheetPart FindeWorkSheetPart(string sheetname, 
                WorkbookPart wbp, SpreadsheetDocument ssd)
            {
                Sheet sheet = wbp.Workbook.Descendants<Sheet>().Where(S => S.Name == sheetname).FirstOrDefault();
                return ssd.GetPartById(sheet.Id) as WorksheetPart;
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 dev00 2012年5月2日 15:32
    • 取消答案标记 dev00 2012年5月10日 15:19
    • 已标记为答案 dev00 2012年5月10日 15:19
    2012年4月30日 3:32
  • thanks Tom its works 


    Tabbasi

    2012年5月2日 15:32