谁能给我写几行代码,展示如何使用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类。
但是我不知道如何在我的代码中实现它:(

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

救命 ! :)

最佳答案

看看这个例子:



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

关于java - 使用apache-poi在excel中画一条线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13907788/

10-09 02:47