セルの書式設定

セルまたはセル範囲の書式設定

セルまたはセル範囲の書式設定を行いたい場合、Aspose.CellsはStyleクラスを提供しています。このクラスを使用して、セルやセル範囲の書式設定をすべて行うことができます。IStyleクラスを使用して達成できる書式設定に関連するいくつかのことは以下の通りです

  • セルの塗りつぶしの色を設定する
  • セルのテキスト折り返しを設定する
  • セルの上部、左部、下部、右側の境界など、セルの境界線を設定する
  • フォントの色、サイズ、名前、取り消し線、太字、斜体、下線などを設定します。
  • テキストの水平または垂直配置を右、左、上、下、中央などに設定します。

個々のセルのスタイルを設定する場合は、Cell->SetStyle() メソッドを使用し、セルの範囲のスタイルを設定する場合は、Range->ApplyStyle() メソッドを使用してください。

サンプルコード

以下のサンプルコードは、ワークシートのセルC4の書式をさまざまな方法で設定し、それによって生成された 出力エクセルファイル を参照してください。

todo:image_alt_text

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of output excel file
U16String outputFormatCellOrRangeOfCells = outPath + "outputFormatCellOrRangeOfCells.xlsx";
//Create a new workbook
Workbook wb;
//Get first worksheet which is created by default
Worksheet ws = wb.GetWorksheets().Get(0);
//Access cell C4 by cell name
Cell cell = ws.GetCells().Get(u"C4");
//Add some text in cell
cell.PutValue((U16String)u"This is sample data.");
//Access the cell style
Style st = cell.GetStyle();
//Fille the cell color to Yellow
st.SetPattern(BackgroundType::Solid);
st.SetForegroundColor(Color{ 0xff, 0xff, 0xff, 0 });//Yellow
//Set the text to wrapp
st.SetIsTextWrapped(true);
//Set the left and right border to Red
st.SetBorder(BorderType::LeftBorder, CellBorderType::Thick, Color{ 0xff, 0xff, 0, 0 });//Red
st.SetBorder(BorderType::RightBorder, CellBorderType::Thick, Color{ 0xff, 0xff, 0, 0 });//Red
//Set font color, font size, strike, bold, italic
st.GetFont().SetColor(Color{ 0xff, 0, 0, 0xff });//Blue
st.GetFont().SetSize(16);
st.GetFont().SetStrikeType(TextStrikeType::Single);
st.GetFont().SetIsBold(true);
st.GetFont().SetIsItalic(true);
//Set text horizontal and vertical alignment to center
st.SetHorizontalAlignment(TextAlignmentType::Center);
st.SetVerticalAlignment(TextAlignmentType::Center);
//Set the cell style
cell.SetStyle(st);
//Set the cell column width and row height
ws.GetCells().SetColumnWidth(cell.GetColumn(), 20);
ws.GetCells().SetRowHeight(cell.GetRow(), 70);
//Save the output excel file
wb.Save(outputFormatCellOrRangeOfCells);
Aspose::Cells::Cleanup();