Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在apache.poi中为数据库制作纯色_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何在apache.poi中为数据库制作纯色

Java 如何在apache.poi中为数据库制作纯色,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试创建一个带有进度条的工作表,它将表示一些进度 我正在使用这些库: org.apache.poi:poi:4.1.0 org.apache.poi:poiooxml:4.1.0 org.apache.poi:poi ooxml模式:4.1.0 我得到的只是一个带有渐变的进度条,但我需要一个带有纯色的进度条,而不是色阶。Office Open XML中定义的所有条件格式数据条都使用渐变色。甚至没有一个属性或属性可以改变这一点。以后的Excel版本正在使用来自命名空间x14=”的扩展http

我正在尝试创建一个带有进度条的工作表,它将表示一些进度

我正在使用这些库:

  • org.apache.poi:poi:4.1.0
  • org.apache.poi:poiooxml:4.1.0
  • org.apache.poi:poi ooxml模式:4.1.0

  • 我得到的只是一个带有渐变的进度条,但我需要一个带有纯色的进度条,而不是色阶。

    Office Open XML中定义的所有条件格式数据条都使用渐变色。甚至没有一个属性或属性可以改变这一点。以后的
    Excel
    版本正在使用来自命名空间
    x14=”的扩展http://schemas.microsoft.com/office/spreadsheetml/2009/9/main“
    。但这些并不是Ecma Office Open XML文件格式标准的一部分

    Apache-poi
    到目前为止只基于
    Ecma-Office-openxml文件格式标准
    。因此,在
    apachepoi
    中为条件格式数据栏制作纯色的唯一方法是从头开始创建扩展
    x14
    数据栏条件格式的
    XML

    完整示例:

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFDataBarFormatting;
    import org.apache.poi.xssf.usermodel.XSSFConditionalFormattingRule;
    
    import org.apache.poi.ss.util.CellRangeAddress;
    
    import java.io.FileOutputStream;
    
    import java.lang.reflect.Field;
    
    public class ConditionalFormattingDataBars {
    
     public static void applyDataBars(SheetConditionalFormatting sheetCF, String region, ExtendedColor color) throws Exception {
      CellRangeAddress[] regions = { CellRangeAddress.valueOf(region) };
      ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(color);
      DataBarFormatting dbf = rule.getDataBarFormatting();
      dbf.getMinThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.NUMBER);
      dbf.getMinThreshold().setValue(0d);
      dbf.getMaxThreshold().setRangeType(ConditionalFormattingThreshold.RangeType.NUMBER);
      dbf.getMaxThreshold().setValue(100d);
    
      dbf.setIconOnly(true);
    
      dbf.setWidthMin(0); //cannot work for XSSFDataBarFormatting, see https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l57
      dbf.setWidthMax(100); //cannot work for XSSFDataBarFormatting, see https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l64
    
      if (dbf instanceof XSSFDataBarFormatting) {
       Field _databar = XSSFDataBarFormatting.class.getDeclaredField("_databar");
       _databar.setAccessible(true);
       org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar ctDataBar =
        (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar)_databar.get(dbf);
       ctDataBar.setMinLength(0);
       ctDataBar.setMaxLength(100);
      }
    
      // use extension from x14 namespace to set data bars not using gradient color 
      if (rule instanceof XSSFConditionalFormattingRule) {
       Field _cfRule = XSSFConditionalFormattingRule.class.getDeclaredField("_cfRule");
       _cfRule.setAccessible(true);
       org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule ctRule =
        (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule)_cfRule.get(rule);
       org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtensionList extList =
        ctRule.addNewExtLst();
       org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtension ext = extList.addNewExt();
       String extXML = 
          "<x14:id"
        + " xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\">"
        + "{00000000-000E-0000-0000-000001000000}"
        + "</x14:id>";
       org.apache.xmlbeans.XmlObject xlmObject = org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
       ext.set(xlmObject);
       ext.setUri("{B025F937-C7B1-47D3-B67F-A62EFF666E3E}");
    
       Field _sh = XSSFConditionalFormattingRule.class.getDeclaredField("_sh");
       _sh.setAccessible(true);
       XSSFSheet sheet = (XSSFSheet)_sh.get(rule);
       extList = sheet.getCTWorksheet().addNewExtLst();
       ext = extList.addNewExt();
       extXML = 
          "<x14:conditionalFormattings xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\">"
        + "<x14:conditionalFormatting xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\">"
        + "<x14:cfRule type=\"dataBar\" id=\"{00000000-000E-0000-0000-000001000000}\">"
        + "<x14:dataBar minLength=\"" + 0 + "\" maxLength=\"" + 100 + "\" gradient=\"" + false + "\">"
        + "<x14:cfvo type=\"num\"><xm:f>" + 0 + "</xm:f></x14:cfvo>"
        + "<x14:cfvo type=\"num\"><xm:f>" + 100 + "</xm:f></x14:cfvo>"
        + "</x14:dataBar>"
        + "</x14:cfRule>"
        + "<xm:sqref>" + region + "</xm:sqref>"
        + "</x14:conditionalFormatting>"
        + "</x14:conditionalFormattings>";
       xlmObject = org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
       ext.set(xlmObject);
       ext.setUri("{78C0D931-6437-407d-A8EE-F0AAD7539E65}");
      }
    
      sheetCF.addConditionalFormatting(regions, rule);
     }
    
     public static void main(String[] args) throws Exception {
      Workbook workbook = new XSSFWorkbook();
    
      Sheet sheet = workbook.createSheet("new sheet");
    
      double[] list = new double[]{0d, 10d, 20d, 30d, 40d, 50d, 60d, 70d, 80d, 90d, 100d};
      for (int i = 0; i < list.length; i++) {
       sheet.createRow(i+1).createCell(0).setCellValue(0d);
       sheet.getRow(i+1).createCell(1).setCellValue(list[i]);
       sheet.getRow(i+1).createCell(2).setCellValue(100d);
      }
    
      SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
      ExtendedColor color = workbook.getCreationHelper().createExtendedColor();
      color.setARGBHex("FF80C279");
      applyDataBars(sheetCF, "B2:B12", color);
    
      sheet.setColumnWidth(1, 50*256);
    
      FileOutputStream out = new FileOutputStream("ConditionalFormattingDataBars.xlsx");
      workbook.write(out);
      out.close();
      workbook.close();
    
     }
    }
    
    import org.apache.poi.ss.usermodel.*;
    导入org.apache.poi.xssf.usermodel.xssf工作簿;
    导入org.apache.poi.xssf.usermodel.xssfheet;
    导入org.apache.poi.xssf.usermodel.XSSFDataBarFormatting;
    导入org.apache.poi.xssf.usermodel.xssfconditionFormattingRule;
    导入org.apache.poi.ss.util.CellRangeAddress;
    导入java.io.FileOutputStream;
    导入java.lang.reflect.Field;
    公共类条件格式数据库{
    公共静态void applyDataBars(SheetConditionalFormatting sheetCF、字符串区域、ExtendedColor)引发异常{
    CellRangeAddress[]regions={CellRangeAddress.valueOf(region)};
    ConditionalFormattingRule=sheetCF.createConditionalFormattingRule(颜色);
    DataBarFormatting dbf=rule.getDataBarFormatting();
    dbf.getMinThreshold().setRangeType(条件格式Reshold.RangeType.NUMBER);
    dbf.getMinThreshold().setValue(0d);
    dbf.getMaxThreshold().setRangeType(条件格式Reshold.RangeType.NUMBER);
    dbf.getMaxThreshold().setValue(100d);
    dbf.seticonly(真);
    dbf.setWidthMin(0);//无法用于XSSFDataBarFormatting,请参阅https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l57
    dbf.setWidthMax(100);//无法用于XSSFDataBarFormatting,请参阅https://svn.apache.org/viewvc/poi/tags/REL_4_0_1/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java?view=markup#l64
    if(dbf instanceof XSSFDataBarFormatting){
    字段_databar=XSSFDataBarFormatting.class.getDeclaredField(“_databar”);
    _databar.setAccessible(true);
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar CTDataBar=
    (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar)databar.get(dbf);
    ctDataBar.setMinLength(0);
    ctDataBar.setMaxLength(100);
    }
    //使用x14命名空间的扩展来设置不使用渐变颜色的数据栏
    if(XSSFConditionFormattingRule的规则实例){
    字段cfRule=XSSFConditionalFormattingRule.class.getDeclaredField(“cfRule”);
    _cfRule.setAccessible(true);
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule ctRule=
    (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule)\u cfRule.get(rule);
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtensionList extList=
    ctRule.addNewExtLst();
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtension ext=extList.addNewExt();
    字符串extXML=
    ""
    +“{00000000-000E-0000-0000-000001000000}”
    + "";
    org.apache.xmlbeans.XmlObject xlmObject=org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
    ext.set(xlmObject);
    ext.setUri({B025F937-C7B1-47D3-B67F-A62EF666E3E});
    字段_sh=XSSFConditionalFormattingRule.class.getDeclaredField(“_sh”);
    _sh.setAccessible(真);
    XSSFSheet sheet=(XSSFSheet)_sh.get(规则);
    extList=sheet.getctsheet().addNewExtLst();
    ext=extList.addNewExt();
    extXML=
    ""
    + ""
    + ""
    + ""
    + "" + 0 + ""
    + "" + 100 + ""
    + ""
    + ""
    +“”+区域+“”
    + ""
    + "";
    xlmObject=org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
    ext.set(xlmObject);
    ext.setUri({78C0D931-6437-407d-A8EE-F0AAD7539E65});
    }
    表单cf.添加条件格式(区域、规则);
    }
    公共静态void main(字符串[]args)引发异常{
    工作簿=新的XSSF工作簿();
    工作表=工作簿。创建工作表(“新工作表”);
    double[]list=新的double[]{0d、10d、20d、30d、40d、50d、60d、70d、80d、90d、100d};
    for(int i=0;i
    如果看不到您的代码,就很难发现您做错了什么!你说的是条件格式的数据条吗?如果是这样,那么
    apachepoi
    不支持您想要的。
    officeopenxml
    中定义的所有条件格式数据栏都使用渐变色。甚至没有一个属性或属性可以改变这一点。以后的
    Excel
    版本正在使用来自命名空间
    x14=”的扩展http://schemas.microsoft.com/office/spreadsheetml/2009/9/main“
    。但是这些不是Ecma Office Open XML文件格式标准的一部分,很抱歉,是代码