如何使用Apache POI的单元格中创建一个进度条? [英] How to create a Progress Bar within a cell using apache poi?

查看:828
本文介绍了如何使用Apache POI的单元格中创建一个进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Excel工作表单元格中的进度条。我必须使用Apache浦二库,但我不知道如何甚至开始。 (这样的事情,但使用的Java库) HTTP ://www.tech-recipes.com/rx/35064/excel-2013-create-progress-bars/

I would like to create a progress bar within a Excel-sheet cell. I must use Apache Poi library, but I do not know how to even start. (Something like this, but using the Java library) http://www.tech-recipes.com/rx/35064/excel-2013-create-progress-bars/

我想我必须把有条件的格式化,但我知道它是如何工作,我无法找到一个解决办法的任何地方......有人能帮助我吗?

I guess I must put a conditional formating, but I do know how it works and I can not find a solution anywhere ... somebody can help me out?

先谢谢了。

推荐答案

如你所说,我用你的链接,以创建一个示例XLSX,简单地重新创建必要的XML结构,即打开XLSX文件为zip压缩包,并有看看 XL /工作表/ sheet1.xml 。旁边的POI-ooxml.jar你需要的OOXML-模式-1.1.jar。

As you suggested, I've used your link to create an example xlsx and simply recreated the necessary xml structures, i.e. open the xlsx file as zip archive and have a look at xl/worksheets/sheet1.xml. Beside the poi-ooxml.jar you'll need the ooxml-schemas-1.1.jar.

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

这篇关于如何使用Apache POI的单元格中创建一个进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆