인쇄용 버전       전송     
평가 및 의견을 보내려면 클릭하십시오.
MSDN
MSDN Library
기술 문서(Technical Articles)
Others
 Open XML 개체 모델을 사용한  Excel 2007 파일 ...
Open XML 개체 모델을 사용한  Excel 2007 파일 및 PowerPoint 2007 파일 작업 (1부)

개요 :Microsoft SDK for Open XML Formats 기술 프리뷰」는 Open XML 형식의 파일에 액세스하기 위한 라이브러리입니다.
이 문서에서는 Microsoft Office Excel 2007 파일과 Microsoft Office PowerPoint 2007 파일 액세스와 조작에 사용할 수 있는 Open XML 개체 모델 코드를 설명합니다.

Frank Rice, Microsoft Corporation

2007 년 8 월

적용 대상 : Microsoft Office Excel 2007, Microsoft Office PowerPoint 2007

목차

 「Open XML 개체 모델을 사용한 Excel 2007 파일 및 PowerPoint 2007 파일 작업 (2부)」를 참조해 주세요.

개요

2007 Microsoft Office system에서는 Open XML 형식으로 불리는 XML 기반의 새로운 파일 형식이 도입됩니다. Microsoft Office Word 2007, Microsoft Office Excel 2007 및 Microsoft Office PowerPoint 2007에서는 이 파일 형식이 기본값 파일 형식으로서 사용됩니다. Open XML 형식은 ZIP 와 XML 이라는 일반적인 기술에 기반하는 오픈 표준으로 사용하기 쉽습니다. Microsoft에서는 .NET Framework 3.0 기술의 일부로서 System.IO.Packaging 네임 스페이스로 이러한 파일에 액세스하기 위한 라이브러리를 「Microsoft SDK for Open XML Formats 기술 프리뷰」에 있습니다. Open XML 개체 모델은 System.IO.Packaging API 를 기반으로 생성되어, Open XML 문서를 조작하기 위한 엄밀하게 형식 지정된 파트 클래스를 갖추고 있습니다. 이 SDK 를 이용하여 Open XML 패키지의 조작이 간단해집니다. Open XML 개체 모델은 개발자가 Open XML 패키지에 대해서 실행하는 일반적인 작업의 대부분을 캡슐화되어 복잡한 조작을 몇 줄의 코드로 실행할 수 있습니다.

Note메모 :

Open XML 형식의 파일을 조작하는 그 외의 샘플 및 Open XML 개체 모델에 포함된 각 멤버의 레퍼런스는 「2007 Office system: Microsoft SDK for Open XML Formats」를 참조해 주세요.

Open Package Convention specification (영어)」에서는 XML 파일집합이 정의됩니다. 이 파일 집합에 의해 하나의 패키지에 보관된 모든 파트의 컨텐츠가 포함된 각 파트의 관계가 정의됩니다. 이 패키지에는 Open XML 형식을 지원하는 2007 Microsoft Office 프로그램 용무의 문서 파일을 구성하는 파트가 정리됩니다. 이 문서로 설명하는 Open XML 개체 모델을 사용하여 패키지를 생성하고, 그 패키지를 구성하는 파일을 조작할 수 있습니다. 이 문서에서는 Excel 2007에서 PowerPoint 2007 의 Open XML 패키지에 액세스하기 위한 코드를 설명합니다.


워크시트 주석을 사용자 이름순 삭제

다음은 보이는 코드에서는 특정 사용자 이름에 대응하는 문서 컨텐츠를 삭제합니다.

Visual Basic
Public Sub XLDeleteCommentsByUser(ByVal fileName As String, ByVal userName As String)
   Const commentsSchema As String = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
   Dim doc As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, True)
   '  Manage namespaces to perform XML XPath queries.
   Dim nt As NameTable = New NameTable
   Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(nt)
   nsManager.AddNamespace("sh", commentsSchema)
   For Each part As WorksheetPart In doc.WorkbookPart.WorksheetParts
      If (Not (part.WorksheetCommentsPart) Is Nothing) Then
         Dim commentDoc As XmlDocument = New XmlDocument
         commentDoc.Load(part.WorksheetCommentsPart.GetStream)
         Dim searchString As String = String.Format("//sh:authors/sh:author[text()='{0}']", userName)
         Dim node As XmlNode = commentDoc.SelectSingleNode(searchString, nsManager)
         If (Not (node) Is Nothing) Then
            Dim nodes As XmlNodeList = node.SelectNodes("preceding-sibling::sh:author", nsManager)
            Dim authorID As Integer = nodes.Count

            Dim commentListNode As XmlNode = commentDoc.SelectSingleNode("sh:comments/sh:commentList", nsManager)
            If (Not (commentListNode) Is Nothing) Then
               searchString = String.Format("./sh:comment[@authorId='{0}']", authorID)
               Dim comments As XmlNodeList = commentListNode.SelectNodes(searchString, nsManager)
               For Each commentNode As System.Xml.XmlNode In comments
                  commentListNode.RemoveChild(commentNode)
               Next
            End If
         End If
         '  Save the comment XML back to its part.
         commentDoc.Save(part.WorksheetCommentsPart.GetStream(FileMode.Create))
      End If
   Next
End Sub

C#
public static void XLDeleteCommentsByUser(string fileName, string userName)
{
   const string commentsSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

   using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fileName, true))
   {
      //  Manage namespaces to perform XML XPath queries.
      NameTable nt = new NameTable();
      XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
      nsManager.AddNamespace("sh", commentsSchema);

      foreach (WorksheetPart part in doc.WorkbookPart.WorksheetParts)
      {
         if (part.WorksheetCommentsPart != null)
         {
            XmlDocument commentDoc = new XmlDocument();
            commentDoc.Load(part.WorksheetCommentsPart.GetStream());
            string searchString = string.Format("//sh:authors/sh:author[text()='{0}']", userName);
            XmlNode node = commentDoc.SelectSingleNode(searchString, nsManager);
            if (node != null)
            {
                XmlNodeList nodes = node.SelectNodes("preceding-sibling::sh:author", nsManager);
                int authorID = nodes.Count;

                XmlNode commentListNode = commentDoc.SelectSingleNode("sh:comments/sh:commentList", nsManager);
                if (commentListNode != null)
                {
                   searchString = string.Format("./sh:comment[@authorId='{0}']", authorID);
                   XmlNodeList comments = commentListNode.SelectNodes(searchString, nsManager);
                   foreach (System.Xml.XmlNode commentNode in comments)
                   {
                      commentListNode.RemoveChild(commentNode);
                   }
                }
            }
            //  Save the comment XML back to its part.
       commentDoc.Save(part.WorksheetCommentsPart.GetStream(FileMode.Create));
         }
      }
   }
}

이 과정에서는 파일에의 전체 경로와 삭제하는 주석에 연결된 사용자 이름이라는 두 가지 매개 변수를 전달합니다. 다음은 SpreadsheetDocument 개체의 Open 메서드를 사용하여 입력파일을 Open XML 패키지로서 엽니다. 또한 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. sh 수식자를 사용하여, 기본값 SpreadsheetML 네임 스페이스 참조를 설정합니다.

다음은 패키지에 주석 파트가 존재하는지 체크합니다. 주석 파트의 내용이 메모리상주의 XML 문서에 로드됩니다. 다음은 XPath 식 sh:authors/sh:author 를 사용하여, 특정 사용자 이름에 대응하는 노드를 워크시트에서 검색합니다. 그 사용자 이름이 발견되면 그 사용자 이름의 서수 값을 검색합니다. 그리고, 선택된 노드의 인덱스를 검색합니다. 따라서,  그 노드 참조를 검색하여 존재하는 노드의 수를 조사합니다.

마지막에 업데이트 된 주석 마크 업 스트림을 원본 주석 파트에 저장합니다.


워크시트 삭제

다음 코드에서는 스프레드시트 문서에서 워크시트 삭제합니다.

Visual Basic
Public Function XLDeleteSheet(ByVal fileName As String, ByVal sheetToDelete As String) As Boolean
   Dim returnValue As Boolean = False
   '  Open the package with read/write access.
   Dim xlDoc As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, True)
   Dim doc As XmlDocument = New XmlDocument
   doc.Load(xlDoc.WorkbookPart.GetStream)
   Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
   nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI)
   Dim searchString As String = String.Format("//d:sheet[@name='{0}']", sheetToDelete)
   Dim node As XmlNode = doc.SelectSingleNode(searchString, nsManager)
   If (Not (node) Is Nothing) Then
      Dim relationAttribute As XmlAttribute = node.Attributes("r:id")
      If (Not (relationAttribute) Is Nothing) Then
         Dim relId As String = relationAttribute.Value
         xlDoc.WorkbookPart.DeletePart(relId)
         node.ParentNode.RemoveChild(node)
         doc.Save(xlDoc.WorkbookPart.GetStream(FileMode.Create))
         returnValue = True
      End If
   End If
   Return returnValue
End Function

C#
public static bool XLDeleteSheet(string fileName, string sheetToDelete)
{
   bool returnValue = false;
   using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, true))
   {
      XmlDocument doc = new XmlDocument();
      doc.Load(xlDoc.WorkbookPart.GetStream());

      XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
      nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI);

      string searchString = string.Format("//d:sheet[@name='{0}']", sheetToDelete);
      XmlNode node = doc.SelectSingleNode(searchString, nsManager);
      if (node != null)
      {
         XmlAttribute relationAttribute = node.Attributes["r:id"];
         if (relationAttribute != null)
         {
            string relId = relationAttribute.Value;
            xlDoc.WorkbookPart.DeletePart(relId);
            node.ParentNode.RemoveChild(node);
            doc.Save(xlDoc.WorkbookPart.GetStream(FileMode.Create));
            returnValue = true;
          }
      }
   }
   return returnValue;
}

이 과정에서는 통합 문서의 전체 경로와 삭제하는 워크시트 이름이라는 두가지 매개 변수를 전달합니다. 다음은 SpreadsheetDocument 개체의 Open 메서드를 사용하여, 입력파일을 Open XML 패키지로서 엽니다. XML DOM 문서에 통합 문서의 내용을 로드합니다. 또한 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. d 수식자를 사용하여  기본값 SpreadsheetML 네임 스페이스 참조를 설정합니다. 이 문서 안에서//d:sheet 노드의 name 특성을 사용하여, 지정된 워크시트를 검색합니다.

발견된 모든 노드 (존재하는 경우)에 대해, 관계Id 가 검색되어 Id 에 대응하는 워크시트가 삭제됩니다.

마지막에 업데이트 된 SpreadsheetML 마크 업이 원본 메인 통합 문서 파트에 저장 됩니다.


워크시트내의 숨겨진 행과 열 삭제

다음은 보이는 코드에 통합 문서와 워크시트 이름을 지정하면, 숨겨진 행의 번호 목록 또는 숨겨진 열의 번호 목록이 반환됩니다.


Visual Basic
Public Function XLDetectHiddenRowsOrCols(ByVal fileName As String, ByVal sheetName As String, ByVal detectRows As Boolean) As List(Of Integer)
   Const worksheetSchema As String = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
   '  Set up the return value list.
   Dim itemList As List(Of Integer) = New System.Collections.Generic.List(Of Integer)
   Dim xlDoc As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)
   '  Load the contents of the workbook.
   Dim doc As XmlDocument = New XmlDocument
   doc.Load(xlDoc.WorkbookPart.GetStream)
   '  Create a namespace manager, so you can search.
   '  Add a prefix for the default namespace.
   Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
   nsManager.AddNamespace("d", worksheetSchema)
   Dim searchString As String = String.Format("//d:sheet[@name='{0}']", sheetName)
   Dim sheetNode As XmlNode = doc.SelectSingleNode(searchString, nsManager)
   If (Not (sheetNode) Is Nothing) Then
      '  Get the relId attribute.
      Dim relationAttribute As XmlAttribute = sheetNode.Attributes("r:id")
      If (Not (relationAttribute) Is Nothing) Then
         Dim relId As String = relationAttribute.Value
         Dim sheetPart As WorksheetPart = CType(xlDoc.WorkbookPart.GetPartById(relId), WorksheetPart)
         '  First, get the relationship between the document and the sheet.
         '  Load the contents of the workbook.
         Dim sheetDoc As XmlDocument = New XmlDocument
         sheetDoc.Load(sheetPart.GetStream(FileMode.Open))
         If detectRows Then
             '  Retrieve the list of hidden rows.
             For Each node As System.Xml.XmlNode In sheetDoc.SelectNodes("//d:row[@hidden='1']", nsManager)
                  '  For each hidden row, add information to the output list.
                  Dim rowAttr As XmlAttribute = node.Attributes("r")
                  If (Not (rowAttr) Is Nothing) Then
                      itemList.Add(Convert.ToInt32(rowAttr.Value))
                  End If
          Next
      Else
          '  Retrieve the list of hidden columns
          For Each node As System.Xml.XmlNode In sheetDoc.SelectNodes("//d:cols/d:col", nsManager)
            Dim hiddenAttr As XmlAttribute = node.Attributes("hidden")
            If (Not (hiddenAttr) Is Nothing) Then
                If (hiddenAttr.Value = "1") Then
                   '  Get the range of columns that are hidden.
                   Dim minAttr As XmlAttribute = node.Attributes("min")
                   Dim maxAttr As XmlAttribute = node.Attributes("max")
                   If ((Not (minAttr) Is Nothing) AndAlso (Not (maxAttr) Is Nothing)) Then
                      '  Finally, add the range of values to the list.
                      Dim minValue As Integer = Convert.ToInt32(minAttr.Value)
                      Dim maxValue As Integer = Convert.ToInt32(maxAttr.Value)
                      Dim i As Integer = minValue
                      Do While (i <= maxValue)
                         itemList.Add(i)
                         i = (i + 1)
                      Loop
                   End If
                 End If
              End If
            Next
         End If
      End If
   End If
   Return itemList
End Function

C#
public static List<int> XLDetectHiddenRowsOrCols(string fileName, string sheetName, bool detectRows)
{
   const string worksheetSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

   //  Set up the return value list.
   List<int> itemList = new System.Collections.Generic.List<int>();

   using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, false))
   {
      //  Load the contents of the workbook.
      XmlDocument doc = new XmlDocument();
      doc.Load(xlDoc.WorkbookPart.GetStream());

      //  Create a namespace manager, so you can search.
      //  Add a prefix for the default namespace.
      XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
      nsManager.AddNamespace("d", worksheetSchema);

      string searchString = string.Format("//d:sheet[@name='{0}']", sheetName);
      XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager);
      if (sheetNode != null)
      {
          //  Get the relId attribute.
          XmlAttribute relationAttribute = sheetNode.Attributes["r:id"];
          if (relationAttribute != null)
          {
            string relId = relationAttribute.Value;

            WorksheetPart sheetPart = (WorksheetPart)xlDoc.WorkbookPart.GetPartById(relId);
            //  First, get the relationship between the document and the sheet.

            //  Load the contents of the workbook.
            XmlDocument sheetDoc = new XmlDocument();
            sheetDoc.Load(sheetPart.GetStream(FileMode.Open));

            if (detectRows)
            {
                //  Retrieve the list of hidden rows.
                foreach (System.Xml.XmlNode node in sheetDoc.SelectNodes("//d:row[@hidden='1']", nsManager))
                {
                  //  For each hidden row, add information to the output list.
                  XmlAttribute rowAttr = node.Attributes["r"];
                  if (rowAttr != null)
                  {
                      itemList.Add(Convert.ToInt32(rowAttr.Value));
                  }
                }
            }
            else
            {
               //  Retrieve the list of hidden columns.
               foreach (System.Xml.XmlNode node in sheetDoc.SelectNodes("//d:cols/d:col", nsManager))
               {
                  XmlAttribute hiddenAttr = node.Attributes["hidden"];
                  if (hiddenAttr != null)
                  {
                      if (hiddenAttr.Value == "1")
                      {
                          //  Get the range of columns that are hidden.
                          XmlAttribute minAttr = node.Attributes["min"];
                          XmlAttribute maxAttr = node.Attributes["max"];
                          if (minAttr != null && maxAttr != null)
                          {
                             //  Finally, add the range of values to the list.
                             int minValue = Convert.ToInt32(minAttr.Value);
                             int maxValue = Convert.ToInt32(maxAttr.Value);
                             for (int i = minValue; i <= maxValue; i++)
                             {
                                itemList.Add(i);
                             }
                          }
                      }
                   }
                }
             }
          }
       }
   }
   return itemList;
}

이 과정에서는 Excel 2007 통합 문서의 전체 경로, 워크시트 이름, 한층 더 숨겨진 행을 검색할지를 지정하는 Boolean 값 라는 세가지 매개 변수를 전달합니다. detectRows true인 경우, 숨겨진 행의 목록이 반환됩니다. detectRows false인 경우, 숨겨진 열의 목록이 반환됩니다. 다음은 SpreadsheetDocument 개체의 Open 메서드를 사용하여, 입력파일을 Open XML 패키지로서 엽니다. XML DOM 문서에 통합 문서의 내용을 로드합니다. 또한 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. d 수식자를 사용하여  기본값 SpreadsheetML 네임 스페이스 참조를 설정합니다. 이 문서내에서//d:sheet 노드의 name 특성을 사용하여, 지정된 워크시트를 나타내는 노드를 검색합니다.

해당 노드가 발견되면 통합 문서와 워크시트 관계가 검색됩니다. 우선, 워크시트 내용을 로드합니다. detectRows true인 경우, //d:row 노드의 hidden 특성을 사용하여, 숨겨진 행의 목록을 검색합니다.

Note메모 :

행의 번호는 1에서 시작됩니다. 열의 번호는 0에서 시작됩니다.

detectRows false인 경우, 숨겨진 열을 검색하여 처리합니다. 여기에는 //d:cols/d:col 노드의 hidden 특성이 사용됩니다.

마지막으로 항목의 목록이 호출자 프로시저에 반환됩니다.


워크시트 그래프 보내기

Excel 2007 통합 문서와 그래프의 제목을 지정하고, Word 2007 파일을 생성하여, 그래프를 보냅니다.


Visual Basic
Public Sub XLExportChart(ByVal inputFileName As String, ByVal outputFileName As String, ByVal chartTitle As String)
   Const drawingMLSchema As String = "http://schemas.openxmlformats.org/drawingml/2006/main"
   Const chartMLSchema As String = "http://schemas.openxmlformats.org/drawingml/2006/chart"
   Dim chartFound As Boolean = False
   Dim xlDoc As SpreadsheetDocument = SpreadsheetDocument.Open(inputFileName, False)
   For Each sheetPart As WorksheetPart In xlDoc.WorkbookPart.WorksheetParts
      If (sheetPart.DrawingsPart Is Nothing) Then
         Continue For
      End If
      '  Loop through all the parts related to the worksheet.
      For Each chartPart As ChartPart In sheetPart.DrawingsPart.ChartParts
         '  A chart is found. Does it have the correct title?
         '  Create a namespace manager, so you can search.
         Dim nt As NameTable = New NameTable
         Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(nt)
         nsManager.AddNamespace("c", chartMLSchema)
         nsManager.AddNamespace("a", drawingMLSchema)
         '  Load the chart document from the part's stream, and 
         '  search for the requested title.
         Dim chartDoc As XmlDocument = New XmlDocument(nt)
         chartDoc.Load(chartPart.GetStream)
         Dim titleNode As XmlNode = chartDoc.SelectSingleNode("//c:chart//c:title//a:t", nsManager)
         If (Not (titleNode) Is Nothing) Then
            If (String.Compare(titleNode.InnerText, chartTitle, True) = 0) Then
               Dim newDoc As WordprocessingDocument = WordprocessingDocument.Create(outputFileName, WordprocessingDocumentType.Document)
               newDoc.AddMainDocumentPart()
            newDoc.MainDocumentPart.AddPart(Of ChartPart)(chartPart)
               '  Tell the outer loops that you are finished.
               chartFound = True
             End If
         End If
      Next
      '  Because you need to export only a single chart, exit.
      If chartFound Then
         Exit Sub
      End If
   Next
End Sub

C#
public static void XLExportChart(string inputFileName, string outputFileName, string chartTitle)
{
   const string drawingMLSchema = "http://schemas.openxmlformats.org/drawingml/2006/main";
   const string chartMLSchema = "http://schemas.openxmlformats.org/drawingml/2006/chart";

   bool chartFound = false;

   using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(inputFileName, false))
   {
      foreach (WorksheetPart sheetPart in xlDoc.WorkbookPart.WorksheetParts)
      {
          if (sheetPart.DrawingsPart == null)
            continue;
          //  Loop through all the parts related to the worksheet.
          foreach (ChartPart chartPart in sheetPart.DrawingsPart.ChartParts)
          {
              //  A chart is found. Does it have the correct title?      

              //  Create a namespace manager, so you can search.
              NameTable nt = new NameTable();
              XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
              nsManager.AddNamespace("c", chartMLSchema);
              nsManager.AddNamespace("a", drawingMLSchema);

              //  Load the chart document from the part's stream, and 
              //  search for the requested title.
              XmlDocument chartDoc = new XmlDocument(nt);
              chartDoc.Load(chartPart.GetStream());

              XmlNode titleNode = chartDoc.SelectSingleNode("//c:chart//c:title//a:t", nsManager);
              if (titleNode != null)
              {
                 if (string.Compare(titleNode.InnerText, chartTitle, true) == 0)
                 {
                    using (WordprocessingDocument newDoc = WordprocessingDocument.Create(outputFileName, WordprocessingDocumentType.Document))
                    {
                        newDoc.AddMainDocumentPart();
                        newDoc.MainDocumentPart.AddPart<ChartPart>(chartPart);
                    }

                    //  Tell the outer loops that you are finished.
                    chartFound = true;
                 }
            }
          }
          //  You need to export only a single chart, so get out now.
          if (chartFound)
          {
            break;
          }
      }
   }
}

이 과정에서는 통합 문서의 전체 경로와 출력 파일의 전체 경로 및 그래프 이름이라는 세가지 매개 변수를 전달합니다.

Note메모 :

이 과정에서는 그래프 파트의 일시적인 저장소로서 Word 2007 문서를 사용합니다. 그 때문에 관계는 정의되지 않습니다. 따라서 이 문서를 Word에서 열릴 수 없습니다. 이 과정의 코드는 문서 패키지에 파트를 적절히 추가하는 코드에 옮겨놓을 수 있습니다. 이러한 코드의 예는 「2007 Office system: Microsoft SDK for Open XML Formats」를 참조해 주세요.

우선, SpreadsheetDocument 개체의 Open 메서드를 사용하여 입력파일을 Open XML 패키지로서 엽니다. 다음은 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. sh 수식자를 사용하여  기본값 chartML 네임 스페이스 참조를 a 수식자를 사용하여 drawingML 네임 스페이스 참조를 설정합니다.

각 chartPart 파트를 반복 처리하고, //c:chart//c:title//a:t XPath 식을 사용하여 그래프의 제목을 검색합니다. 그래프가 발견되면 Word 2007 문서가 생성되어 패키지에 chartPart 파트를 보냅니다.


숨겨진 워크시트 목록 가져오기

다음 코드 예에서는 워크시트 모든 숨겨진 워크시트 목록을 생성합니다.

Visual Basic
Public Function XLGetHiddenSheets(ByVal fileName As String) As List(Of String)
   Dim sheets As List(Of String) = New List(Of String)()
   Dim xlDoc As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)
   '  Get the main document part (workbook.xml).
   Dim doc As XmlDocument = New XmlDocument
   doc.Load(xlDoc.WorkbookPart.GetStream)
   '  Create a NamespaceManager to handle the default namespace, 
   '  and create a prefix for the default namespace.
   Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
   nsManager.AddNamespace("default", doc.DocumentElement.NamespaceURI)
   For Each node As System.Xml.XmlNode In doc.SelectNodes("//default:sheets//default:sheet[@state='hidden']", nsManager)
      Dim sheetName As String = node.Attributes("name").Value
      sheets.Add(sheetName)
   Next
   Return sheets
End Function

C#
public static List<string> XLGetHiddenSheets(string fileName)
{
   List<string> sheets = new List<string>();

   using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, false))
   {
      //  Get the main document part (workbook.xml).
      XmlDocument doc = new XmlDocument();
      doc.Load(xlDoc.WorkbookPart.GetStream());

      //  Create a NamespaceManager to handle the default namespace, 
      //  and create a prefix for the default namespace.
      XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
      nsManager.AddNamespace("default", doc.DocumentElement.NamespaceURI);

      foreach (System.Xml.XmlNode node in doc.SelectNodes("//default:sheets//default:sheet[@state='hidden']", nsManager))
      {
         string sheetName = node.Attributes["name"].Value;
         sheets.Add(sheetName);
      }
   }
   return sheets;
}

이 과정에서는 1 개의 매개 변수 (통합 문서의 전체 경로)를 전달합니다. 다음은 SpreadsheetDocument 개체의 Open 메서드를 사용하여 입력파일을 Open XML 패키지로서 열어, 데이터를 XML 문서에 로드합니다. 또한 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. 기본값 SpreadsheetML 네임 스페이스 참조를 설정 하고, 숨겨진 워크시트를 검색합니다.

이 문서 내에서 //default:sheets//default:sheet 노드의state 특성을 사용하여 숨겨진 워크시트를 검색합니다. 숨겨진 워크시트 이름이 쉬트 목록에 추가됩니다.
모든 워크시트가 검색되면, 호출자의 프로시저에 쉬트 목록이 반환됩니다.


워크시트내의 셀 값 검색

다음 코드에서는 워크시트내의 셀의 값을 검색합니다.

Visual Basic
Public Function XLGetCellValue(ByVal fileName As String, ByVal sheetName As String, ByVal addressName As String) As String
   Const worksheetSchema As String = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
   Const sharedStringSchema As String = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
   Dim cellValue As String = Nothing
   '  Retrieve the stream containing the requested
   '  worksheet's information.
   Dim xlDoc As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)
   '  Get the main document part (workbook.xml).
   Dim doc As XmlDocument = New XmlDocument
   doc.Load(xlDoc.WorkbookPart.GetStream)
   '  Create a namespace manager, so you can search.
   '  Add a prefix (d) for the default namespace.
   Dim nt As NameTable = New NameTable
   Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(nt)
   nsManager.AddNamespace("d", worksheetSchema)
   nsManager.AddNamespace("s", sharedStringSchema)
   Dim searchString As String = String.Format("//d:sheet[@name='{0}']", sheetName)
   Dim sheetNode As XmlNode = doc.SelectSingleNode(searchString, nsManager)
   If (Not (sheetNode) Is Nothing) Then
      '  Get the relId attribute.
      Dim relationAttribute As XmlAttribute = sheetNode.Attributes("r:id")
      If (Not (relationAttribute) Is Nothing) Then
         Dim relId As String = relationAttribute.Value
         '  Load the contents of the workbook.
         Dim sheetDoc As XmlDocument = New XmlDocument(nt)
         sheetDoc.Load(xlDoc.WorkbookPart.GetPartById(relId).GetStream)
         Dim cellNode As XmlNode = sheetDoc.SelectSingleNode(String.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName), nsManager)
         If (Not (cellNode) Is Nothing) Then
            Dim typeAttr As XmlAttribute = cellNode.Attributes("t")
            Dim cellType As String = String.Empty
            If (Not (typeAttr) Is Nothing) Then
               cellType = typeAttr.Value
            End If
            Dim valueNode As XmlNode = cellNode.SelectSingleNode("d:v", nsManager)
            If (Not (valueNode) Is Nothing) Then
               cellValue = valueNode.InnerText
            End If
            If (cellType = "b") Then
               If (cellValue = "1") Then
                  cellValue = "TRUE"
               Else
                  cellValue = "FALSE"
               End If
            ElseIf (cellType = "s") Then
               If (Not (xlDoc.WorkbookPart.SharedStringTablePart) Is Nothing) Then
                   Dim stringDoc As XmlDocument = New XmlDocument(nt)
                   stringDoc.Load(xlDoc.WorkbookPart.SharedStringTablePart.GetStream)
                   '  Add the string schema to the namespace manager.
                   nsManager.AddNamespace("s", sharedStringSchema)
                   Dim requestedString As Integer = Convert.ToInt32(cellValue)
                   Dim strSearch As String = String.Format("//s:sst/s:si[{0}]", (requestedString + 1))
                   Dim stringNode As XmlNode = stringDoc.SelectSingleNode(strSearch, nsManager)
                   If (Not (stringNode) Is Nothing) Then
                      cellValue = stringNode.InnerText
                   End If
                End If
            End If
         End If
      End If
   End If
   Return cellValue
End Function

C#
public static string XLGetCellValue(string fileName, string sheetName, string addressName)
{
   const string worksheetSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
   const string sharedStringSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";

   string cellValue = null;

   //  Retrieve the stream containing the requested
   //  worksheet's info.
   using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(fileName, false))
   {
      //  Get the main document part (workbook.xml).
      XmlDocument doc = new XmlDocument();
      doc.Load(xlDoc.WorkbookPart.GetStream());

      //  Create a namespace manager, so you can search.
      //  Add a prefix (d) for the default namespace.
      NameTable nt = new NameTable();
      XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
      nsManager.AddNamespace("d", worksheetSchema);
      nsManager.AddNamespace("s", sharedStringSchema);

      string searchString = string.Format("//d:sheet[@name='{0}']", sheetName);
      XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager);
      if (sheetNode != null)
      {
         //  Get the relId attribute.
          XmlAttribute relationAttribute = sheetNode.Attributes["r:id"];
         if (relationAttribute != null)
         {
            string relId = relationAttribute.Value;
            //  Load the contents of the workbook.
            XmlDocument sheetDoc = new XmlDocument(nt);
            sheetDoc.Load(xlDoc.WorkbookPart.GetPartById(relId).GetStream());

            XmlNode cellNode = sheetDoc.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName), nsManager);
            if (cellNode != null)
            {
               XmlAttribute typeAttr = cellNode.Attributes["t"];
               string cellType = string.Empty;
               if (typeAttr != null)
               {
                  cellType = typeAttr.Value;
               }

               XmlNode valueNode = cellNode.SelectSingleNode("d:v", nsManager);
               if (valueNode != null)
               {
                  cellValue = valueNode.InnerText;
               }
               if (cellType == "b")
               {
                  if (cellValue == "1")
                  {
                     cellValue = "TRUE";
                  }
                  else
                  {
                     cellValue = "FALSE";
                  }
               }
               else if (cellType == "s")
               {
                   if (xlDoc.WorkbookPart.SharedStringTablePart != null)
                   {
                      XmlDocument stringDoc = new XmlDocument(nt);
                      stringDoc.Load(xlDoc.WorkbookPart.SharedStringTablePart.GetStream());
                      //  Add the string schema to the namespace manager.
                      nsManager.AddNamespace("s", sharedStringSchema);

                      int requestedString = Convert.ToInt32(cellValue);
                      string strSearch = string.Format("//s:sst/s:si[{0}]", requestedString + 1);
                      XmlNode stringNode = stringDoc.SelectSingleNode(strSearch, nsManager);
                      if (stringNode != null)
                      {
                          cellValue = stringNode.InnerText;
                      }
                   }
                }
            }
         }
       }
   }
   return cellValue;
}

이 과정에서는 통합 문서의 전체 경로, 워크시트 이름 및 검색하는 값이 포함되는 셀 주소의 세가지 매개 변수를 전달합니다. 다음은 SpreadsheetDocument 개체의 Open 메서드를 사용하여 입력파일을 Open XML 패키지로서 열어, 데이터를 XML 문서에 로드합니다. 다음은 XmlNamespaceManager 개체를 사용하여  네임 스페이스 관리자를 설정합니다. d 수식자를 사용하여  기본값 worksheetSchema 네임 스페이스 참조를 s 수식자를 사용하여 sharedStringSchema 네임 스페이스 참조를 설정합니다. sharedStringSchema 네임 스페이스는 SharedStringTablePart 파트를 참조합니다. 이 파트에는 여러 개의 셀에서 공유되는 문자열이 보관됩니다.

//d:sheet 노드의 name 특성을 선택하여 메인 통합 문서 파트내의 지정 워크시트를 나타내는 노드를 검색합니다. 노드가 발견되면 그 워크시트 관계 ID 를 검색하여, 그 ID 를 사용하여  워크시트를 XML 문서에 로드합니다. 다음은 그 값을 검색합니다. 그 노드의 t 특성에 s가 보관된 경우, 이것은 공유 문자열이기 때문에 SharedStringTablePart 파트내에서 참조해야 합니다. 그 이외의 경우, 값은 노드에서 직접 얻을 수 있습니다.

Note메모 :

이 코드에서는 Boolean 값과 String 값만을 개별적으로 체크합니다.


마지막에 셀의 값 또는 검색이 정상적으로 실행되었는지를 보여주는 Boolean 값을 반환합니다.


요약

Microsoft SDK for Open XML Formats 기술 프리뷰」를 참조하여 Excel 2007 및 PowerPoint 2007 파일의 조작이 매우 간단하게 됩니다.
이 시리즈의  2부에서는 Excel 2007 파일 및 PowerPoint 2007 파일을 사용하여  실행할 수 있는작업에 대해 설명합니다.


© 2009 Microsoft Corporation. All rights reserved. 사용약관 | 상표 | 개인정보취급방침 및 청소년보호정책
Page view tracker