2014-01-23 1 views
0

주어진 템플릿과 변수 사전을 사용하여 .xlsx 파일을 생성하려면 OpenXML 스프레드 시트를 사용하고 있습니다. 하지만 SharedStringItem의 InnerText는 읽기 전용이므로 SharedStringTable의 값을 업데이트 할 때 질문이 생깁니다. 내 템플릿 엑셀 파일은 .xlsx 파일입니다. 대체해야하는 변수에는 접두어 "$$$"이 있습니다. 예를 들어 사전에 "$$$ abc"라고 입력하면 < "abc", "Payson"> 쌍 (사전에 키 "abc"가 없으면 "$$$ abc"를 그대로 둡니다. 내가 무슨 짓을. 는 정해진 접근이없는,이SharedStringTable의 OpenXML 스프레드 시트 업데이트 값

 private void UpdateValue(WorkbookPart wbPart, Dictionary<string, string> dictionary) 
     { 

      var stringTablePart = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault(); 
      if (stringTablePart == null) 
      { 
       stringTablePart = wbPart.AddNewPart<SharedStringTablePart>(); 
      } 
      var stringTable = stringTablePart.SharedStringTable; 
      if (stringTable == null) 
      { 
       stringTable = new SharedStringTable(); 
      } 
      //iterate through all the items in the SharedStingTable 
      // if there is any text starts with $$$, find out the name of the string 
      // look for the string in the dictionary 
      // replace it if found, or keep it if not. 
      foreach (SharedStringItem item in stringTable.Elements<SharedStringItem>()) 
      { 
       if (item.InnerText.StartsWith("$$$")) 
       { 
        string variableName = item.InnerText.Substring(3); 
        if (!string.IsNullOrEmpty(variableName) && dictionary.containsKey(variableName)) 
        { 
         // The problem is here since InnerText is read only. 
         item.InnerText = dictionary[variableName]; 
        } 
       } 
      } 
     } 

문서 같은 것이 여기에서도 문서가 innerText와는하지만, 설정 될 수 있음을 언급 http://msdn.microsoft.com/en-us/library/documentformat.openxml.openxmlcompositeelement.innertext(v=office.14).aspx

입니다. 사람이 알고 있나요 방법 InnterText를 설정하십시오. 동일한 변수 이름이 "$$$ abc"인 많은 셀이있을 수 있으므로 "Payson"으로 모든 셀을 바꾸고 싶습니다.

답변

0

InnerXml을 바꾼 다음 SharedStringTable을 저장해야하는 것으로 보입니다. Set text value using SharedStringTable with xml sdk in .net을 참조하십시오.

+0

안녕하세요. 감사합니다. 그러나, 나는 그것이 문제라고 생각하지 않는다. 나는 아이템에 새로운 것을 할당하지 않는다. 그냥 InnerText 속성을 업데이트하십시오. 그러나이 속성은 가져 오기만하므로 값을 설정할 수 없습니다. 다른 방법이 있는지 알고 싶습니다. SharedStringTable을 편집하려면. 다시 한 번 감사드립니다 – Payson

+0

아마해야 할 일은 SharedStringTable의 개별 요소를 제거하고 추가하는 것입니다. http://msdn.microsoft.com/en-us/library/office/cc797480.aspx에는 SharedStringTable에서 항목을 제거하기위한 코드가 포함되어 있습니다. http://openxmldeveloper.org/discussions/development_tools/f/35/p/6234/160671.aspx에는이를 삽입하기위한 코드가 포함되어 있습니다. – Steve

+0

방법 -이 질문은 이미 http://stackoverflow.com/questions/11061551/set-text-value-using-sharedstringtable-with-xml-sdk-in-net에서 답변을 얻은 것으로 보입니다. 그에 따라 내 대답을 편집했습니다. – Steve

0

텍스트를 편집하려고 시도했지만 작동합니다.

   Text newText = new Text(); 
       newText.Text = dictionary[variableName]; 
       item.Text = newText; 
       stringTable.Save(); 
관련 문제