как повернуть HSSFShapeTypes.ThickArrow - PullRequest
0 голосов
/ 26 мая 2020
HSSFSimpleShape p3 = patriarch.createSimpleShape(anchor);
p3.setShapeType(HSSFShapeTypes.ThickArrow);

Я создаю форму в моем excel с помощью apache poi , как повернуть ThickArrow?

enter image description here

1 Ответ

1 голос
/ 26 мая 2020

Во-первых: то, что показывает ваш скриншот, не HSSFShapeTypes.ThickArrow, а HSSFShapeTypes.HomePlate.

Второе: ответ на ваш вопрос - просто использовать HSSFShape.setFlipHorizontal . Направление фигур по умолчанию - слева направо. Так что, если нужна такая же форма слева, ее нужно перевернуть по горизонтали. Итак,

p3.setFlipHorizontal(true);

должно сработать.

Чтобы быть лучшим справочником для других читателей, вот полный пример, который также работает с двоичной файловой системой BIFF HSSF при использовании файловой системы Office Open XML XSSF`:

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;

class CreateExcelThickArrowShape {

 public static void main(String[] args) throws Exception{

  //Workbook workbook = new XSSFWorkbook(); String filePath = "./CreateExcelShapes.xlsx";
  Workbook workbook = new HSSFWorkbook(); String filePath = "./CreateExcelShapes.xls";

  Sheet sheet = workbook.createSheet("Sheet1");

  CreationHelper helper = workbook.getCreationHelper();
  Drawing drawing = sheet.createDrawingPatriarch();

  //Anchor1
  //This determines the size of the shape to be from 
  //upper left edge of B2 to upper left edge of E4.
  ClientAnchor anchor1 = helper.createClientAnchor();
  anchor1.setCol1(1);
  anchor1.setRow1(1); 
  anchor1.setCol2(4);
  anchor1.setRow2(3);

  //Anchor2
  //This determines the size of the shape to be from 
  //upper left edge of F2 to upper left edge of I4.
  ClientAnchor anchor2 = helper.createClientAnchor();
  anchor2.setCol1(5);
  anchor2.setRow1(1); 
  anchor2.setCol2(8);
  anchor2.setRow2(3);

  //From here on XSSF only.
  if (workbook instanceof XSSFWorkbook) {
   XSSFDrawing xssfDrawing = (XSSFDrawing)drawing;
   //This is the default. Right arrow.
   XSSFClientAnchor xssfAnchor = (XSSFClientAnchor)anchor1;
   XSSFSimpleShape xssfShape = xssfDrawing.createSimpleShape(xssfAnchor);
   xssfShape.setShapeType(ShapeTypes.HOME_PLATE);
   xssfShape.setLineWidth(1);
   xssfShape.setLineStyle(0);
   xssfShape.setLineStyleColor(0, 0, 0);

   //This is the default. Right arrow again.
   xssfAnchor = (XSSFClientAnchor)anchor2;
   xssfShape = xssfDrawing.createSimpleShape(xssfAnchor);
   xssfShape.setShapeType(ShapeTypes.HOME_PLATE);
   xssfShape.setLineWidth(1);
   xssfShape.setLineStyle(0);
   xssfShape.setLineStyleColor(0, 0, 0);
   //Now flip this horizontally. -> Left arrow.
   xssfShape.getCTShape().getSpPr().getXfrm().setFlipH(true);
  }

  //From here on HSSF only.
  if (workbook instanceof HSSFWorkbook) {
   HSSFPatriarch hssfDrawing = (HSSFPatriarch)drawing;

   //This is the default. Right arrow.
   HSSFClientAnchor hssfAnchor = (HSSFClientAnchor)anchor1;
   HSSFSimpleShape hssfShape = hssfDrawing.createSimpleShape(hssfAnchor);
   hssfShape.setShapeType(HSSFShapeTypes.HomePlate);
   hssfShape.setLineWidth(1);
   hssfShape.setLineStyle(0);
   hssfShape.setLineStyleColor(0, 0, 0);

   //This is the default. Right arrow again.
   hssfAnchor = (HSSFClientAnchor)anchor2;
   hssfShape = hssfDrawing.createSimpleShape(hssfAnchor);
   hssfShape.setShapeType(HSSFShapeTypes.HomePlate);
   hssfShape.setLineWidth(1);
   hssfShape.setLineStyle(0);
   hssfShape.setLineStyleColor(0, 0, 0);
   //Now flip this horizontally. -> Left arrow.
   hssfShape.setFlipHorizontal(true);
  }


  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();

 }
}

Он производит:

enter image description here

...