Poi solve the problem using the drop-down box in export excel enumeration more

Without further ado, directly on the code:

  1 package com.fst.attachment.controller;
  2 
  3 import java.io.FileOutputStream;
  4 
  5 import org.apache.poi.hssf.usermodel.DVConstraint;
  6 import org.apache.poi.hssf.usermodel.HSSFDataValidation;
  7 import org.apache.poi.hssf.usermodel.HSSFSheet;
  8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9 import org.apache.poi.ss.usermodel.DataValidation;
 10 import org.apache.poi.ss.usermodel.DataValidationConstraint;
 11 import org.apache.poi.ss.usermodel.DataValidationHelper;
 12 import org.apache.poi.ss.usermodel.Name;
 13 import org.apache.poi.ss.usermodel.Sheet;
 14 import org.apache.poi.ss.usermodel.Workbook;
 15 import org.apache.poi.ss.util.CellRangeAddressList;
 16 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 17 import org.apache.poi.xssf.usermodel.XSSFDataValidation;
 18 import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
 19 import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
 20 import org.apache.poi.xssf.usermodel.XSSFSheet;
21 is  Import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 22 is  
23 is  public  class TestPOIDropDown {
 24  
25      public  static  void main (String [] args) throws Exception {
 26 is          // Create enumeration 
27          int len = 200 is ;
 28          String [] = DATAS new new [len] String;
 29          for ( int I = 0; I <len; I ++ ) {
 30              DATAS [I] = I + "I --- enumeration drop down box" ;
 31          }
 32  
33 is         // ---------------- generation ------------------------------- -
 34 is          // method a: createExplicitListConstraint implemented using, defects:
 35          // only meet dropdown enumeration less, preferably not more than 20, particularly the number of field length may be enumerated.
36          // the Workbook Workbook = HSSFSetDropDown (DATAS);
 37 [          // the Workbook Workbook = XSSFSetDropDown (DATAS);
 38 is          // the Workbook Workbook = SXSSFSetDropDown (DATAS); 
39          / * 
40           * Comparative simple HSSF, XSSF, SXSSF: - Since the new XSSF supports Excel 2007 OOXML (.xlsx) files are XML-based,
 41           * and therefore treatment (.xls) files are binary memory footprint than legacy HSSF support. -
 42           (support after the 3.8-beta3) * SXSSF used when generating very large spreadsheets, compared to XSSF, it can only access a limited number of the line at some point.
43          * Http://poi.apache.org/components/spreadsheet/ 
44 is           * / 
45  
46 is          // two: createFormulaListConstraint implemented, which is suitable for enumeration of more drop-down box,
 47          // implementation steps substantially as follows: Create a hidden Sheet, enumeration and placed inside, and then increased in the first association Sheet 
48          the Workbook Workbook = XSSFSetDropDownAndHidden (DATAS);
 49  
50          // output 
51 is          a FileOutputStream Stream = new new a FileOutputStream ( "D: \\ testDropDown.xlsx " );
 52 is          workbook.write (Stream);
 53 is          stream.close ();
 54 is      }
 55      
56 is  
57 is      / **
58       * createFormulaListConstraint implemented using drop-down box
 59       * @param formulaString
 60       * @return 
61 is       * / 
62 is      public  static the Workbook XSSFSetDropDownAndHidden (String [] formulaString) {
 63 is          the Workbook Workbook = new new XSSFWorkbook ();
 64          Sheet Sheet = workbook.createSheet ( "drop-down list test " );
 65          // create the sheet, write enumeration 
66          Sheet hideSheet = workbook.createSheet (" hiddenSheet " );
 67          for ( int i = 0; i <formulaString.length; i ++) {
 68              hideSheet.createRow (I) .createCell (0 ) .setCellValue (formulaString [I]);
 69          }
 70          // create a name, reference may be other cells 
71 is          the Name category1Name = workbook.createName ();
 72          category1Name. setNameName ( "hidden" );
 73 is          // set the name of the referenced equation
 74          @ using as 'A1: B1' such relative movement result in unexpected values when using Microsoft Excel workbook in the name of the cell pointed to,
 75          // usually absolute references, e.g. '$ A $ 1: $ B $ 1' avoid this situation.
76          @ Reference: http://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/Name.html 
77         category1Name.setRefersToFormula ( "hiddenSheet!" + "$ A $. 1: $ A $" + formulaString.length);
 78          // Get the name of the data above 
79          DataValidationHelper Helper = sheet.getDataValidationHelper ();
 80          DataValidationConstraint constraint = helper.createFormulaListConstraint ( "hidden" );
 81          // set the position of the drop-down box 
82          CellRangeAddressList as addressList = new new CellRangeAddressList (0, 200 is, 0, 0 );
 83          dataValidation dataValidation = helper.createValidation (constraint, as addressList);
 84          // handle Excel compatibility 
85          IF(dataValidation the instanceof XSSFDataValidation) {
 86              // data check 
87              dataValidation.setSuppressDropDownArrow ( to true );
 88              dataValidation.setShowErrorBox ( to true );
 89          } the else {
 90              dataValidation.setSuppressDropDownArrow ( to false );
 91 is          }
 92          // act on the target sheet 
93          sheet.addValidationData (dataValidation);
 94          // set hiddenSheet hide 
95          workbook.setSheetHidden (. 1,to true );
 96          return Workbook;
 97      }
 98      
99  
100      / ** 
101       * HSSF older versions of user model table set down box defect: When the pull-down block data amount exceeds a certain amount, the system Throws.
102       * 
 103       * @param formulaString
 104       * 
 105       * / 
106      public  static the Workbook HSSFSetDropDown (String [] formulaString) {
 107          HSSFWorkbook Workbook = new new HSSFWorkbook ();
 108          HSSFSheet Sheet = workbook.createSheet ( "drop-down Test" );
 109          / / load the drop-down list content 
110         = Constraint DVConstraint DVConstraint.createExplicitListConstraint (formulaString);
 111          // setting data validity on which load cell.
112          // four parameters are: start line, end line, start column, end columns 
113          CellRangeAddressList Regions = new new CellRangeAddressList (0, 200 is, 0, 0 );
 114          // Data Validation objects 
115          DataValidation dataValidation = new new HSSFDataValidation (Regions, constraint);
 1 16          sheet.addValidationData (dataValidation);
 117          return Workbook;
 1 18      }
 119  
120      / ** 
121      * XSSF user model setting table using drop-down box, for processing multiple suffixes xlsx excel defect: When the pull-down block data amount exceeds a certain amount, the file open.
122       * 
 123       * @param formulaString
 124       * 
 125       * / 
126      public  static the Workbook XSSFSetDropDown (String [] formulaString) {
 127          XSSFWorkbook Workbook = new new XSSFWorkbook ();
 128          XSSFSheet Sheet = workbook.createSheet ( "drop-down Test" );
 129          XSSFDataValidationHelper = dvHelper new new XSSFDataValidationHelper (Sheet);
 130.          XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
131                 .createExplicitListConstraint(formulaString);
132         CellRangeAddressList addressList = null;
133         XSSFDataValidation validation = null;
134         for (int i = 0; i < 500; i++) {
135             addressList = new CellRangeAddressList(i, i, 0, 0);
136             validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
137             // 07默认setSuppressDropDownArrow(true);
138             //validation.setSuppressDropDownArrow (to true);
 139              // validation.setShowErrorBox (to true); 
140              sheet.addValidationData (Validation);
 141 is          }
 142          return Workbook;
 143      }
 144  
145      / ** 
146       * SXSSF user using drop-down box defect model setting table: when the drop-down box the amount of data exceeds a certain number of files open.
147       * 
 148       * @param formulaString
 149       * 
 150       * / 
151      public  static the Workbook SXSSFSetDropDown (String [] formulaString) {
 152          SXSSFWorkbook Workbook =new new SXSSFWorkbook ();
 153          Sheet Sheet = workbook.createSheet ( "drop-down Test" );
 154          // load the drop-down list content 
155          DataValidationHelper Helper = sheet.getDataValidationHelper ();
 156          DataValidationConstraint constraint = helper.createExplicitListConstraint (formulaString);
 157          / / set the position of the drop-down box 
158          CellRangeAddressList as addressList = null ;
 159          as addressList = new new CellRangeAddressList (0, 500, 0, 0 );
 160.          dataValidation dataValidation = helper.createValidation(constraint, addressList);
161         // 处理Excel兼容性问题
162         if (dataValidation instanceof XSSFDataValidation) {
163             // 数据校验
164             dataValidation.setSuppressDropDownArrow(true);
165             dataValidation.setShowErrorBox(true);
166         } else {
167             dataValidation.setSuppressDropDownArrow(false);
168         }
169         sheet.addValidationData(dataValidation);
170         return workbook;
171     }
172 
173 }

 


Reference
http://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/Name.html
http://poi.apache.org/components/spreadsheet/
last
reprint please indicate the source, Thank
---------------------
Disclaimer: This article is the original article CSDN bloggers "Fei___", following the CC 4.0 by-sa copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/fei565789229/article/details/85016091

Guess you like

Origin www.cnblogs.com/zouhao/p/11346243.html