poi-匯出Excel
阿新 • • 發佈:2018-11-13
最新poi的jar包。與之前版本存在樣式設定區別
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
//建立工作簿workbook
//利用工作簿物件建立工作表sheet
//利用工作表物件建立行row
//利用行物件建立單元格物件cell
HSSFWorkbook wb= new HSSFWorkbook(); //建立工作薄
HSSFSheet sheet=wb.createSheet(); //利用工作薄建立工作表
// 表頭樣式
HSSFFont font = wb.createFont();//建立字型物件
font.setFontHeightInPoints((short) 24);
HSSFCellStyle cellstyle=wb.createCellStyle();
cellstyle.setBorderBottom(BorderStyle.THIN);//下邊框
cellstyle.setBorderLeft (BorderStyle.THIN);//左邊框
cellstyle.setBorderTop(BorderStyle.THIN);//上邊框
cellstyle.setBorderRight(BorderStyle.THIN);//右邊框
cellstyle.setAlignment(HorizontalAlignment.CENTER);//設定字型居中
cellstyle.setFont(font);
// 欄位名樣式
HSSFFont font2 = wb.createFont();//建立字型物件
font2.setFontHeightInPoints ((short) 16);
HSSFCellStyle cellstyle1=wb.createCellStyle();
cellstyle1.setBorderBottom(BorderStyle.THIN);//下邊框
cellstyle1.setBorderLeft(BorderStyle.THIN);//左邊框
cellstyle1.setBorderTop(BorderStyle.THIN);//上邊框
cellstyle1.setBorderRight(BorderStyle.THIN);//右邊框
cellstyle1.setAlignment(HorizontalAlignment.CENTER);
cellstyle1.setFont(font2);
// 列表樣式
HSSFFont font1 = wb.createFont();//建立字型物件
font1.setFontHeightInPoints((short) 12);
HSSFCellStyle cellstyle2=wb.createCellStyle();
cellstyle2.setBorderBottom(BorderStyle.THIN);//下邊框
cellstyle2.setBorderLeft(BorderStyle.THIN);//左邊框
cellstyle2.setBorderTop(BorderStyle.THIN);//上邊框
cellstyle2.setBorderRight(BorderStyle.THIN);//右邊框
cellstyle2.setFont(font1);
HSSFRow row = null;//建立行物件
HSSFCell cell =null;//建立列物件
CellRangeAddress cellRangeAddress =null;// new CellRangeAddress(0, 0, 0, 11);
String yuanShi[] = new String[]{"序號","地區","部門","名稱","編碼","型別","建立時間"};
// 合併單元格 前兩個引數行 後兩個列
sheet.addMergedRegion(new CellRangeAddress(0,0,0, 6));//合併單元格
row = sheet.createRow((short)0);//通過工作薄建立行物件
cell = row.createCell((short)0);//通過行物件獲得列物件
cell.setCellValue("事項清單列表");//設定列的值
cell.setCellStyle(cellstyle);//設定表格樣式
sheet.setDefaultRowHeight((short)30);
//每次
row = sheet.createRow((short)1);//新建行物件
for(int s=0;s<yuanShi.length;s++){
cell = row.createCell((short)s);
cell.setCellValue(yuanShi[s]);
cell.setCellStyle(cellstyle1);
sheet.setColumnWidth((short)0,(short)(15 * 300));
}
for(int j=0;j<object.size();j++){
Object[] obj = object.get(j);
row = sheet.createRow((short)j+2);
cell = row.createCell((short)0);//建立列
cell.setCellValue(j+1);//序號
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)0,(short)(15 * 150));
cell = row.createCell((short)1);
cell.setCellValue(obj[0].toString());//地區
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)1,(short)(15 * 300));
cell = row.createCell((short)2);
cell.setCellValue(obj[1].toString());//部門
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)2,(short)(15 * 300));
cell = row.createCell((short)3);
cell.setCellValue(obj[2].toString());//名稱
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)3,(short)(20 * 300));
cell = row.createCell((short)4);
cell.setCellValue(obj[4].toString());//編碼
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)4,(short)(15 * 300));
cell = row.createCell((short)5);
cell.setCellValue(obj[5].toString());//型別
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)5,(short)(15 * 300));
cell = row.createCell((short)6);
cell.setCellValue(obj[3].toString());//建立時間
cell.setCellStyle(cellstyle2);
sheet.setColumnWidth((short)6,(short)(30 * 300));
}
// 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,
// 第六步,將檔案存到指定位置
boolean t =false;
try {
ServletOutputStream servletoutputstream = rep.getOutputStream();
String fileName = "";
if(req.getHeader("user-agent").indexOf("MSIE") != -1 || req.getHeader("user-agent").indexOf("rv:11") !=-1) {
fileName = java.net.URLEncoder.encode("匯出","utf-8") + ".xls";
}else{
fileName = new String("匯出".getBytes("utf-8"),"iso-8859-1")+ ".xls";
}
rep.setHeader("Content-disposition", "attachment; filename="+ fileName);
rep.setContentType("application/vnd.ms-excel;charset=utf-8");
wb.write(servletoutputstream);
servletoutputstream.flush();
t =true;
servletoutputstream.close();
}
catch (Exception e) {
e.printStackTrace();
}
return t;