如何使用apache poi在单元格内创build一个进度条?

我想在Excel表单元格内创build一个进度条。 我必须使用Apache Poi库,但我不知道如何启动。 (就像这样,但使用Java库) http://www.tech-recipes.com/rx/35064/excel-2013-create-progress-bars/

我想我必须把有条件的格式化,但我知道它是如何工作的,我找不到任何地方的解决scheme…有人可以帮我吗?

提前致谢。

正如你所build议的,我已经使用你的链接来创build一个xlsx的例子,并简单地重新创build必要的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进行testing)

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