java POI导出excel画斜线

想要实现这样的效果

java POI导出excel画斜线_第1张图片

1、HSSF画斜线(wps可打开,office老版本可打开)

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheetName");
sheet.setColumnWidth(0, 5600);
Row row1 = sheet.createRow(0);
row1.setHeight((short) 800);
//画线(由左上到右下的斜线)  在A1的第一个cell(单位  分类)加入一条对角线
Row row = sheet.getRow(0);
Cell cell0 = row.createCell(0);
cell0.setCellValue("xxxx         xxxx");
HSSFPatriarch drawingPatriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 0, 0);
HSSFSimpleShape simpleShape = drawingPatriarch.createSimpleShape(a);
simpleShape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
simpleShape.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID);
//填充你的数据
//......
//导出
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
return os;

2、XSSF画斜线(wps可打开,office可打开,且画出来效果更好)

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheetName");
sheet.setColumnWidth(0, 6500);
Row row1 = sheet.createRow(0);
row1.setHeight((short) 800);
//画线(由左上到右下的斜线)  在A1的第一个cell(单位  分类)加入一条对角线
Row row = sheet.getRow(0);
Cell cell0 = row.createCell(0);
cell0.setCellValue("xxxx         xxxx");
CreationHelper helper = workbook.getCreationHelper();
XSSFDrawing xssfDrawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
//设置斜线的开始位置,我这里是从第0行到第1行,从第0列到第1列
anchor.setCol1(0);
anchor.setCol2(1);
anchor.setRow1(0);
anchor.setRow2(1);
XSSFSimpleShape simpleShape = xssfDrawing.createSimpleShape((XSSFClientAnchor) anchor);
//设置形状类型未线型
simpleShape.setShapeType(ShapeTypes.LINE);
//设置线宽
simpleShape.setLineWidth(0.5);
//设置线的风格
simpleShape.setLineStyle(0);
//设置线的颜色
simpleShape.setLineStyleColor(0,0,0);
//填充你的数据
//......
//导出
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
return os;

以上是两种方式,供参考。

你可能感兴趣的:(excel,java)