1.基于项目开发的业务需求,导出部分字段的Excel,写出了如下的通用的方法,基于POI 3.10-FINALjar包开发
1 import java.io.FileNotFoundException; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 11 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 12 import org.apache.poi.ss.usermodel.Cell; 13 import org.apache.poi.ss.usermodel.CellStyle; 14 import org.apache.poi.ss.usermodel.Row; 15 import org.apache.poi.ss.usermodel.Sheet; 16 import org.apache.poi.ss.usermodel.Workbook; 17 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 18 import org.slf4j.Logger; 19 import org.slf4j.LoggerFactory; 20 21 import com.epichust.unimax.client.dao.QisRecordDao; 22 23 public class ExportExcel 24 { 25 private static Logger logger = LoggerFactory.getLogger(QisRecordDao.class); 26 public static boolean exportExcel(String fileName, String title, String[] headers, List<Map> dataset, String pattern) 27 { 28 boolean flag = false; 29 Workbook workbook = null; 30 if (fileName.endsWith("xlsx")) 31 { 32 workbook = new XSSFWorkbook(); 33 } else if (fileName.endsWith("xls")) 34 { 35 workbook = new HSSFWorkbook(); 36 } else 37 { 38 try 39 { 40 throw new Exception("invalid file name, should be xls or xlsx"); 41 } catch (Exception e) 42 { 43 logger.info("必须是xls或者xlsx结尾的文件."); 44 e.printStackTrace(); 45 } 46 47 } 48 49 Sheet sheet = workbook.createSheet(title); 50 CellStyle style = workbook.createCellStyle(); 51 52 // 列名 53 Row row = sheet.createRow(0); 54 for (int i = 0; i < headers.length; i++) 55 { 56 Cell cell = row.createCell(i); 57 sheet.setColumnWidth(i, 5000); 58 style.setAlignment(CellStyle.ALIGN_CENTER); 59 cell.setCellValue(headers[i]); 60 } 61 62 Iterator<Map> it = dataset.iterator(); 63 int index = 0; 64 while (it.hasNext()) 65 { 66 index++; 67 row = sheet.createRow(index); 68 69 Map map = it.next(); 70 logger.info(map.toString()); 71 Set<String> mapKey = (Set<String>)map.keySet(); 72 logger.info(mapKey.toString()); 73 Iterator<String> iterator = mapKey.iterator(); 74 logger.info(iterator.toString()); 75 int num = 0; 76 while(iterator.hasNext()){ 77 Cell cell = row.createCell(num); 78 num++; 79 String key = iterator.next(); 80 logger.info(key); 81 Object obj = map.get(key); 82 if (obj instanceof Date) 83 { 84 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 85 cell.setCellValue(sdf.format(obj)); 86 } else if (obj instanceof Integer) 87 { 88 cell.setCellValue((Integer) obj); 89 } else if (obj instanceof Double) 90 { 91 cell.setCellValue((Double) obj); 92 } else 93 { 94 cell.setCellValue((String) obj); 95 } 96 } 97 } 98 FileOutputStream fos; 99 try 100 { 101 fos = new FileOutputStream(fileName); 102 workbook.write(fos); 103 fos.close(); 104 flag = true; 105 } catch (FileNotFoundException e) 106 { 107 logger.info("文件不存在"); 108 flag = false; 109 e.printStackTrace(); 110 } catch (IOException e) 111 { 112 logger.info("文件写入错误"); 113 flag = false; 114 e.printStackTrace(); 115 116 } 117 return flag; 118 } 119 }