apache POI:整列(标题行除外)的dataValidation(或样式)?

时间:2019-01-16 21:26:28

标签: apache-poi

对于CellRange,我们可以同时为开始/结束行参数传递-1以应用样式和dataValidators to the entire column

但是如何跳过标题?

理想的解决方案是使用“ A1:A $”创建的CellRangeAddressList,但是它只有int构造函数。

我尝试假设-1是一个特殊值,这意味着有些特殊,但是CellRangeAddressList(1, -1, ...)失败,并出现“开始行>结束行”错误。然后,我还尝试假设-1表示最后一个单元格,但从最后一个到1个CellRangeAddressList(-1, 1, ...)导致未选择任何单元格。

最后,我尝试从CellRangeAddressList(-1, -1, ...)中删除第一行,但是据我从docs所知,无法在创建后操纵范围。

1 个答案:

答案 0 :(得分:1)

为除第一行之外的整个列创建一个CellRangeAddress,这意味着CellRangeAddress从第2行开始,直到最大行数。这取决于SpreadsheetVersion。在EXCEL2007中,最大行数是2 ^ 20 =1048576。在EXCEL97中,最大行数是2 ^ 16 = 65536。

使用SpreadsheetVersion,我们可以得出取决于SpreadsheetVersion的不同最大行数。

示例:

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.SpreadsheetVersion;

class CreateCellRangeAddressList {

 public static void main(String[] args) throws Exception {

  //Workbook workbook = new XSSFWorkbook(); 
  Workbook workbook = new HSSFWorkbook(); 

// ...

  int lastRow = workbook.getSpreadsheetVersion().getLastRowIndex();

  CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(
   1, // row 2
   lastRow, 
   2, // column C
   2);

System.out.println(cellRangeAddressList.getCellRangeAddress(0)); 
//C2:C1048576 or C2:C65536 dependent on SpreadsheetVersion

// ...

 }
}

因为问题是关于除第一行外的整个列的数据验证,所以我们举一个例子。

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;

class CreateExcelDataValidationListsWholeColumn {

 public static void main(String[] args) throws Exception {

  //Workbook workbook = new HSSFWorkbook();
  Workbook workbook = new XSSFWorkbook();

  Sheet sheet = workbook.createSheet("Sheet1");

  sheet.createRow(0).createCell(1).setCellValue("col2Head");

  //data validation in column B, except first row:
  DataValidationHelper dvHelper = sheet.getDataValidationHelper();
  DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(new String[]{"X", "Y"}) ;
  int lastRow = workbook.getSpreadsheetVersion().getLastRowIndex();
  CellRangeAddressList addressList = new CellRangeAddressList(1, lastRow, 1, 1); //B2:B1048576      
  DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
  sheet.addValidationData(validation); // data validation for B2:B1048576 

  FileOutputStream out = null;
  if (workbook instanceof HSSFWorkbook) {
   out = new FileOutputStream("CreateExcelDataValidationListsWholeColumn.xls");
  } else if (workbook instanceof XSSFWorkbook) {
   out = new FileOutputStream("CreateExcelDataValidationListsWholeColumn.xlsx");
  }
  workbook.write(out);
  workbook.close();
  out.close();

 }
}

这将导致工作表XML如下:

<worksheet>
 <dimension ref="B1"/>
 <sheetViews>
  <sheetView workbookViewId="0" tabSelected="true"/>
 </sheetViews>
 <sheetFormatPr defaultRowHeight="15.0"/>
 <sheetData>
  <row r="1"><c r="B1" t="s"><v>0</v></c></row>
 </sheetData>
 <dataValidations count="1">
  <dataValidation type="list" sqref="B2:B1048576" allowBlank="true" errorStyle="stop">
    <formula1>"X,Y"</formula1>
  </dataValidation>
 </dataValidations>
 <pageMargins bottom="0.75" footer="0.3" header="0.3" left="0.7" right="0.7" top="0.75"/>
</worksheet>

使用HSSFWorkbook所得的CreateExcelDataValidationListsWholeColumn.xls大小为4 KB。