2012-12-17 8 views
1

Quelqu'un peut-il m'écrire quelques lignes de code montrant comment écrire une ligne dans une cellule dans Excel, en utilisant apache poi?! En Excel simple, je voudrais aller à Insérer - Formes - Ligne. Fondamentalement, faire le code comme ceci:Dessiner une ligne dans Excel en utilisant apache-poi

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

maintenant, code manquant serait aller là-bas. Comme j'ai cherché sur le net, je devrais utiliser les classes HSSFSimpleShape et OBJECT_TYPE_LINE. Mais je ne sais pas comment le mettre en œuvre dans mon code :(

Je suppose que je devrais avoir la cellule que je veux tracer la ligne ou des pixels comme un coordinaets ou quelque chose.

Aide! :)

+1

Vous ne voyez pas? J'imagine que cela peut vous aider à faire l'affaire: http://www.roseindia.net/tutorials/poi/DrawingShapesExcelSheet.shtml – Dani

Répondre

2

Découvrez cet exemple:

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

Je recommande également de jeter un oeil à HSSFClientAnchor Javadoc

+0

Salut toute idée comment faire la même chose en utilisant XSSF ou SS usermodel? – mee

Questions connexes