Open2

NPOIをつかってC#でExcelファイルを操作するメモ

kiwikiwi

テキストボックスを作るサンプル

IWorkbook workbook = new XSSFWorkbook(); ;
ISheet sheet1 = workbook.CreateSheet("Sheet1");
XSSFDrawing patriarch = (XSSFDrawing)sheet1.CreateDrawingPatriarch();

const int x_size = 10;
const int y_size = 2;
const int x = 1;
const int y = 1;
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, x, y, x + x_size, y + y_size);
anchor.AnchorType = AnchorType.MoveDontResize;

XSSFTextBox textbox = patriarch.CreateTextbox(anchor);
textbox.SetLineStyleColor(255, 0, 0);
textbox.LineStyle = LineStyle.DashDotSys;
textbox.SetText("test of textbox created by NPOI.");
textbox.TextParagraphs[0].TextAlign = TextAlign.CENTER;

FileStream sw = File.Create("Sample_TextBox1.xlsx");
workbook.Write(sw);
sw.Close();

LineStyle のサンプル

kiwikiwi

矢印を作るサンプル
(左上から右下への矢印しか作れないため、それ以外のパターンはX軸/Y軸反転を組み合わせる必要がある)

IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
ICreationHelper helper = workbook.GetCreationHelper();

IClientAnchor anchor = helper.CreateClientAnchor();
anchor.Col1 = 1;
anchor.Row1 = 1;
anchor.Col2 = 3;
anchor.Row2 = 3;

XSSFDrawing xssfdrawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
XSSFClientAnchor xssfanchor = (XSSFClientAnchor)anchor;

XSSFSimpleShape xssfshape = xssfdrawing.CreateSimpleShape(xssfanchor);
xssfshape.ShapeType = (int)ShapeTypes.Line;
xssfshape.LineWidth = 4;
xssfshape.LineStyle = LineStyle.None;
xssfshape.GetCTShape().spPr.ln.tailEnd = new CT_LineEndProperties();
xssfshape.GetCTShape().spPr.ln.tailEnd.type = ST_LineEndType.triangle;

FileStream sw = File.Create("Sample_Line2.xlsx");
workbook.Write(sw);
sw.Close();