java导出excel如何设置单元格样式为文本样式

[color=green][b][size=medium]//新增的四句话,设置CELL格式为文本格式
HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();
HSSFDataFormat format = demoWorkBook.createDataFormat();
cellStyle2.setDataFormat(format.getFormat("@"));
cell.setCellStyle(cellStyle2);
cell.setCellValue(cells.get(i));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);


//设置本列只能输入数字
sheet.createRow(0).createCell(0).setCellValue("本列只允许输入数字");
CellRangeAddressList regions = new CellRangeAddressList(1, 65535, 0, 0);//设置范围
DVConstraint constraint = DVConstraint.createNumericConstraint(DVConstraint.ValidationType.INTEGER, DVConstraint.OperatorType.BETWEEN, "0", "10");//设置格式
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
dataValidate.createErrorBox("error", "You must input a number between 0 to 10.");
sheet.addValidationData(dataValidate);


[/size][/b][/color]

你可能感兴趣的:(JAVA)