// 第一步,创建一个HSSFWorkbook,对应一个Excel文件 if(wb == null){ wb = new HSSFWorkbook(); } // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); //设置司机下拉框 String[] costPerformObjects = driverList.toArray(new String[driverList.size()]); //创建一个数组 用来存放 我们取出来的数据 //遍历每个peoduct对象,来获取productName属性并添加到数组中 for (int i = 0; i < driverList.size(); i++) { String product = driverList.get(i); costPerformObjects[i] = product; } //将下拉框数据放到新的sheet里,然后excle通过新的sheet数据加载下拉框数据 Sheet hidden = wb.createSheet("hidden"); //创建单元格对象 Cell cell1 = null; //遍历我们上面的数组,将数据取出来放到新sheet的单元格中 for (int i = 0, length = costPerformObjects.length; i < length; i++){ //取出数组中的每个元素 String name = costPerformObjects[i]; //根据i创建相应的行对象(说明我们将会把每个元素单独放一行) Row row = hidden.createRow(i); //创建每一行中的第一个单元格 cell1 = row.createCell(0); //然后将数组中的元素赋值给这个单元格 cell1.setCellValue(name); } // 创建名称,可被其他单元格引用 Name namedCell = wb.createName(); namedCell.setNameName("hidden"); // 设置名称引用的公式 // 1、使用像’A1:B1’这样的相对值会导致在Microsoft Excel中使用工作簿时名称所指向的单元格的意外移动,通常使用绝对引用,例如"$A$1:$B$1"可以避免这种情况。 // 2、参照单元格,行偏移量,列偏移量 下面代码就是:选取行为A列为1 到 行为A列为productNameArray.length的数据 // 3、比如productNameArray.length为10 也就是 选取行为A列为1 到 行为A列为10 的数据(也就取到了10个单元格的数据) // 4、比如"hidden!$A$1:$B$10"也就是 选取行为A列为1 到 行为B列为10 的数据 (也就是选取到了20个单元格的数据) namedCell.setRefersToFormula("hidden!$A$1:$A$" + costPerformObjects.length); //加载数据,将名称为hidden的sheet中的数据转换为List形式 DVConstraint constraint = DVConstraint.createFormulaListConstraint("hidden"); // 设置第一列的3-65534行为下拉列表 // (3, 65534, 0, 0) ====> (起始行,结束行,起始列,结束列) CellRangeAddressList regions = new CellRangeAddressList(1, 10000, 1, 1); // 将设置下拉选的位置和数据的对应关系 绑定到一起 DataValidation dataValidation = new HSSFDataValidation(regions, constraint); //将第二个sheet设置为隐藏 wb.setSheetHidden(1, true); //将数据赋给下拉列表 sheet.addValidationData(dataValidation); // (3, 65534, 0, 0) ====> (起始行,结束行,起始列,结束列) /* CellRangeAddressList costPerformRegions = new CellRangeAddressList(1,100000,1,1); DVConstraint costPerformConstraint = DVConstraint.createExplicitListConstraint(costPerformObjects); HSSFDataValidation costPerformDataValidation = new HSSFDataValidation(costPerformRegions,costPerformConstraint); costPerformDataValidation.setShowErrorBox(true); sheet.addValidationData(costPerformDataValidation);*/ //设置加油卡下拉框 String[] oilCardObjects = oilCardList.toArray(new String[oilCardList.size()]); //创建一个数组 用来存放 我们取出来的数据 //遍历每个peoduct对象,来获取productName属性并添加到数组中 for (int i = 0; i < oilCardList.size(); i++) { String product = oilCardList.get(i); oilCardObjects[i] = product; } Sheet hiddenOilCar = wb.createSheet("hiddenOilCar"); Cell cell2 = null; //遍历我们上面的数组,将数据取出来放到新sheet的单元格中 for (int i = 0, length = oilCardObjects.length; i < length; i++){ //取出数组中的每个元素 String name = oilCardObjects[i]; //根据i创建相应的行对象(说明我们将会把每个元素单独放一行) Row row = hiddenOilCar.createRow(i); //创建每一行中的第一个单元格 cell2 = row.createCell(0); //然后将数组中的元素赋值给这个单元格 cell2.setCellValue(name); } // 创建名称,可被其他单元格引用 Name namedCell2 = wb.createName(); namedCell2.setNameName("hiddenOilCar"); namedCell2.setRefersToFormula("hiddenOilCar!$A$1:$A$" + oilCardObjects.length); DVConstraint constraint2 = DVConstraint.createFormulaListConstraint("hiddenOilCar"); CellRangeAddressList regions2 = new CellRangeAddressList(1, 10000, 3, 3); DataValidation dataValidation2 = new HSSFDataValidation(regions2, constraint2); wb.setSheetHidden(2, true); sheet.addValidationData(dataValidation2); /* String[] oilCardObjects = oilCardList.toArray(new String[oilCardList.size()]); CellRangeAddressList oilCardRegions = new CellRangeAddressList(1,100000,3,3); DVConstraint oilCardConstraint = DVConstraint.createExplicitListConstraint(oilCardObjects); HSSFDataValidation oilCardDataValidation = new HSSFDataValidation(oilCardRegions,oilCardConstraint); oilCardDataValidation.setShowErrorBox(true); sheet.addValidationData(oilCardDataValidation);*/ // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 HSSFRow row = sheet.createRow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 //设置边框 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //设置背景色 style.setFillForegroundColor((short) 13);// 设置背景色 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //声明列对象 HSSFCell cell = null; HSSFCellStyle cellStyle = wb.createCellStyle(); sheet.setDefaultColumnStyle(4,cellStyle); //创建标题 for(int i=0;i<title.length;i++){ cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); }