POI导出Excel三

xiaoxiao2021-02-28  58

import java.io.IOException; import java.lang.reflect.Field; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; 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.ss.usermodel.HorizontalAlignment; public class ExportExcel{ /** * * @param title * 表格标题名 * @param headersName * 表格属性列名数组 * @param headersId * 表格属性列名对应的字段 * @param dataset * 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象 * @param out * 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 * @param fileName * 导出文件名; * @param res * 浏览器的响应 */ public static boolean exportExcel(String[] headersName, String[] headersId, List<?> dtoList, String fileName, HttpServletResponse res) { // 表头 Map<Integer, String> map = new HashMap<Integer, String>(); int key = 0; for (int i = 0; i < headersName.length; i++) { if (!headersName[i].equals(null)) { map.put(key, headersName[i]); key++; } } // 字段 // Map<Integer, String> zdMap = new HashMap<Integer, String>(); // int value = 0; // for (int i = 0; i < headersId.length; i++) { // if (!headersId[i].equals(null)) { // zdMap.put(value, headersId[i]); // value++; // } // } // 声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); sheet.setDefaultColumnWidth((short) 15); // 生成一个样式 HSSFCellStyle style = wb.createCellStyle(); HSSFRow row = sheet.createRow(0); style.setAlignment(HorizontalAlignment.CENTER); HSSFCell cell; Collection c = map.values(); Iterator<String> it = c.iterator(); // 根据选择的字段生成表头 short size = 0; while (it.hasNext()) { cell = row.createCell(size); cell.setCellValue(it.next().toString()); cell.setCellStyle(style); size++; } // 字段 //Collection zdC = zdMap.values(); Iterator<?> labIt = dtoList.iterator(); int zdRow = 0; while (labIt.hasNext()) { // int zdCell = 0; zdRow++; row = sheet.createRow(zdRow); Object l=labIt.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = l.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); String fieldName = field.getName(); //System.out.println("-----------------------------"+fieldName); for (int j = 0; j < headersId.length; j++) { if(fieldName.equals(headersId[j])){ Object val; try { val = field.get(l); // System.out.println(val); String type = field.getType().toString();// 得到此属性的类型 // System.out.println(type); if (type.endsWith("String")) { row.createCell(j).setCellValue((String) val); } else if (type.endsWith("int") || type.endsWith("Integer")) { row.createCell(j).setCellValue((double) val); } else if (type.endsWith("Boolean")) { row.createCell(j).setCellValue((boolean) val); } else if (type.endsWith("Date")) { // 如果是日期格式,设置单元格格式; HSSFCell cella = row.createCell(j); if(val!=null){ cella.setCellValue((Date) val); HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFDataFormat format = wb.createDataFormat(); cellStyle.setDataFormat(format.getFormat("yyyy-MM-dd")); cella.setCellStyle(cellStyle);} } else if (type.endsWith("Timestamp")) { // 如果是日期格式,设置单元格格式; HSSFCell cella = row.createCell(j); cella.setCellValue((Date) val); HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFDataFormat format = wb.createDataFormat(); cellStyle.setDataFormat(format .getFormat("yyyy-MM-dd HH:mm")); cella.setCellStyle(cellStyle); } else if (type.endsWith("BigDecimal")) { row.createCell(j).setCellValue((double) val); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } ByteArrayOutputStream os =new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Out(res, fileName,os); // try { // // 浏览器响应.. // res.reset(); // res.setContentType("application/vnd.ms-excel;charset=utf-8"); // res.setHeader("Content-Disposition", "attachment;filename=" // + new String((fileName + ".xls").getBytes(), "iso-8859-1")); // // ServletOutputStream out = res.getOutputStream(); // // 工作薄以流的形式直接传到浏览器; // wb.write(out); // // out.close(); // // JOptionPane.showMessageDialog(null, "导出成功!"); // } catch (Exception e) { // // JOptionPane.showMessageDialog(null, "导出失败!"); // e.printStackTrace(); // } } public static boolean Out(HttpServletResponse res, String fileName, ByteArrayOutputStream os ) { try { // 浏览器响应.. res.reset(); res.setContentType("application/vnd.ms-excel;charset=utf-8"); res.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1")); ServletOutputStream out = res.getOutputStream(); byte[] bytes = os.toByteArray(); // 工作薄以流的形式直接传到浏览器; //wb.getBytes(); //wb.write(out); out.write(bytes); out.close(); // JOptionPane.showMessageDialog(null, "导出成功!"); } catch (Exception e) { // JOptionPane.showMessageDialog(null, "导出失败!"); e.printStackTrace(); return false; } return true; } }
转载请注明原文地址: https://www.6miu.com/read-78121.html

最新回复(0)