セルのスタイルCellFormats
は、Excel ドキュメントのセクションで処理されます。XML を見て、s
属性が整数に設定されていることを確認すると、セルにフォーマットがある場合がわかります。
<x:c r="A1" s="1" t="s" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:v>0</x:v>
</x:c>
その属性は、このセルの書式設定に対応するリスト内のインデックスでStyleIndex
ある を表します。うまくいけば、もう少し明確にするためにの XML を次に示します。CellFormat
CellFormats
CellFormats
<x:cellXfs count="2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" />
<x:xf numFmtId="0" fontId="0" fillId="1" borderId="1" xfId="0" />
</x:cellXfs>
上記の XML には、x:cellXfs
要素である要素があり、それにはor要素CellFormats
の 2 つの子があります。属性から、要素の下に最初のインデックス (または 2 番目の要素) が必要であることがわかります。つまり、これが必要です。x:xf
CellFormat
StyleIndex
CellFormats
CellFormat
<x:xf numFmtId="0" fontId="0" fillId="1" borderId="1" xfId="0" />
セルのテキストを回転させるには、 を介してそれを制御する必要がありますCellFormat
。90 度回転する CellFormat を作成するために使用する必要があるコードは次のとおりです。
public CellFormat GenerateCellFormat()
{
CellFormat cellFormat1 = new CellFormat(){ NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyAlignment = true };
Alignment alignment1 = new Alignment(){ TextRotation = (UInt32Value)90U };
cellFormat1.Append(alignment1);
return cellFormat1;
}
テキストを別の角度で回転させたい場合は、90U を -90 から 90 までの任意の角度に置き換えます。
これを に挿入してから、回転させたいセルの に新しいインデックスを設定する必要がCellFormat
ありますCellFormats
。StyleIndex
Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex);
cell.StyleIndex = InsertCellFormat(workbookPart, GenerateCellFormat());
// Helper method to insert the cell format in the CellFormats
public static uint InsertCellFormat(WorkbookPart workbookPart, CellFormat cellFormat)
{
CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First();
cellFormats.Append(cellFormat);
return (uint)cellFormats.Count++;
}