想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有:
poi-3.9.jar
poi-ooxml-3.9.jar
poi-ooxml-schemas-3.9.jar
数据库我这边用的是mysql数据库。
要在d盘目录下新建一个叫"data"的文件夹,生成的excel都会在这个文件夹中。
当然,我这边项目是用maven管理jar的:
这个例子excel中的数据是模拟产生的。如果想得到真实的数据,只需要从数据库中取出数据,然后对应的数组里面就ok了
上代码:
package com.imtdata05101800; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; 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.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.IndexedColors; public class ExportExcel { private String title;// 显示导出表的标题 private String[] rowName;// 导出表的列名 private List<Object[]> dataList = new ArrayList<Object[]>(); public ExportExcel(String title, String[] rowName, List<Object[]> dataList) { super(); this.title = title; this.rowName = rowName; this.dataList = dataList; } // 导出数据 public void export() throws Exception { try { HSSFWorkbook workbook = new HSSFWorkbook();// 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 产生表格标题行,第一行 HSSFRow rowm1 = sheet.createRow(0); HSSFCell cell1 = rowm1.createCell(0); // rowm1.setHeight((short) (25 * 30));//设置第一行的高度 // sheet样式定义[getcolumnTopStyle()/getStyle()均为自定义方法-在下面-可扩展] HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); HSSFCellStyle style = this.getStyle(workbook); // 定义所需要的列数 int columnNum = rowName.length; HSSFRow rowRowName1 = sheet.createRow(0); HSSFRow rowRowName2 = sheet.createRow(1); rowRowName1.setHeight((short) (15 * 15));// 设置第一行高度 rowRowName2.setHeight((short) (20 * 25));// 设置第二行高度 // 将列头设置到sheet单元格中 for (int n = 0; n < columnNum; n++) { // 设置第一行的值 HSSFCell cellRowName1 = rowRowName1.createCell(n); // 创建列头对应个数的单元格 cellRowName1.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型 HSSFRichTextString text1 = new HSSFRichTextString(rowName[n]); cellRowName1.setCellValue(text1); // 设置列头单元格的值 cellRowName1.setCellStyle(columnTopStyle); // 设置列头单元格样式 // 设置第二行的值 HSSFCell cellRowName2 = rowRowName2.createCell(n); // 创建列头对应个数的单元格 cellRowName2.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型 HSSFRichTextString text2 = new HSSFRichTextString(rowName[n]); cellRowName2.setCellValue(text2); // 设置列头单元格的值 cellRowName2.setCellStyle(columnTopStyle); // 设置列头单元格样式 } // 合并业务属性后设置合并单元格的值 HSSFRow rowywsx = sheet.getRow(0); HSSFCell cellywsx = rowywsx.getCell(6); cellywsx.setCellValue("业务属性"); // 合并技术属性后设置合并单元格的值 HSSFRow rowjssx = sheet.getRow(0); HSSFCell celljssx = rowjssx.getCell(11); celljssx.setCellValue("技术属性"); // 合并重复组后设置合并单元格的值 HSSFRow rowjcfz = sheet.getRow(0); HSSFCell celljcfz = rowjcfz.getCell(18); celljcfz.setCellValue("重复组"); // 将前三列合并 sheet.addMergedRegion(new CellRangeAddress(2, 6, 0, 0)); sheet.addMergedRegion(new CellRangeAddress(2, 6, 1, 1)); sheet.addMergedRegion(new CellRangeAddress(2, 6, 2, 2)); // 将第一行与第二行合并 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2)); sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3)); sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4)); sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5)); // 合并业务属性 sheet.addMergedRegion(new CellRangeAddress(0, 0, 6, 10)); // 合并技术属性 sheet.addMergedRegion(new CellRangeAddress(0, 0, 11, 16)); // 合并说明 sheet.addMergedRegion(new CellRangeAddress(0, 1, 17, 17)); // 合并重复组 sheet.addMergedRegion(new CellRangeAddress(0, 0, 18, 20)); // 将查询出的数据设置到sheet对应的单元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i);// 遍历每个对象 HSSFRow row = sheet.createRow(i + 2);// 创建所需的行数 row.setHeight((short) (25 * 35)); // 设置第三行开始的单元格高度 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; // 设置单元格的数据类型 cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING); if (!"".equals(obj[j]) && obj[j] != null) { cell.setCellValue(obj[j].toString()); // 设置单元格的值 } cell.setCellStyle(style); // 设置单元格样式 } } // 让列宽随着导出的列长自动适应 // for (int colNum = 0; colNum < columnNum; colNum++) { // int columnWidth = sheet.getColumnWidth(colNum) / 256; // for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { // HSSFRow currentRow; // // 当前行未被使用过 // if (sheet.getRow(rowNum) == null) { // currentRow = sheet.createRow(rowNum); // } else { // currentRow = sheet.getRow(rowNum); // } // if (currentRow.getCell(colNum) != null) { // HSSFCell currentCell = currentRow.getCell(colNum); // if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { // int length = currentCell.getStringCellValue().getBytes().length; // if (columnWidth < length) { // columnWidth = length; // } // } // } // } // if (colNum == 0) { // sheet.setColumnWidth(colNum, (columnWidth - 2) * 128); // } else { // sheet.setColumnWidth(colNum, (columnWidth + 4) * 256); // } // // } if (workbook != null) { try { FileOutputStream out = new FileOutputStream( "D:/data/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() + ".xls"); workbook.write(out); out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 列头单元格样式 */ public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 9); // 字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置字体名字 font.setFontName("Times New Roman"); // 设置样式; HSSFCellStyle style = workbook.createCellStyle(); // 设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); // 设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); // 设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); // 设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); // 在样式用应用设置的字体; style.setFont(font); // 设置自动换行; style.setWrapText(true); // 设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); // 设置单元格背景颜色 style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); return style; } /* * 列数据信息单元格样式 */ public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 9); // 字体加粗 // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置字体名字 font.setFontName("Times New Roman"); // 设置样式; HSSFCellStyle style = workbook.createCellStyle(); // 设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); // 设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); // 设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); // 设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); // 在样式用应用设置的字体; style.setFont(font); // 设置自动换行; style.setWrapText(true); // 设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM); return style; } }
package com.imtdata05101800; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ExcelGenerate { public static void main(String[] args) throws Exception { List<HashMap<String, Object>> listMap = new ArrayList<>(); HashMap<String, Object> dataMap1 = new HashMap<>(); // 第1行数据放入map集合 开始 dataMap1.put("serviceID", "服务编号1"); dataMap1.put("serviceName", "服务名称1"); dataMap1.put("fieldType", "字段类型1"); dataMap1.put("dataElementEncoding", "数据元编码1"); dataMap1.put("chineseName", "中文名称1"); dataMap1.put("englishName", "英文名称1"); dataMap1.put("typeOfData", "数据类型1"); dataMap1.put("jdataLength", "数据长度1"); dataMap1.put("jdataAccuracy", "数据精度1"); dataMap1.put("unit", "单位1"); dataMap1.put("businessRules", "业务规则1"); dataMap1.put("englishAbbreviations", "英文缩写1"); dataMap1.put("dataElementTypeField", "数据元类型域1"); dataMap1.put("dataStorageType", "数据存储类型1"); dataMap1.put("ydataLength", "数据长度1"); dataMap1.put("ydataAccuracy", "数据精度1"); dataMap1.put("dataFormat", "数据格式1"); dataMap1.put("instructions", "说明1"); dataMap1.put("isItRedundant", "是否重复1"); dataMap1.put("duplicateGroupStructrueTitle", "重复组结构体名1"); dataMap1.put("parentStructureName", "父结构体名1"); dataMap1.put("number", "221"); dataMap1.put("repeatNumber", "221"); // 第1行数据放入map集合结束 listMap.add(dataMap1);// 将第1个map集合放入listMap中 // 第2行数据放入map集合开始 HashMap<String, Object> dataMap2 = new HashMap<>(); dataMap2.put("serviceID", "服务编号2"); dataMap2.put("serviceName", "服务名称2"); dataMap2.put("fieldType", "字段类型2"); dataMap2.put("dataElementEncoding", "数据元编码2"); dataMap2.put("chineseName", "中文名称2"); dataMap2.put("englishName", "英文名称2"); dataMap2.put("typeOfData", "数据类型2"); dataMap2.put("jdataLength", "数据长度2"); dataMap2.put("jdataAccuracy", "数据精度2"); dataMap2.put("unit", "单位2"); dataMap2.put("businessRules", "业务规则2"); dataMap2.put("englishAbbreviations", "英文缩写2"); dataMap2.put("dataElementTypeField", "数据元类型域2"); dataMap2.put("dataStorageType", "数据存储类型2"); dataMap2.put("ydataLength", "数据长度2"); dataMap2.put("ydataAccuracy", "数据精度2"); dataMap2.put("dataFormat", "数据格式2"); dataMap2.put("instructions", "说明2"); dataMap2.put("isItRedundant", "是否重复2"); dataMap2.put("duplicateGroupStructrueTitle", "重复组结构体名2"); dataMap2.put("parentStructureName", "父结构体名2"); dataMap2.put("number", "222"); dataMap2.put("repeatNumber", "222"); // 第2行数据放入map集合结束 listMap.add(dataMap2);// 将第2个map集合放入listMap中 // 第3行数据放入map集合开始 HashMap<String, Object> dataMap3 = new HashMap<>(); dataMap3.put("serviceID", "服务编号3"); dataMap3.put("serviceName", "服务名称3"); dataMap3.put("fieldType", "字段类型3"); dataMap3.put("dataElementEncoding", "数据元编码3"); dataMap3.put("chineseName", "中文名称3"); dataMap3.put("englishName", "英文名称3"); dataMap3.put("typeOfData", "数据类型3"); dataMap3.put("jdataLength", "数据长度3"); dataMap3.put("jdataAccuracy", "数据精度3"); dataMap3.put("unit", "单位3"); dataMap3.put("businessRules", "业务规则3"); dataMap3.put("englishAbbreviations", "英文缩写3"); dataMap3.put("dataElementTypeField", "数据元类型域3"); dataMap3.put("dataStorageType", "数据存储类型3"); dataMap3.put("ydataLength", "数据长度3"); dataMap3.put("ydataAccuracy", "数据精度3"); dataMap3.put("dataFormat", "数据格式3"); dataMap3.put("instructions", "说明3"); dataMap3.put("isItRedundant", "是否重复3"); dataMap3.put("duplicateGroupStructrueTitle", "重复组结构体名3"); dataMap3.put("parentStructureName", "父结构体名3"); dataMap3.put("number", "223"); dataMap3.put("repeatNumber", "223"); // 第3行数据放入map集合结束 listMap.add(dataMap3);// 将第3个map集合放入listMap中 // 第4行数据放入map集合开始 HashMap<String, Object> dataMap4 = new HashMap<>(); dataMap4.put("serviceID", "服务编号4"); dataMap4.put("serviceName", "服务名称4"); dataMap4.put("fieldType", "字段类型4"); dataMap4.put("dataElementEncoding", "数据元编码4"); dataMap4.put("chineseName", "中文名称4"); dataMap4.put("englishName", "英文名称4"); dataMap4.put("typeOfData", "数据类型4"); dataMap4.put("jdataLength", "数据长度4"); dataMap4.put("jdataAccuracy", "数据精度4"); dataMap4.put("unit", "单位4"); dataMap4.put("businessRules", "业务规则4"); dataMap4.put("englishAbbreviations", "英文缩写4"); dataMap4.put("dataElementTypeField", "数据元类型域4"); dataMap4.put("dataStorageType", "数据存储类型4"); dataMap4.put("ydataLength", "数据长度4"); dataMap4.put("ydataAccuracy", "数据精度4"); dataMap4.put("dataFormat", "数据格式4"); dataMap4.put("instructions", "说明4"); dataMap4.put("isItRedundant", "是否重复4"); dataMap4.put("duplicateGroupStructrueTitle", "重复组结构体名4"); dataMap4.put("parentStructureName", "父结构体名4"); dataMap4.put("number", "224"); dataMap4.put("repeatNumber", "224"); // 第4行数据放入map集合结束 listMap.add(dataMap4);// 将第4个map集合放入listMap中 // 第5行数据放入map集合开始 HashMap<String, Object> dataMap5 = new HashMap<>(); dataMap5.put("serviceID", "服务编号4"); dataMap5.put("serviceName", "服务名称4"); dataMap5.put("fieldType", "字段类型4"); dataMap5.put("dataElementEncoding", "数据元编码4"); dataMap5.put("chineseName", "中文名称4"); dataMap5.put("englishName", "英文名称4"); dataMap5.put("typeOfData", "数据类型4"); dataMap5.put("jdataLength", "数据长度4"); dataMap5.put("jdataAccuracy", "数据精度4"); dataMap5.put("unit", "单位4"); dataMap5.put("businessRules", "业务规则4"); dataMap5.put("englishAbbreviations", "英文缩写4"); dataMap5.put("dataElementTypeField", "数据元类型域4"); dataMap5.put("dataStorageType", "数据存储类型4"); dataMap5.put("ydataLength", "数据长度4"); dataMap5.put("ydataAccuracy", "数据精度4"); dataMap5.put("dataFormat", "数据格式4"); dataMap5.put("instructions", "说明4"); dataMap5.put("isItRedundant", "是否重复4"); dataMap5.put("duplicateGroupStructrueTitle", "重复组结构体名4"); dataMap5.put("parentStructureName", "父结构体名4"); dataMap5.put("number", "224"); dataMap5.put("repeatNumber", "224"); // 第5行数据放入map集合结束 listMap.add(dataMap5);// 将第5个map集合放入listMap中 // } String title = "交易数据"; String[] rowsName = new String[] { "服务编号", "服务名称", "字段类型", "数据元编码", "中文名称", "英文名称", "数据类型", "数据长度", "数据精度", "单位", "业务规则", "英文缩写", "数据元类型域", "数据存储类型", "数据长度", "数据精度", "数据格式", "说明", "是否重复", "重复组结构体名", "父结构体名", "", "" }; List<Object[]> dataList = new ArrayList<Object[]>(); Object[] objs = null; for (int i = 0; i < listMap.size(); i++) { HashMap<String, Object> data = listMap.get(i);// 获取5次 objs = new Object[rowsName.length]; objs[0] = data.get("serviceID"); objs[1] = data.get("serviceName"); objs[2] = data.get("fieldType"); objs[3] = data.get("dataElementEncoding"); objs[4] = data.get("chineseName"); objs[5] = data.get("englishName"); objs[6] = data.get("typeOfData"); objs[7] = data.get("jdataLength"); objs[8] = data.get("jdataAccuracy"); objs[9] = data.get("unit"); objs[10] = data.get("businessRules"); objs[11] = data.get("englishAbbreviations"); objs[12] = data.get("dataElementTypeField"); objs[13] = data.get("dataStorageType"); objs[14] = data.get("ydataLength"); objs[15] = data.get("ydataAccuracy"); objs[16] = data.get("dataFormat"); objs[17] = data.get("instructions"); objs[18] = data.get("isItRedundant"); objs[19] = data.get("duplicateGroupStructrueTitle"); objs[20] = data.get("parentStructureName"); objs[21] = data.get("number"); objs[22] = data.get("repeatNumber"); dataList.add(objs); } ExportExcel ex = new ExportExcel(title, rowsName, dataList); ex.export(); } }
生成后的Excel样式: