import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public boolean createExcel( Vector<Vector<Object>> resultList){ boolean flag=false; try { // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); HSSFFont font = workbook.createFont(); font.setColor(HSSFFont.COLOR_RED); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFont(font); HSSFSheet sheet = workbook.createSheet(); workbook.setSheetName(0, "BGA", HSSFWorkbook.ENCODING_UTF_16); // 在索引0的位置创建行(最顶端的行) HSSFRow row = sheet.createRow((short) 0); // 在索引0的位置创建单元格(左上端) HSSFCell cell = row.createCell((short) 0); //cell.setCellStyle(cellStyle);// 设置单元格格式 cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置UTF-16编码 cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 定义单元格为字符串类型 cell.setCellValue("SFR");// 在单元格中输入一些内容 生成标题栏 cell = row.createCell((short) 1); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("申请时间"); cell = row.createCell((short) 2); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("当前状态"); cell = row.createCell((short) 3); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("申请人"); cell = row.createCell((short) 4); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("Location"); cell = row.createCell((short) 5); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("Rework Type"); cell = row.createCell((short) 6); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("www.gbsou.com"); cell = row.createCell((short) 7); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("Fail Symptom"); cell = row.createCell((short) 8); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("Chip Date Code"); cell = row.createCell((short) 9); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("BGA站维修结果"); cell = row.createCell((short) 10); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("BGA站完成时间"); cell = row.createCell((short) 11); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("工程师确认结果"); cell = row.createCell((short) 12); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("工程师确认时间"); cell = row.createCell((short) 13); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("www.gbsou.com"); cell = row.createCell((short) 14); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("FA确认时间"); Vector<Vector<Object>> list = resultList; int crNum = 0; if (list != null) { Iterator searchList = list.iterator(); while (searchList.hasNext()) { Vector<Object> bgaVector = (Vector<Object>)searchList.next(); crNum += 1; row = sheet.createRow((short) crNum); for(int i=0;i<15;i++){ cell = row.createCell((short) i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(bgaVector.get(i)!=null?bgaVector.get(i).toString():""); } } } String fileName ="D:BGASearch_"+new Date().getTime()+".xls"; FileOutputStream fOut = new FileOutputStream(fileName); // 把相应的Excel工作簿存盘 workbook.write(fOut); fOut.flush(); fOut.close(); flag=true; } catch (Exception e) { e.printStackTrace(); System.out.println("已运行 xlCreate()发生异常 : " + e); flag=false; } return flag; }