XSSFSimpleShape用法
package com.citi.POILineTest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class App
{
public static void main(String[] args) throws FileNotFoundException
{
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0) ;
row.setHeight((short) 500) ;
sheet.setColumnWidth(0, 4000) ;
XSSFCellStyle cellStyle = workbook.createCellStyle() ;
cellStyle.setWrapText(true);
cellStyle.setAlignment((short)2) ;
cellStyle.setVerticalAlignment((short)1) ;
XSSFCell cell = row.createCell(0) ;
cell.setCellValue("姓名 密码") ;
cell.setCellStyle(cellStyle) ;
XSSFDrawing drawing = sheet.createDrawingPatriarch();
paintLine(drawing, drawing.createAnchor(0, 0, 0, 0, 1, 14, 2, 16));
paintLine(drawing, drawing.createAnchor(0, 0, 0, 0, 1, 14, 2, 18));
OutputStream outputStream = new FileOutputStream("test.xlsx") ;
try
{
workbook.write(outputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("generate excel success ! ");
}
private static void paintLine(XSSFDrawing drawing, XSSFClientAnchor anchor) {
XSSFSimpleShape shape = drawing.createSimpleShape(anchor);
//设置图形的类型为线
shape.setShapeType(ShapeTypes.LINE);
//设置填充颜色
shape.setFillColor(0, 0, 0);
//设置边框线型:solid=0、dot=1、dash=2、lgDash=3、dashDot=4、lgDashDot=5、lgDashDotDot=6、sysDash=7、sysDot=8、sysDashDot=9、sysDashDotDot=10
shape.setLineStyle(0);
//设置边框线颜色
shape.setLineStyleColor(0, 0, 0);
//设置边框线宽,单位Point
shape.setLineWidth(1);
}
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
package com.citi.main;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
class BottomLeftTopRight {
public static void main(String[] args) throws Exception{
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
//Anchor B2:C4
//This determines the size of the line shape to be from
//upper left edge of B2 to upper left edge of C4 plus dx and dy.
//Since dx and dy are both 0, this is the bottom right edge of B3.
anchor.setCol1(1);
anchor.setRow1(1);
anchor.setCol2(2);
anchor.setRow2(3);
//From here on XSSF only.
XSSFDrawing xssfdrawing = (XSSFDrawing)drawing;
XSSFClientAnchor xssfanchor = (XSSFClientAnchor)anchor;
//Draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3.
//This is the default.
// XSSFSimpleShape xssfshape = xssfdrawing.createSimpleShape(xssfanchor);
// xssfshape.setShapeType(ShapeTypes.LINE);
// xssfshape.setLineWidth(1);
// xssfshape.setLineStyle(0);
// xssfshape.setLineStyleColor(0, 0, 0);
//This sets the arrow line end type:
// xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
// org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE);
//Again draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3.
XSSFSimpleShape xssfshapeFlipV = xssfdrawing.createSimpleShape(xssfanchor);
xssfshapeFlipV.setShapeType(ShapeTypes.LINE);
xssfshapeFlipV.setLineWidth(1);
xssfshapeFlipV.setLineStyle(0);
xssfshapeFlipV.setLineStyleColor(0, 0, 0);
//Now flip this vertically.
//So it now will to be from bottom left edge of B3 to upper left edge of B2.
xssfshapeFlipV.getCTShape().getSpPr().getXfrm().setFlipV(true);
// xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
// org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE);
workbook.write(new FileOutputStream("CreateExcelLineShapes.xlsx"));
workbook.close();
}
}