无法在XSSF表中添加验证数据

时间:2018-02-28 10:05:28

标签: java apache-poi xssf

我正在尝试编写一个简单的程序,在xlsm文件中的XSSFSheet中添加下拉框。我写了以下代码

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class XSSFDropDownTrial {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String fileName = "C:\\Ishan\\Test.xlsm";
    String fileName2 = "C:\\Ishan\\Test_Feb28.xlsm";
    FileOutputStream fout = null;
    FileInputStream fin = null;
     XSSFWorkbook workbook=null;
     XSSFCreationHelper createHelper = null;
     String[] dropDownList =  new String[5];
     dropDownList[0]="1";
     dropDownList[1]="2"; dropDownList[2]="3";
     dropDownList[3]="4"; dropDownList[4]="5";

    try {
        fin = new FileInputStream(new File(fileName));
        workbook = new XSSFWorkbook(OPCPackage.open(fin)
        );

//              createHelper = workbook.getCreationHelper();
           String [] listOfValues = new String [3];
           listOfValues[0] = "Ishan";
           listOfValues[1] = "IshanA";
           listOfValues[2] = "IshanB";
           CellRangeAddressList addressList = new CellRangeAddressList();
            addressList.addCellRangeAddress(1, 4, 100, 4);

            XSSFSheet sheet = workbook.createSheet("Data Validation");
            XSSFDataValidationHelper dvHelper = new 
            XSSFDataValidationHelper(sheet);
        XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)dvHelper.createExplicitListConstraint(listOfValues);
        XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(dvConstraint, addressList);
        validation.setShowErrorBox(true);
        validation.setSuppressDropDownArrow(true);
        sheet.addValidationData(validation);

             fout = new FileOutputStream(new File(fileName2));
            workbook.write(fout);
            fout.close();
            System.out.println("xlsm created successfully..");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(fout!=null){
                    fout.flush();
                    fout.close();
                    fout = null;
                }
                if(workbook!=null){
                    workbook = null;
                }
                if(fin!=null){
                    fin.close();
                    fin = null;
                }
                }catch(Exception e){
                    e.getMessage();
                }
            }

}

}

但它在错误后给出错误

  

对于XSSFSheet类型,未定义方法addValidationData(XSSFDataValidation)

在以下代码行

  

sheet.addValidationData(验证);

我不确定为什么会这样。我正在使用 apache-poi-3.8-20120326 JARs

但问题是,同一项目中还有一个java文件具有相同的代码,但该代码中没有问题。我把那段代码也作为参考

    package com;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;


    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.ss.util.CellRangeAddressList;
    import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
    import org.apache.poi.xssf.usermodel.XSSFDataValidation;
    import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
    import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;

    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;


    public class DemoXLSM {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String fileName = "C:\\Ishan\\Test.xlsm";
            String fileName2 = "C:\\Ishan\\Test_Feb28.xlsm";
            FileOutputStream fout = null;
            FileInputStream fin = null;
             XSSFWorkbook workbook=null;
             XSSFCreationHelper createHelper = null;
             String[] dropDownList =  new String[5];
             dropDownList[0]="1";
             dropDownList[1]="2"; dropDownList[2]="3";
             dropDownList[3]="4"; dropDownList[4]="5";

            try {
                fin = new FileInputStream(new File(fileName));
                workbook = new XSSFWorkbook(OPCPackage.open(fin)
                );


                // dropdown
                String [] listOfValues = new String [3];
                   listOfValues[0] = "Ishan";
                   listOfValues[1] = "IshanA";
                   listOfValues[2] = "IshanB";
                   CellRangeAddressList addressList = new CellRangeAddressList();
                    addressList.addCellRangeAddress(1, 4, 100, 4);

                XSSFSheet sheet = workbook.createSheet("Data Validation");
                XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
                XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)dvHelper.createExplicitListConstraint(listOfValues);
                XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(dvConstraint, addressList);
                validation.setShowErrorBox(true);
                validation.setSuppressDropDownArrow(true);
                sheet.addValidationData(validation);
                // dropdown


                 fout = new FileOutputStream(new File(fileName2));
                workbook.write(fout);
                fout.close();
                System.out.println("xlsm created successfully..");

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try{
                    if(fout!=null){
                        fout.flush();
                        fout.close();
                        fout = null;
                    }
                    if(workbook!=null){
                        workbook = null;
                    }
                    if(fin!=null){
                        fin.close();
                        fin = null;
                    }
                    }catch(Exception e){
                        e.getMessage();
                    }
                }
        }

    }

此代码中没有错误。

0 个答案:

没有答案