在excel POI中创建一个小圆圈

在excel POI中创建一个小圆圈,excel,apache-poi,Excel,Apache Poi,我知道这个问题可能看起来很相似,但我有不同的问题。我知道如何创建一个圆或形状。这就是我想要创造的 excel单元格中心的一个小圆圈 我能做的圆圈,看看教程等是: 这是我用来创建的代码: CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = worksheet.createDrawingPatriarch(); ClientAnchor anchor = help

我知道这个问题可能看起来很相似,但我有不同的问题。我知道如何创建一个圆或形状。这就是我想要创造的

excel单元格中心的一个小圆圈

我能做的圆圈,看看教程等是:

这是我用来创建的代码:

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

       ClientAnchor anchor = helper.createClientAnchor();

       anchor.setCol1(0);
       anchor.setRow1(0); 
       anchor.setCol2(1);
       anchor.setRow2(1); 
       anchor.setDx1(255);
       anchor.setDx2(255);
       anchor.setDy1(0);
       anchor.setDy2(0);

       XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
       shape.setShapeType(ShapeTypes.FLOW_CHART_CONNECTOR);
       shape.setFillColor(255, 0, 0);
我认为这和dx1,dx2,dy1,dy2有关,但在其中设置任何值都并没有效果


我需要以某种方式将该形状缩小

如果该形状仅在一个单元内,则锚定也必须仅为一个单元。然后使用
Dx
Dy
进行定位。但测量单位是特殊的。它是
EMU
英制公制单位

因此,单元格
A1
中椭圆的定位如下:

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

import org.apache.poi.util.Units;

import java.io.FileOutputStream;
import java.io.IOException;


class CenterShapeInCell {

 public static void main(String[] args) {
  try {

   Workbook workbook = new XSSFWorkbook();
   Sheet sheet = workbook.createSheet("Sheet1");

   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);
   row.setHeight((short)(20*20));
   sheet.setColumnWidth(0, 20*256);

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

   ClientAnchor anchor = helper.createClientAnchor();

   //set anchor to A1 only
   anchor.setCol1(0);
   anchor.setRow1(0); 
   anchor.setCol2(0);
   anchor.setRow2(0); 

   //get the cell width of A1
   float cellWidthPx = sheet.getColumnWidthInPixels(0);
System.out.println(cellWidthPx);

   //set wanted shape size
   int shapeWidthPx = 20;
   int shapeHeightPx = 20;

   //calculate the position of left upper edge
   float centerPosPx = cellWidthPx/2f - (float)shapeWidthPx/2f;
System.out.println(centerPosPx);

   //set the position of left edge as Dx1 in unit EMU
   anchor.setDx1(Math.round(centerPosPx * Units.EMU_PER_PIXEL));

   //set the position of right edge as Dx2 in unit EMU
   anchor.setDx2(Math.round((centerPosPx + shapeWidthPx) * Units.EMU_PER_PIXEL));

   //set upper padding
   int upperPaddingPx = 4;

   //set upper padding as Dy1 in unit EMU
   anchor.setDy1(upperPaddingPx * Units.EMU_PER_PIXEL);

   //set upper padding + shape height as Dy2 in unit EMU
   anchor.setDy2((upperPaddingPx + shapeHeightPx) * Units.EMU_PER_PIXEL);

   XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
   shape.setShapeType(ShapeTypes.ELLIPSE);
   shape.setFillColor(255, 0, 0);


   FileOutputStream fileOut = new FileOutputStream("CenterShapeInCell.xlsx");
   workbook.write(fileOut);
   fileOut.close();

  } catch (IOException ioex) {
  }
 }
}
看起来像:


但我怀疑你真正想要的是一种带有交通灯符号的条件格式,比如:

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

import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.io.IOException;

class ConditionalFormattingIconSet {

 public static void main(String[] args) {
  try {

   Workbook workbook = new XSSFWorkbook();

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

   CellStyle cellStyle = workbook.createCellStyle();
   //cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // old apache poi versions
   cellStyle.setAlignment(HorizontalAlignment.CENTER); // current apache poi version 4.1.0
   //cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // old apache poi versions
   cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // current apache poi version 4.1.0

   Cell cell;

   for (int i = 0; i < 3; i++) {
    cell = sheet.createRow(i).createCell(0);
    cell.setCellValue(1+i);
    cell.setCellStyle(cellStyle);
   }

   SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

   ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_TRAFFIC_LIGHTS);

   rule.getMultiStateFormatting().setIconOnly(true);

   ConditionalFormattingRule [] cfRules = {rule};

   CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A3")};

   sheetCF.addConditionalFormatting(regions, cfRules);

   FileOutputStream fileOut = new FileOutputStream("ConditionalFormattingIconSet.xlsx");
   workbook.write(fileOut);
   fileOut.close();

  } catch (IOException ioex) {
  }
 }
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.CellRangeAddress;
导入java.io.FileOutputStream;
导入java.io.IOException;
类条件格式Conset{
公共静态void main(字符串[]args){
试一试{
工作簿=新的XSSF工作簿();
工作表=工作簿。创建工作表(“工作表1”);
CellStyle CellStyle=workbook.createCellStyle();
//cellStyle.setAlignment(cellStyle.ALIGN_CENTER);//旧的apache poi版本
cellStyle.setAlignment(HorizontalAlignment.CENTER);//当前ApachePOI版本4.1.0
//cellStyle.setVerticalAlignment(cellStyle.VERTICAL_CENTER);//旧的apache poi版本
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//当前apache poi版本4.1.0
细胞;
对于(int i=0;i<3;i++){
cell=sheet.createRow(i).createCell(0);
cell.setCellValue(1+i);
cell.setCellStyle(cellStyle);
}
SheetConditionalFormatting sheetCF=sheet.getSheetConditionalFormatting();

ConditionalFormattingRule=sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_交通灯); rule.getMultilateFormat().setIconly(true); 条件格式规则[]cfRules={rule}; CellRangeAddress[]区域={CellRangeAddress.valueOf(“A1:A3”)}; 表CF.addConditionalFormatting(地区、cfRules); FileOutputStream fileOut=新的FileOutputStream(“ConditionalFormattingConset.xlsx”); 工作簿。写入(归档); fileOut.close(); }捕获(IOException ioex){ } } }

如下所示:

如果形状应仅在一个单元内,则锚点也必须仅为一个单元。然后使用
Dx
Dy
进行定位。但测量单位是特殊的。它是
EMU
英制公制单位

因此,单元格
A1
中椭圆的定位如下:

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

import org.apache.poi.util.Units;

import java.io.FileOutputStream;
import java.io.IOException;


class CenterShapeInCell {

 public static void main(String[] args) {
  try {

   Workbook workbook = new XSSFWorkbook();
   Sheet sheet = workbook.createSheet("Sheet1");

   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);
   row.setHeight((short)(20*20));
   sheet.setColumnWidth(0, 20*256);

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

   ClientAnchor anchor = helper.createClientAnchor();

   //set anchor to A1 only
   anchor.setCol1(0);
   anchor.setRow1(0); 
   anchor.setCol2(0);
   anchor.setRow2(0); 

   //get the cell width of A1
   float cellWidthPx = sheet.getColumnWidthInPixels(0);
System.out.println(cellWidthPx);

   //set wanted shape size
   int shapeWidthPx = 20;
   int shapeHeightPx = 20;

   //calculate the position of left upper edge
   float centerPosPx = cellWidthPx/2f - (float)shapeWidthPx/2f;
System.out.println(centerPosPx);

   //set the position of left edge as Dx1 in unit EMU
   anchor.setDx1(Math.round(centerPosPx * Units.EMU_PER_PIXEL));

   //set the position of right edge as Dx2 in unit EMU
   anchor.setDx2(Math.round((centerPosPx + shapeWidthPx) * Units.EMU_PER_PIXEL));

   //set upper padding
   int upperPaddingPx = 4;

   //set upper padding as Dy1 in unit EMU
   anchor.setDy1(upperPaddingPx * Units.EMU_PER_PIXEL);

   //set upper padding + shape height as Dy2 in unit EMU
   anchor.setDy2((upperPaddingPx + shapeHeightPx) * Units.EMU_PER_PIXEL);

   XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
   shape.setShapeType(ShapeTypes.ELLIPSE);
   shape.setFillColor(255, 0, 0);


   FileOutputStream fileOut = new FileOutputStream("CenterShapeInCell.xlsx");
   workbook.write(fileOut);
   fileOut.close();

  } catch (IOException ioex) {
  }
 }
}
看起来像:


但我怀疑你真正想要的是一种带有交通灯符号的条件格式,比如:

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

import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.io.IOException;

class ConditionalFormattingIconSet {

 public static void main(String[] args) {
  try {

   Workbook workbook = new XSSFWorkbook();

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

   CellStyle cellStyle = workbook.createCellStyle();
   //cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // old apache poi versions
   cellStyle.setAlignment(HorizontalAlignment.CENTER); // current apache poi version 4.1.0
   //cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // old apache poi versions
   cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // current apache poi version 4.1.0

   Cell cell;

   for (int i = 0; i < 3; i++) {
    cell = sheet.createRow(i).createCell(0);
    cell.setCellValue(1+i);
    cell.setCellStyle(cellStyle);
   }

   SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

   ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_TRAFFIC_LIGHTS);

   rule.getMultiStateFormatting().setIconOnly(true);

   ConditionalFormattingRule [] cfRules = {rule};

   CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A3")};

   sheetCF.addConditionalFormatting(regions, cfRules);

   FileOutputStream fileOut = new FileOutputStream("ConditionalFormattingIconSet.xlsx");
   workbook.write(fileOut);
   fileOut.close();

  } catch (IOException ioex) {
  }
 }
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.CellRangeAddress;
导入java.io.FileOutputStream;
导入java.io.IOException;
类条件格式Conset{
公共静态void main(字符串[]args){
试一试{
工作簿=新的XSSF工作簿();
工作表=工作簿。创建工作表(“工作表1”);
CellStyle CellStyle=workbook.createCellStyle();
//cellStyle.setAlignment(cellStyle.ALIGN_CENTER);//旧的apache poi版本
cellStyle.setAlignment(HorizontalAlignment.CENTER);//当前ApachePOI版本4.1.0
//cellStyle.setVerticalAlignment(cellStyle.VERTICAL_CENTER);//旧的apache poi版本
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//当前apache poi版本4.1.0
细胞;
对于(int i=0;i<3;i++){
cell=sheet.createRow(i).createCell(0);
cell.setCellValue(1+i);
cell.setCellStyle(cellStyle);
}
SheetConditionalFormatting sheetCF=sheet.getSheetConditionalFormatting();

ConditionalFormattingRule=sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR_3_交通灯); rule.getMultilateFormat().setIconly(true); 条件格式规则[]cfRules={rule}; CellRangeAddress[]区域={CellRangeAddress.valueOf(“A1:A3”)}; 表CF.addConditionalFormatting(地区、cfRules); FileOutputStream fileOut=新的FileOutputStream(“ConditionalFormattingConset.xlsx”); 工作簿。写入(归档); fileOut.close(); }捕获(IOException ioex){ } } }

看起来:

交通灯符号就是我要找的。据我所知,ConditionalFormattingRule将包含绿色、黄色和红色圆圈的规则。Axel您使用的是哪种poi版本?对于poi 3.14,您列出的函数调用不可用。我正在使用poi-3.14,我已经对其进行了测试。请准确一点。哪些函数调用对您不可用?ConditionalFormattingRule=sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR\u 3\u TRAFFIC\u LIGHTS);我拥有的库没有任何设置了图标的参数。他们以字符串作为输入,请从红绿灯符号下载是我要找的。据我所知,ConditionalFormattingRule将包含绿色、黄色和红色圆圈的规则。Axel您使用的是哪种poi版本?对于poi 3.14,您列出的函数调用不可用。我正在使用poi-3.14,我已经对其进行了测试。请准确一点。哪些函数调用对您不可用?ConditionalFormattingRule=sheetCF.createConditionalFormattingRule(IconMultiStateFormatting.IconSet.GYR\u 3\u TRAFFIC\u LIGHTS);我拥有的库没有任何设置了图标的参数。他们以字符串作为输入,请从