maven项目引入包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
测试了一下,删除poi-ooxml-schemas包程序依然可用,时间问题,没有深究。
写个测试类:Test.java
import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * @author 田海超 * @E-mail tianhaichao@sinosoft.com.cn * @date 2019年5月13日 下午3:51:27 * @Description 类描述 */ public class Test { public static void main(String[] args) { try { String[] subjects = new String[] { "JAVA", "C++", "JS" }; createExcel("E:\test00.xlsx", subjects); } catch (Exception e) { e.printStackTrace(); } } public static void createExcel(String filePath, String[] subjects) throws Exception { // 生成文件流 FileOutputStream exportXlsStream = null; XSSFWorkbook workbook = null; File file = null; try { workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("test"); sheet.setDefaultColumnWidth((short) 15); // 生成一个样式 XSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置每列的列宽 sheet.setColumnWidth(0, 2600); sheet.setColumnWidth(1, 2600 * 2); sheet.setColumnWidth(2, 2600 * 2); sheet.setColumnWidth(3, 2600); sheet.setColumnWidth(4, 2600); XSSFCell cell = null; // 创建第一行(也就是标题行) XSSFRow row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("***帐号"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("*****"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("用户组code"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("职位"); cell.setCellStyle(style); // 对职位这一列的每一行添加下拉选项(从第一行加到第20000行) addValidation(sheet, subjects, 0, 200000, 4, 4); exportXlsStream = new FileOutputStream(filePath); // 将生成的workbook以流的方式写入文件 workbook.write(exportXlsStream); // 这里是创建临时文件,在开发中,我们可能不知道那个路径下有创建文件的权限,所以创建系统临时文件,但这样写也要记得return file后在finally中删除临时文件 // file = File.createTempFile(prefix, suffix); } catch (Throwable e) { System.out.println("这里打印异常日志提示,例如: 生成模版文件异常"); e.printStackTrace(); throw new Exception(); } finally { if (workbook != null) { workbook.close(); } if (exportXlsStream != null) { exportXlsStream.close(); } // if(file != null){ // file.deleteOnExit(); // } } } /** * @author 田海超 * @date 2019年5月21日 * @description : * @param sheet * @param subjects * 下拉选项 * @param firstRow * 第一个开始加下拉选项的行,从0开始,即第一行开始传入0 * @param endRow * 最后一个开始加下拉选项的行 * @param firstCol * 第一个开始加下拉选项的列,从0开始,即第一列开始传入0 * @param endCol * 最后一个开始加下拉选项的列 * @throws Exception * void */ public static void addValidation(XSSFSheet sheet, String[] subjects, int firstRow, int endRow, int firstCol, int endCol) throws Exception { DataValidationHelper helper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = helper.createExplicitListConstraint(subjects); CellRangeAddressList addressList = null; DataValidation dataValidation = null; // 设置数据有效性(下拉列)加载在哪个单元格上。四个参数分别是:起始行、终止行、起始列、终止列 addressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); dataValidation = helper.createValidation(constraint, addressList); sheet.addValidationData(dataValidation); addressList = null; dataValidation = null; } }
代码运行结果: