2016-08-04 19 views
0

この質問はよく似ているかもしれないが、私は別の問題があると知っている。私は円や形を作る方法を知っています。これは私がPOIで小さな円を作成

enter image description here

にExcelのセル

私はなどのチュートリアルを見て、作ることができる午前円の中心にある小さな円を作成している何をしたいです:

enter image description here

これは、私が作成するために使用していたコードです:

 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と関係があると思いますが、そこにある値を設定しても効果はありません。

私は形状が一つのセルの中だけにしてアンカーはまた、唯一つのセルでなければならないものとした場合は、何らかの形

答えて

2

その形状を小さくする必要があります。位置決めはDxDyで実行されます。しかし、測定単位は特別です。それは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) { 
    } 
} 
} 

は、次のようになります。enter image description here


しかし、私は、あなたが本当にしたいことは、トラフィックの光で条件付き書式である疑いがありますそのような記号:

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); 
    cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 

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

    } catch (IOException ioex) { 
    } 
} 
} 

L ooksのような:enter image description here

+0

私は探していたものだった。 ConditionalFormattingRuleには、緑色、黄色、赤色の円の規則が含まれています。 –

+0

poiバージョンのAxelをお使いですか? poi 3.14では、あなたがリストした関数呼び出しは利用できません。 –

+0

poi-3.14を使用しています。私はそれをテストしました。正確にしてください。どのような関数呼び出しが利用できないのですか? –

関連する問題