OpenXMLSDKToolV25.msi (生産性向上ツール) をまだダウンロードしていない場合、openxmnl を操作するのは骨の折れる作業です。
基本的には反射ツールです。Excel ドキュメントを開くと、ツールは同じものをゼロから構築するために必要なすべてのコードを作成します。
CellValue 値専用です。数式を使用すると、セル数式を処理する必要があります。
例えば。A1 = 1256 in B1 = 2 in C1 "=A1*B1" で Excel ファイルを作成し、OpenXMLSDKTool でファイルを開きました。
public Row GenerateRow()
{
Row row1 = new Row(){ RowIndex = (UInt32Value)1U, Spans = new ListValue<StringValue>() { InnerText = "1:3" } };
Cell cell1 = new Cell(){ CellReference = "A1" };
CellValue cellValue1 = new CellValue();
cellValue1.Text = "1256";
cell1.Append(cellValue1);
Cell cell2 = new Cell(){ CellReference = "B1" };
CellValue cellValue2 = new CellValue();
cellValue2.Text = "2";
cell2.Append(cellValue2);
Cell cell3 = new Cell(){ CellReference = "C1" };
CellFormula cellFormula1 = new CellFormula();
cellFormula1.Text = "A1*B1";
CellValue cellValue3 = new CellValue();
cellValue3.Text = "2512";
cell3.Append(cellFormula1);
cell3.Append(cellValue3);
row1.Append(cell1);
row1.Append(cell2);
row1.Append(cell3);
return row1;
}
このことから、CellValue と CellFormula は両方とも Cell の子であることがわかります。したがって、行 r を取得できると仮定すると、次のようになります。
Cell c = r.Elements<Cell>().ElementAt(2);
CellValue cv = c.CellValue;
CellFormula cf = c.CellFormula;