poi 데이터 유효성 (아래 목록)

1996 단어 드롭다운 목록
버전8
public class Test {

	public static void main(String[] args) {
			FileInputStream in = null;
		    String outputPath = null;
			try {
				//  Workbook
				in = new FileInputStream("c:/temp.xlsx");
				Workbook workbook = WorkbookFactory.create(in);
				Sheet sheet =workbook.getSheetAt(1);
				Row row = sheet.getRow(3);
				Cell cell = row.getCell(7);
				String[] textList = { "  1", "  2" };
			    sheet.addValidationData(setDataValidation(sheet,textList, 3,3, 7, 7));    
			    cell.setCellValue("  1");
				
				FileOutputStream out = new FileOutputStream("c:/aaa.xlsx");
				workbook.write(out);
				out.close();
		
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				try {
					if(in != null){
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}
	
	
	public static DataValidation setDataValidation(Sheet sheet,String[] textList, int firstRow, int endRow, int firstCol, int endCol) {  
	  
		DataValidationHelper helper = sheet.getDataValidationHelper();
		//           
		DataValidationConstraint constraint = helper.createExplicitListConstraint(textList);  
	    // DVConstraint constraint = new DVConstraint();  
	    constraint.setExplicitListValues(textList);  
	  
	    //                 。  
	    //        :   、   、   、     
	    CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol);  
	  
	    //          
	    DataValidation data_validation = helper.createValidation(constraint, regions);
	    //DataValidation data_validation = new DataValidation(regions, constraint);  
	  
	    return data_validation;  
	} 

좋은 웹페이지 즐겨찾기