あなたが提案したように、私はあなたのリンクを使用してxlsxの例を作成し、必要なxml構造を単純に再作成しました。つまり、xlsxファイルをzipアーカイブとして開き、xl/worksheets/sheet1.xml
. poi-ooxml.jar のほかに、ooxml-schemas-1.1.jar が必要です。
(Libre Office 4.0、Excel Viewer 2010、POI 3.10-beta1 でテスト済み)
import java.io.FileOutputStream;
import java.lang.reflect.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
public class Databar {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
for (int i=0; i<4; i++) {
sheet.createRow(i).createCell(0).setCellValue(new int[]{12,38,93,42}[i]);
}
SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule xcfrule =
(XSSFConditionalFormattingRule)cf.createConditionalFormattingRule("");
Method m = XSSFConditionalFormattingRule.class.getDeclaredMethod("getCTCfRule");
m.setAccessible(true);
CTCfRule cfRule = (CTCfRule)m.invoke(xcfrule);
cfRule.removeFormula(0); // cleanup
cfRule.setType(STCfType.DATA_BAR);
CTDataBar databar = cfRule.addNewDataBar();
CTCfvo vfoMin = databar.addNewCfvo();
vfoMin.setType(STCfvoType.NUM);
vfoMin.setVal("0");
CTCfvo vfoMax = databar.addNewCfvo();
vfoMax.setType(STCfvoType.NUM);
vfoMax.setVal("100");
CTColor color = databar.addNewColor();
color.setRgb(new byte[]{(byte)0xFF, 0x00, 0x00, (byte)0xFF});
CellRangeAddress cra[] = {new CellRangeAddress(0, 3, 0, 0)};
cf.addConditionalFormatting(cra, xcfrule);
FileOutputStream fos = new FileOutputStream("databar-out.xlsx");
wb.write(fos);
fos.close();
}
}