1
我的任務是使用OPEN XML SDK 2.0並遇到此問題。是否有可能有不同的風格爲細胞類似下面的圖片中一個CellValue:在OPEN XML SDK 2.0的單元格中使用不同樣式的兩個CellValues
答:純文本
B:粗體和下劃線
注:我需要在一個單一的細胞只感謝:)
我的任務是使用OPEN XML SDK 2.0並遇到此問題。是否有可能有不同的風格爲細胞類似下面的圖片中一個CellValue:在OPEN XML SDK 2.0的單元格中使用不同樣式的兩個CellValues
答:純文本
B:粗體和下劃線
注:我需要在一個單一的細胞只感謝:)
是的,這是可能的。一種方法是格式化將要插入到SharedStringTable
中的值。這個片段將創建你上面的例子:
// 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
中定義的參考。我建議在空白的Excel文檔中創建此示例,然後使用Open XML Productivity Tool來查看XML。該工具還將爲您提供上述我提供的代碼。它應該給你一個下一步走向的大方向。