1

谁能给我写几行代码,展示如何使用 apache poi 在 excel 的单元格中写一行?!在普通的 Excel 中,我会去插入 - 形状 - 线条。基本上,制作这样的代码:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row=sheet.createRow(0);
Cell cell = row.createCell(0);

现在,缺少的代码会在这里。当我在网上搜索时,我应该使用 HSSFSimpleShape 类和 OBJECT_TYPE_LINE。但我不知道如何在我的代码中实现它:(

我想我应该有我想在其中画线的单元格或一些像素作为坐标或其他东西。

帮助 !:)

4

1 回答 1

2

看看这个例子:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();

/* Here is the thing: the line will go from top left in cell (0,0) to down left 
of cell (0,1) */
HSSFClientAnchor anchor = new HSSFClientAnchor(
  0, 0, 0, 255, (short) 0, 0,(short) 1, 0);

HSSFSimpleShape shape = patriarch.createSimpleShape(anchor);
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape.setLineStyleColor(10, 10, 10);
shape.setFillColor(90, 10, 200);
shape.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT);
shape.setLineStyle(HSSFShape.LINESTYLE_SOLID);

// you don't even need the cell, but if you also want to write anything...
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test"); 

我还建议看看HSSFClientAnchor Javadoc

于 2013-02-28T16:36:10.243 回答