2011-08-30 16 views
6

DocumentFormat.OpenXml.Spreadsheet के साथ एक्सेल के लिए ओपन एक्सएमएल का उपयोग करके, मैं बोल्ड में टेक्स्ट का केवल एक हिस्सा कैसे सेट करूं?ओपन एक्सएमएल एसडीके: एक्सेल-सेल

var cell = new Cell { 
    //DataType = CellValues.InlineString, 
    CellReference = "A" + 1 
}; 

// TODO: Set "bold text" to bold style 
//var inlineString = new InlineString(); 
//inlineString.AppendChild(new Text { Text = "Normal text... bold text..." }); 
//cell.AppendChild(inlineString); 

अब कोड का उपयोग किया गया है, लेकिन इसे बदला जाना चाहिए या शायद बदला जाना चाहिए।

उत्तर

6

टेक्स्ट को केवल बोल्ड में सेट करने के लिए आप इसे SharedStringTable में अपना टेक्स्ट डालने और अपने सेल का डेटा प्रकार SharedString और InlineString पर नियंत्रित करके नियंत्रित करना चाहते हैं। यह सेलवैलू को इस तालिका में संदर्भित करेगा, जैसे कि 0, 1, 2 इत्यादि। और इनलाइन स्ट्रिंग करने के बाद अधिक नियंत्रण की अनुमति दें।

यहाँ कैसे pharse के दूसरे भाग बनाने के लिए पर कुछ नमूना कोड है "सामान्य पाठ ... बोल्ड पाठ ..." बोल्ड:

 // Creates an SharedStringItem instance and adds its children. 
     public SharedStringItem GenerateSharedStringItem() 
     { 
      SharedStringItem sharedStringItem1 = new SharedStringItem(); 

      Run run1 = new Run(); 
      Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve }; 
      text1.Text = "Normal text… "; 

      run1.Append(text1); 

      Run run2 = new Run(); 

      RunProperties runProperties1 = new RunProperties(); 
      Bold bold1 = new Bold(); 
      FontSize fontSize1 = new FontSize(){ Val = 11D }; 
      Color color1 = new Color(){ Theme = (UInt32Value)1U }; 
      RunFont runFont1 = new RunFont(){ Val = "Calibri" }; 
      FontFamily fontFamily1 = new FontFamily(){ Val = 2 }; 
      FontScheme fontScheme1 = new FontScheme(){ Val = FontSchemeValues.Minor }; 

      runProperties1.Append(bold1); 
      runProperties1.Append(fontSize1); 
      runProperties1.Append(color1); 
      runProperties1.Append(runFont1); 
      runProperties1.Append(fontFamily1); 
      runProperties1.Append(fontScheme1); 
      Text text2 = new Text(); 
      text2.Text = "bold text…"; 

      run2.Append(runProperties1); 
      run2.Append(text2); 

      sharedStringItem1.Append(run1); 
      sharedStringItem1.Append(run2); 
      return sharedStringItem1; 
     } 

इस विधि आप पहली बार कोई पता लगाना चाहते हैं का उपयोग करने के SharedStringTable और उसके बाद के कहने इसे में अपने नए ShareStringItem सम्मिलित करें:

  using (MemoryStream stream = new MemoryStream()) 
      { 
       // create in-memory copy of the Excel template file 
       byte[] byteArray = File.ReadAllBytes(TEMPLATE_FILE_NAME); 
       stream.Write(byteArray, 0, (int)byteArray.Length); 

       using (SpreadsheetDocument document = SpreadsheetDocument.Open(stream, true)) 
       { 
        // Set private variable template component references (for reuse between methods) 
        mExcelWorkbookPart = document.WorkbookPart; 
        mSharedStringTablePart = mExcelWorkbookPart.SharedStringTablePart; 

        mSharedStringTablePart.SharedStringTable.AppendChild(GenerateSharedStringItem()); 
       } 

       return stream.ToArray(); 
      } 
+0

हाय amurra, आप मुझे http://stackoverflow.com/questions/15791732/openxml-sdk-having-borders-for-cell साथ सहायता कर सकते हैं । सुनिश्चित नहीं है कि सेल बनाने के लिए सीमाएं कैसे हैं। धन्यवाद –

संबंधित मुद्दे