package com.HeiBeiEDU.test2; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.lang.reflect.Method; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.HSSFCell; 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; public class ListToExcel { /** * * @param name * Excel保存的主题名 * @param data * 里边有Map和List Map存放字段对应关系(ziDuan,字段的第一个字符是序号) * List存放对象数据(listData) * @return [0]是fileName [1]是filePath */ public static String[] objListToExcel(String name, Map data, String path) { Map<String, String> ziDuan = (Map<String, String>) data.get("ziDuan"); List listData = (List) data.get("listData"); Object[] keys = ziDuan.keySet().toArray(); String[] ziDuanKeys = new String[keys.length]; for (int k = 0; k < keys.length; k++) { String temp = keys[k].toString(); int xuHao = Integer.valueOf(temp.substring(0, 1)); ziDuanKeys[xuHao] = temp.substring(1); } try { File newFile = new File(path); if (newFile.exists()) { int ii = JOptionPane.showConfirmDialog(null, "文件已存在,你确定要覆盖吗?", "文件已存在", JOptionPane.YES_NO_OPTION); if (ii == 0) {// 确定 newFile.createNewFile(); System.out.println("创建文件成功:" + path); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); for (int i = 0; i < listData.size(); i++) { HSSFRow row = sheet.createRow(i); Object obj = listData.get(i); for (int j = 0; j < ziDuanKeys.length; j++) { HSSFCell cell = row.createCell(j); if (i == 0) { sheet.setColumnWidth(j, 6000); cell.setCellValue(new HSSFRichTextString(ziDuan.get(j + ziDuanKeys[j]))); } else { String ziDuanName = (String) ziDuanKeys[j]; System.out.println(ziDuanName); ziDuanName = ziDuanName.replaceFirst(ziDuanName.substring(0, 1), ziDuanName.substring(0, 1).toUpperCase()); ziDuanName = "get" + ziDuanName; Class clazz = Class.forName(obj.getClass().getName()); Method[] methods = clazz.getMethods(); Pattern pattern = Pattern.compile(ziDuanName); Matcher mat = null; for (Method m : methods) { mat = pattern.matcher(m.getName()); if (mat.find()) { Object shuXing = m.invoke(obj, null); if (shuXing != null) { cell.setCellValue(shuXing.toString());// 这里可以做数据格式处理 } else { cell.setCellValue(""); } break; } } } } } OutputStream out = new FileOutputStream(path); wb.write(out);// 写入File out.flush(); out.close(); } } else { newFile.createNewFile(); System.out.println("创建文件成功:" + path); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); for (int i = 0; i < listData.size(); i++) { HSSFRow row = sheet.createRow(i); Object obj = listData.get(i); for (int j = 0; j < ziDuanKeys.length; j++) { HSSFCell cell = row.createCell(j); if (i == 0) { sheet.setColumnWidth(j, 6000); cell.setCellValue(new HSSFRichTextString(ziDuan.get(j + ziDuanKeys[j]))); } else { String ziDuanName = (String) ziDuanKeys[j]; System.out.println(ziDuanName); ziDuanName = ziDuanName.replaceFirst(ziDuanName.substring(0, 1), ziDuanName.substring(0, 1).toUpperCase()); ziDuanName = "get" + ziDuanName; Class clazz = Class.forName(obj.getClass().getName()); Method[] methods = clazz.getMethods(); Pattern pattern = Pattern.compile(ziDuanName); Matcher mat = null; for (Method m : methods) { mat = pattern.matcher(m.getName()); if (mat.find()) { Object shuXing = m.invoke(obj, null); if (shuXing != null) { cell.setCellValue(shuXing.toString());// 这里可以做数据格式处理 } else { cell.setCellValue(""); } break; } } } } } OutputStream out = new FileOutputStream(path); wb.write(out);// 写入File out.flush(); out.close(); } return null; } catch (Exception e) { e.printStackTrace(); return null; } } }
测试代码为:
try { List listData = new ArrayList(); //---------此处应遍历真正的list,但是现在没有办法测试webservice,所以先写测试代码----------------------------- for (int i = 0; i < 6; i++) { CheckPerson person = new CheckPerson(); person.setDHHM("145647583486"); person.setGMSFHM("13546876874786643"); person.setMZ("MZ"); person.setXM("XM"); person.setXZ("XZ"); listData.add(person); } Map<String, String> ziDuan = new HashMap<String, String>(); ziDuan.put("0DHHM", "电话号码");//属性前边的数字代表字段的先后顺序。 ziDuan.put("1MZ", "暂定");//最好将源码中判别顺序的格式改为"序号-字段"。 ziDuan.put("2XM", "姓名"); ziDuan.put("3XZ", "住址"); Map data = new HashMap(); data.put("listData", listData); data.put("ziDuan", ziDuan); ListToExcel.objListToExcel("爆破作业人员查询结果名单", data,path); }catch(Exception e3){ e3.printStackTrace(); }
时间: 2024-10-09 20:03:09