Drawing a line in excel using apache-poi

坚强是说给别人听的谎言 提交于 2019-12-24 16:45:51

问题


Can anybody write me a few lines of code showing how to write a line in a cell in excel, using apache poi ?! In plain Excel, I would go to Insert - Shapes - Line. Basicaly, make code like this:

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

now, missing code would go here. As i searched the net, I should be using Class HSSFSimpleShape and OBJECT_TYPE_LINE. But i dont know how to implement it in my code :(

I guess I should have the cell which i want to draw the line in or some pixels as a coordinaets or something.

Help ! :)


回答1:


Check out this example:

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"); 

I also recommend to take a look at HSSFClientAnchor Javadoc



来源:https://stackoverflow.com/questions/13907788/drawing-a-line-in-excel-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!