POI匯入匯出Excel檔案(二)
阿新 • • 發佈:2019-02-16
最近工作中用到的POI匯入匯出Excel檔案的地方比較到,所以就乘機總結一下,在這裡先說以匯出Excel的情況簡單的說一下,現在就直接上程式碼,在程式碼上面做註釋了。
/**
* 處理匯出的資料資訊
* @param list
* @return
*/
private int rownum = 0; //在這裡是建立一個全域性變數,用來表示建立的行數
private void exportExcelData(String title,List<String> headerList,List<Yangbyj> dataList,HttpServletResponse response, String fileName) throws Exception{
//建立excel檔案
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(500);
//建立sheet頁面
Sheet createSheet = sxssfWorkbook.createSheet("Export");
//建立excel的檔案樣式
Map<String, CellStyle> createStyles = createStyles(sxssfWorkbook);
//建立標題
if(StringUtils.isNotBlank(title)){
//建立sheet中的行
Row titleRow = createSheet.createRow(rownum++);
titleRow.setHeightInPoints(30); //設定行高
Cell titleCell = titleRow.createCell(0); //建立標題的單元格
titleCell.setCellStyle(createStyles.get("title")); //設定單元格的樣式
titleCell.setCellValue(title); //給單元格賦值
//合併單元格
createSheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(),
titleRow.getRowNum(),titleRow.getRowNum(),headerList.size()-1));
}
//建立表頭
if(headerList == null||headerList.size()==0){
throw new RuntimeException("headerList not null!");
}
//建立表頭行
Row headerRow = createSheet.createRow(rownum++);
headerRow.setHeightInPoints(16);
for(int i=0;i<headerList.size();i++){
Cell cell = headerRow.createCell(i);
cell.setCellStyle(createStyles.get("header"));
cell.setCellValue(headerList.get(i));
/*給表格新增批註
* Comment comment = createSheet.createDrawingPatriarch().createCellComment(
new XSSFClientAnchor(0, 0, 0, 0, 3, 3, 5, 6));
cell.setCellComment(comment);*/
createSheet.autoSizeColumn(i); //自動調整列寬
}
//設定寬度
for(int i=0;i<headerList.size();i++){
int colWidth = createSheet.getColumnWidth(i)*2;
createSheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);
}
//處理資料資訊
for (Yangbyj y : dataList) {
Row row = createSheet.createRow(rownum++);
CellStyle style = createStyles.get("data2");
Cell oneCell = row.createCell(0);
DataFormat format = sxssfWorkbook.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/MM/dd"));
oneCell.setCellValue((Date) y.getShij());
oneCell.setCellStyle(style);
Cell twoCell = row.createCell(1);
twoCell.setCellValue( y.getYunj());
twoCell.setCellStyle(style);
}
//通過瀏覽器將資料寫出來
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode(fileName));
sxssfWorkbook.write(response.getOutputStream());
}
下面是excel的樣式,可以直接拿來用。
private Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
Font titleFont = wb.createFont();
titleFont.setFontName("Arial");
titleFont.setFontHeightInPoints((short) 16);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(titleFont);
styles.put("title", style);
style = wb.createCellStyle();
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_LEFT);
styles.put("data1", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_CENTER);
styles.put("data2", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(CellStyle.ALIGN_RIGHT);
styles.put("data3", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
// style.setWrapText(true);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
上面只是簡單的說以下我這裡用到的部分,後續的自己也在學習中,多多斧正。