1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package cn.vgbhfive.poidemo.utils;
import org.apache.poi.hssf.usermodel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List;
@Service public class ExcelUtils {
private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
private static final short borderRpx = 1;
public static HSSFWorkbook expExcel(List<String> headers, List<List<String>> data) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("sheet1"); HSSFRow row = sheet.createRow(0); HSSFCell cell = null; HSSFCellStyle style = workbook.createCellStyle(); setBorderStyle(style, borderRpx); style.setFont(setFontStyle(workbook, "黑体", (short) 14)); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); sheet.createFreezePane(0, 1, 0, 1);
for (int i = 0; i < headers.size(); i++) { cell = row.createCell(i); cell.setCellValue(headers.get(i)); cell.setCellStyle(style); }
HSSFCellStyle style1 = workbook.createCellStyle(); setBorderStyle(style1, borderRpx); style1.setFont(setFontStyle(workbook, "宋体", (short) 12)); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); for (int i = 0; i < data.size(); i++) { row = sheet.createRow(i+1); List<String> param = data.get(i); for (int j = 0; j < param.size(); j++) { cell = row.createCell(j); cell.setCellValue(param.get(j)); cell.setCellStyle(style1); } }
for (int i = 0; i < headers.size(); i++) { sheet.autoSizeColumn(i); }
return workbook; }
public static void outFile(HSSFWorkbook workbook, String path, HttpServletResponse response) { OutputStream os=null; File file = null; try { file = new File(path); String filename = file.getName(); os = new FileOutputStream(file); response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8")); os= new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=utf-8"); workbook.write(os); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { os.flush(); os.close(); System.gc(); System.out.println(file.delete()); } catch (IOException e) { e.printStackTrace(); } }
public static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) { HSSFFont font = workbook.createFont(); font.setFontHeightInPoints(height); font.setFontName(name); return font; }
public static void setBorderStyle(HSSFCellStyle cellStyle, short border) { cellStyle.setBorderBottom(border); cellStyle.setBorderLeft(border); cellStyle.setBorderTop(border); cellStyle.setBorderRight(border); }
}
|