none
how to write data in Multiple sheets

    Pregunta

  • 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

    sábado, 28 de abril de 2012 7:33

Respuestas

  • 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

    • Marcado como respuesta dev00 miércoles, 02 de mayo de 2012 15:32
    • Desmarcado como respuesta dev00 jueves, 10 de mayo de 2012 15:19
    • Marcado como respuesta dev00 jueves, 10 de mayo de 2012 15:19
    lunes, 30 de abril de 2012 3:32
    Moderador

Todas las respuestas

  • 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

    • Marcado como respuesta dev00 miércoles, 02 de mayo de 2012 15:32
    • Desmarcado como respuesta dev00 jueves, 10 de mayo de 2012 15:19
    • Marcado como respuesta dev00 jueves, 10 de mayo de 2012 15:19
    lunes, 30 de abril de 2012 3:32
    Moderador
  • thanks Tom its works 


    Tabbasi

    miércoles, 02 de mayo de 2012 15:32