在OPEN XML SDK 2.0中的单元格中使用不同样式的两个CellValues

我的任务是使用OPEN XML SDK 2.0,并遇到了这个问题。 单元格内的单个CellValue可能具有不同的样式,如下图所示:

在这里输入图像说明

答:纯文本

B:粗体和下划线

注:我需要在一个单一的单元格只感谢:)

是的,这是可能的。 一种方法是格式化将被插入到SharedStringTable 。 这段代码将创build您的示例上面:

  // Creates an SharedStringItem instance and adds its children. public SharedStringItem GenerateSharedStringItem() { SharedStringItem sharedStringItem1 = new SharedStringItem(); Run run1 = new Run(); RunProperties runProperties1 = new RunProperties(); Bold bold1 = new Bold(); Underline underline1 = new Underline(); 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(underline1); runProperties1.Append(fontSize1); runProperties1.Append(color1); runProperties1.Append(runFont1); runProperties1.Append(fontFamily1); runProperties1.Append(fontScheme1); Text text1 = new Text(); text1.Text = "Project Name:"; run1.Append(runProperties1); run1.Append(text1); Run run2 = new Run(); RunProperties runProperties2 = new RunProperties(); FontSize fontSize2 = new FontSize(){ Val = 11D }; Color color2 = new Color(){ Theme = (UInt32Value)1U }; RunFont runFont2 = new RunFont(){ Val = "Calibri" }; FontFamily fontFamily2 = new FontFamily(){ Val = 2 }; FontScheme fontScheme2 = new FontScheme(){ Val = FontSchemeValues.Minor }; runProperties2.Append(fontSize2); runProperties2.Append(color2); runProperties2.Append(runFont2); runProperties2.Append(fontFamily2); runProperties2.Append(fontScheme2); Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve }; text2.Text = " ALLAN"; run2.Append(runProperties2); run2.Append(text2); sharedStringItem1.Append(run1); sharedStringItem1.Append(run2); return sharedStringItem1; } 

您可以将其插入到SharedStringTable ,然后将单元格值设置为插入到SharedStringTable中的索引。

可能有一些其他的引用,我忘了包括可能在StylesPart定义的StylesPart 。 我build议在空白的Excel文档中创build这个例子,然后使用Open XML Productivity Tool来查看XML。 该工具还将为您提供上面提供的代码。 它应该给你一个大致的方向去下一步。