【前言】
昔日齷齪不足誇,今朝放蕩思無涯。
【思路】
咱們在桌面上建立Excel的時候:ide
//聲明工做薄 HSSFWorkbook wb = new HSSFWorkbook();
//sheet頁籤部分 HSSFSheet sheet = wb.createSheet("頁籤的名字");
//合併標題 sheet.addMergedRegion(new Region(0, (short)0, (short)0,(short)(規劃的列數)));
//建立表頭 row = sheet.createRow(1);
//第一層爲循環建立行 for (int i = 0; i < contentLst.size(); i++) { row = sheet.createRow(i+2); row.setHeight((short) 550); //第二層建立每行的單元格,並填內容 for (int j = 0; j < contentLst.get(i).length; j++) { cell = row.createCell(j); cell.setCellValue(String.valueOf(contentLst.get(i)[j])); cell.setCellStyle(shstyle); } }
//建立標題 HSSFRow row = sheet.createRow(0); //設置標題行高 row.setHeight((short) 行高數); //自定義列寬部分,你將每一個列寬做爲參數傳過來具體每列多寬得本身測試了。 if(liekuanLst != null && liekuanLst.size() > 0){ for (int i = 0; i < liekuanLst.size(); i++) { sheet.setColumnWidth((short)i, liekuanLst.get(i)); } }
//居中字體等樣式區域 sheet.setHorizontallyCenter(true); //主題 HSSFCellStyle titlefontshstyle = wb.createCellStyle(); HSSFFont titlefont = wb.createFont(); setcontentStyleTable(titlefontshstyle, titlefont, "黑體", 20, true, false); public static void setcontentStyleTable(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i, Boolean is_bold, Boolean is_border) { titlefont.setFontHeightInPoints((short) i); titlefont.setFontName(string); shstyle.setWrapText(true);//自動換行 shstyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER); if(i != 11 && is_bold){ titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } if(is_border){ shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框 shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左 shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右 shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上 } shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中 shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定義字體樣式左右居中 shstyle.setFont(titlefont); }
//添加打印樣式 addPrintClassData(sheet, false); //打印的紙張樣式 public static void addPrintClassData(HSSFSheet sheet, Boolean is_landscape) { sheet.setMargin(HSSFSheet.TopMargin, 0.4);//上≈2 sheet.setMargin(HSSFSheet.BottomMargin, 0.4);//下≈2 sheet.setMargin(HSSFSheet.LeftMargin, 0.2);//左≈0.5 sheet.setMargin(HSSFSheet.RightMargin, 0.2);//右≈0.5 sheet.setHorizontallyCenter(true); sheet.setDefaultRowHeight((short) 400);//設置默認行高 HSSFPrintSetup ps = sheet.getPrintSetup(); ps.setLandscape(is_landscape);//true橫向,false縱向 ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//設置紙張A4 }
ByteArrayOutputStream bos = null; try{ /*1轉爲輸入流*/ bos = new ByteArrayOutputStream(); wb.write(bos); byte[] bytes = bos.toByteArray(); InputStream in = new ByteArrayInputStream(bytes); /*2直接寫入Excel文檔*/ /*String fileName = new Date().getTime()+"frozen_excel.xls"; FileOutputStream output = new FileOutputStream("/temp/"+fileName); wb.write(output); output.close();*/ bos.close(); }catch (Exception e){ e.printStackTrace(); }
【總結】
看看上面9步須要哪些參數,將參數封裝作一個統一的公共方法,也就是測試
/** * 導出Excel * @param title Excel標題 * @param liekuanLst 設置的每一個列寬 * @param biaotouLst 表頭內容 * @param contentLst 單元格內容 * @return 待定 */ public String export_Excel(String title, List<Integer> liekuanLst, List<String> biaotouLst, List<Object[]> contentLst){ //聲明工做薄 HSSFWorkbook wb = new HSSFWorkbook(); //sheet頁籤部分 HSSFSheet sheet = wb.createSheet(title); //自定義列寬部分 if(liekuanLst != null && liekuanLst.size() > 0){ for (int i = 0; i < liekuanLst.size(); i++) { sheet.setColumnWidth((short)i, liekuanLst.get(i)); } } //居中字體等樣式區域 sheet.setHorizontallyCenter(true); //主題 HSSFCellStyle titlefontshstyle = wb.createCellStyle(); HSSFFont titlefont = wb.createFont(); setcontentStyleTable(titlefontshstyle, titlefont, "黑體", 20, true, false); //表頭樣式 HSSFCellStyle btfontshstyle = wb.createCellStyle(); HSSFFont btfont = wb.createFont(); setcontentStyleTable(btfontshstyle, btfont, "宋體", 10, true, true); //內容樣式 HSSFCellStyle shstyle = wb.createCellStyle(); HSSFFont contentfont = wb.createFont(); setcontentStyle(shstyle, contentfont, "宋體", 11); //建立標題 HSSFRow row = sheet.createRow(0); //設置標題行高 row.setHeight((short) 920); HSSFCell cell = row.createCell(0); cell.setCellValue(title); cell.setCellStyle(titlefontshstyle); for (int i = 1; i < liekuanLst.size(); i++) { cell = row.createCell(i); cell.setCellStyle(titlefontshstyle); } //合併標題 sheet.addMergedRegion(new Region(0, (short)0, (short)0,(short)(liekuanLst.size()-1))); //建立表頭 row = sheet.createRow(1); //行高 row.setHeight((short) 550); if(biaotouLst != null && biaotouLst.size() > 0){ for (int i = 0; i < biaotouLst.size(); i++) { cell = row.createCell(i); cell.setCellValue(biaotouLst.get(i)); cell.setCellStyle(btfontshstyle); } } if(contentLst != null && contentLst.size() > 0){ //循環插入表格內容 for (int i = 0; i < contentLst.size(); i++) { //當前行 row = sheet.createRow(i+2); row.setHeight((short) 550); //每一個單元格 for (int j = 0; j < contentLst.get(i).length; j++) { cell = row.createCell(j); cell.setCellValue(String.valueOf(contentLst.get(i)[j])); cell.setCellStyle(shstyle); } } } //添加打印樣式 addPrintClassData(sheet, false); /** * 寫入數據 */ ByteArrayOutputStream bos = null; try{ /*1轉爲輸入流*/ bos = new ByteArrayOutputStream(); wb.write(bos); byte[] bytes = bos.toByteArray(); InputStream in = new ByteArrayInputStream(bytes); /*2直接寫入Excel文檔*/ /*String fileName = new Date().getTime()+"frozen_excel.xls"; FileOutputStream output = new FileOutputStream("/temp/"+fileName); wb.write(output); output.close();*/ bos.close(); }catch (Exception e){ e.printStackTrace(); } return ""; } //單元格樣式 public static void setcontentStyle(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i) { titlefont.setFontHeightInPoints((short) i); titlefont.setFontName(string); shstyle.setWrapText(true);//自動換行 if(i != 11){ titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定義字體樣式左右居中 shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中 shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框 shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左 shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右 shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上 shstyle.setFont(titlefont); } public static void setcontentStyleTable(HSSFCellStyle shstyle, HSSFFont titlefont, String string, int i, Boolean is_bold, Boolean is_border) { titlefont.setFontHeightInPoints((short) i); titlefont.setFontName(string); shstyle.setWrapText(true);//自動換行 shstyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER); if(i != 11 && is_bold){ titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } if(is_border){ shstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框 shstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左 shstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右 shstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上 } shstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中 shstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//定義字體樣式左右居中 shstyle.setFont(titlefont); } //打印的紙張樣式 public static void addPrintClassData(HSSFSheet sheet, Boolean is_landscape) { sheet.setMargin(HSSFSheet.TopMargin, 0.4);//上≈2 sheet.setMargin(HSSFSheet.BottomMargin, 0.4);//下≈2 sheet.setMargin(HSSFSheet.LeftMargin, 0.2);//左≈0.5 sheet.setMargin(HSSFSheet.RightMargin, 0.2);//右≈0.5 sheet.setHorizontallyCenter(true); sheet.setDefaultRowHeight((short) 400);//設置默認行高 HSSFPrintSetup ps = sheet.getPrintSetup(); ps.setLandscape(is_landscape);//true橫向,false縱向 ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//設置紙張A4 }
【結束語】
還能夠繼續優化!字體