poi單個導出Excel(klx的比較完整的例子)
//創建Excel
@RequestMapping(value = { "listExportCal", "" })
public String listExport(FormBean formBean, HttpServletRequest request,
HttpServletResponse response) throws IOException {
Map<String, Object> searchParams = formBean.getBean();
String year = MapUtils.getString(searchParams, "ND", "")+"年";
new Page<Map<String, Object>>(request, response), formBean);
//創建HSSFWorkbook對象(excel的文檔對象)
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet對象(excel的表單)
HSSFSheet sheet=wb.createSheet("中山大學黨委組織部部歷");
//================樣式設置start======================
//樣式1
HSSFCellStyle setBorder = wb.createCellStyle();
//二、設置邊框:
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
// 三、設置居中:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 四、設置字體:
HSSFFont font = wb.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 16);//設置字體大小
/*
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);*/
setBorder.setFont(font);//選擇需要用到的字體格式
//樣式2
HSSFCellStyle setBorder1 = wb.createCellStyle();
//一、設置背景色:
setBorder1.setFillForegroundColor(IndexedColors.TAN.getIndex());// 設置背景色
setBorder1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//二、設置邊框:
setBorder1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
// 三、設置居中:
setBorder1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 四、設置字體:
HSSFFont font1 = wb.createFont();
font1.setFontName("宋體");
font1.setFontHeightInPoints((short) 16);//設置字體大小
/*
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);*/
setBorder1.setFont(font1);//選擇需要用到的字體格式
//樣式4
HSSFCellStyle setBorder3 = wb.createCellStyle();
//一、設置背景色:
setBorder3.setFillForegroundColor(IndexedColors.TAN.getIndex());// 設置背景色
setBorder3.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//二、設置邊框:
setBorder3.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder3.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder3.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder3.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
// 三、設置居中:
setBorder3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 指定單元格垂直居中對齊
setBorder3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 四、設置字體:
HSSFFont font3 = wb.createFont();
font3.setFontName("宋體");
font3.setFontHeightInPoints((short) 14);//設置字體大小
/*
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);*/
setBorder3.setFont(font3);//選擇需要用到的字體格式
//樣式5
HSSFCellStyle setBorder4 = wb.createCellStyle();
//二、設置邊框:
setBorder4.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder4.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder4.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder4.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
// 三、設置居中:
setBorder4.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 指定單元格垂直居中對齊
setBorder4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 四、設置字體:
HSSFFont font4 = wb.createFont();
font4.setFontName("宋體");
font4.setFontHeightInPoints((short) 12);//設置字體大小
/*
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);*/
setBorder4.setFont(font4);//選擇需要用到的字體格式
//樣式6
HSSFCellStyle setBorder5 = wb.createCellStyle();
setBorder5.setWrapText(true);
//二、設置邊框:
setBorder5.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder5.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder5.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder5.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
// 三、設置居中:
setBorder5.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 居zuo
// 四、設置字體:
HSSFFont font5 = wb.createFont();
font5.setFontName("宋體");
font5.setFontHeightInPoints((short) 12);//設置字體大小
/*
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);*/
setBorder5.setFont(font5);//選擇需要用到的字體格式
//================樣式設置end========================
HSSFRow row1=sheet.createRow(0);
//創建單元格(excel的單元格,參數為列索引,可以是0~255之間的任何一個
HSSFCell cell=row1.createCell(0);
HSSFCell cell01 = row1.createCell(0);
cell01.setCellStyle(setBorder);
HSSFCell cell111 = row1.createCell(1);
cell111.setCellStyle(setBorder);
HSSFCell cell21 = row1.createCell(2);
cell21.setCellStyle(setBorder);
HSSFCell cell31 = row1.createCell(3);
cell31.setCellStyle(setBorder);
HSSFCell cell41 = row1.createCell(4);
cell41.setCellStyle(setBorder);
HSSFCell cell51 = row1.createCell(5);
cell51.setCellStyle(setBorder);
HSSFCell cell61 = row1.createCell(6);
cell61.setCellStyle(setBorder);
HSSFCell cell71 = row1.createCell(7);
cell71.setCellStyle(setBorder);
HSSFCell cell81 = row1.createCell(8);
cell81.setCellStyle(setBorder);
HSSFCell cell91 = row1.createCell(9);
cell91.setCellStyle(setBorder);
HSSFCell cell101 = row1.createCell(10);
cell101.setCellStyle(setBorder);
HSSFCell cell0111 = row1.createCell(11);
cell0111.setCellStyle(setBorder);
HSSFCell cell121 = row1.createCell(12);
cell121.setCellStyle(setBorder);
HSSFCell cell131 = row1.createCell(13);
cell131.setCellStyle(setBorder);
HSSFCell cell141 = row1.createCell(14);
cell141.setCellStyle(setBorder);
//設置單元格內容
cell.setCellValue("中山大學黨委組織部部歷");
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,14));
cell.setCellStyle(setBorder);
//在sheet裏創建第二行
HSSFRow row2=sheet.createRow(1);
//創建單元格並設置單元格內容
HSSFCell cell0 = row2.createCell(0);
cell0.setCellValue(year);
cell0.setCellStyle(setBorder1);
HSSFCell cell1 = row2.createCell(1);
cell1.setCellValue("周次");
cell1.setCellStyle(setBorder1);
HSSFCell cell2 = row2.createCell(2);
cell2.setCellValue("日");
cell2.setCellStyle(setBorder1);
HSSFCell cell3 = row2.createCell(3);
cell3.setCellValue("一");
cell3.setCellStyle(setBorder1);
HSSFCell cell4 = row2.createCell(4);
cell4.setCellValue("二");
cell4.setCellStyle(setBorder1);
HSSFCell cell5 = row2.createCell(5);
cell5.setCellValue("三");
cell5.setCellStyle(setBorder1);
HSSFCell cell6 = row2.createCell(6);
cell6.setCellValue("四");
cell6.setCellStyle(setBorder1);
HSSFCell cell7 = row2.createCell(7);
cell7.setCellValue("五");
cell7.setCellStyle(setBorder1);
HSSFCell cell8 = row2.createCell(8);
cell8.setCellValue("六");
cell8.setCellStyle(setBorder1);
HSSFCell cell9 = row2.createCell(9);
cell9.setCellValue("工作事項");
cell9.setCellStyle(setBorder1);
HSSFCell cell10 = row2.createCell(10);
cell10.setCellValue("");
cell10.setCellStyle(setBorder1);
HSSFCell cell11 = row2.createCell(11);
cell11.setCellValue("");
cell11.setCellStyle(setBorder1);
HSSFCell cell12 = row2.createCell(12);
cell12.setCellValue("");
cell12.setCellStyle(setBorder1);
HSSFCell cell13 = row2.createCell(13);
cell13.setCellValue("");
cell13.setCellStyle(setBorder1);
HSSFCell cell15 = row2.createCell(14);
cell15.setCellValue("備註");
cell15.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region2 = new CellRangeAddress(1,1,9,(short) 13); //給定要合並的單元格範圍
sheet.addMergedRegion(region2);
//在sheet裏創建第三行
HSSFRow row3=sheet.createRow(2);
//創建單元格並設置單元格內容
HSSFCell cell30 = row3.createCell(0);
cell30.setCellValue(year);
cell30.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region3 = new CellRangeAddress(1,2,0,(short) 0); //給定要合並的單元格範圍
sheet.addMergedRegion(region3);
HSSFCell cell311 = row3.createCell(1);
cell311.setCellValue("周次");
cell311.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region4= new CellRangeAddress(1,2,1,(short) 1); //給定要合並的單元格範圍
sheet.addMergedRegion(region4);
HSSFCell cell32 = row3.createCell(2);
cell32.setCellValue("日");
cell32.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region5= new CellRangeAddress(1,2,2,(short) 2); //給定要合並的單元格範圍
sheet.addMergedRegion(region5);
HSSFCell cell33 = row3.createCell(3);
cell33.setCellValue("一");
cell33.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region6= new CellRangeAddress(1,2,3,(short) 3); //給定要合並的單元格範圍
sheet.addMergedRegion(region6);
HSSFCell cell34 = row3.createCell(4);
cell34.setCellValue("二");
cell34.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region61= new CellRangeAddress(1,2,4,(short) 4); //給定要合並的單元格範圍
sheet.addMergedRegion(region61);
HSSFCell cell35 = row3.createCell(5);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region7= new CellRangeAddress(1,2,5,(short) 5); //給定要合並的單元格範圍
sheet.addMergedRegion(region7);
cell35.setCellValue("三");
cell35.setCellStyle(setBorder1);
HSSFCell cell36 = row3.createCell(6);
cell36.setCellValue("四");
cell36.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region8= new CellRangeAddress(1,2,6,(short) 6); //給定要合並的單元格範圍
sheet.addMergedRegion(region8);
HSSFCell cell37 = row3.createCell(7);
cell37.setCellValue("五");
cell37.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region9= new CellRangeAddress(1,2,7,(short) 7); //給定要合並的單元格範圍
sheet.addMergedRegion(region9);
HSSFCell cell38 = row3.createCell(8);
cell38.setCellValue("六");
cell38.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
CellRangeAddress region10= new CellRangeAddress(1,2,8,(short) 8); //給定要合並的單元格範圍
sheet.addMergedRegion(region10);
HSSFCell cell310 = row3.createCell(9);
cell310.setCellValue("組織工作");
cell310.setCellStyle(setBorder1);
//設置單元格的寬度
sheet.setColumnWidth(2, 5 * 256);
sheet.setColumnWidth(3, 5 * 256);
sheet.setColumnWidth(4, 5 * 256);
sheet.setColumnWidth(5, 5 * 256);
sheet.setColumnWidth(6, 5 * 256);
sheet.setColumnWidth(7, 5 * 256);
sheet.setColumnWidth(8, 5 * 256);
sheet.setColumnWidth(9, 25 * 256);
sheet.setColumnWidth(10, 25 * 256);
sheet.setColumnWidth(11, 25 * 256);
sheet.setColumnWidth(12, 25 * 256);
sheet.setColumnWidth(13, 25 * 256);
sheet.setColumnWidth(14, 25 * 256);
HSSFCell cell3111 = row3.createCell(10);
cell3111.setCellValue("幹部選任");
cell3111.setCellStyle(setBorder1);
HSSFCell cell312 = row3.createCell(11);
cell312.setCellValue("幹部監督");
cell312.setCellStyle(setBorder1);
HSSFCell cell313 = row3.createCell(12);
cell313.setCellValue("教育培訓");
cell313.setCellStyle(setBorder1);
HSSFCell cell314 = row3.createCell(13);
cell314.setCellValue("綜合管理");
cell314.setCellStyle(setBorder1);
HSSFCell cell315 = row3.createCell(14);
cell315.setCellValue("備註");
cell315.setCellStyle(setBorder1);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列(行合並)
CellRangeAddress region11= new CellRangeAddress(1,2,14,(short) 14); //給定要合並的單元格範圍
sheet.addMergedRegion(region11);
//定義行號
int index = 3;
//循環賦值
if(page != null && page.getList()!= null && page.getList().size()>0){
Map<String, Object> resMap = page.getList().get(0);// 取出的結果
List<Map<String, Object>> resList = getListByMap(resMap);
if(null != resList && resList.size()>0){
for(int i = 0;i<resList.size();i++){
++index;
//在sheet裏創建第三行
HSSFRow row5=sheet.createRow(3+i);
//創建單元格並設置單元格內容
HSSFCell cell50 = row5.createCell(0);
if(resList.get(i) != null && resList.get(i).containsKey("month") && null != resList.get(i).get("month")){
cell50.setCellValue(resList.get(i).get("month").toString()+"月");
}else{
cell50.setCellValue("");
}
cell50.setCellStyle(setBorder3);
HSSFCell cell511 = row5.createCell(1);
if(resList.get(i) != null && resList.get(i).containsKey("weekNum")&& null != resList.get(i).get("weekNum")){
cell511.setCellValue("第"+resList.get(i).get("weekNum").toString()+"周");
}else{
cell511.setCellValue("");
}
cell511.setCellStyle(setBorder4);
HSSFCell cell52 = row5.createCell(2);
if(resList.get(i) != null && resList.get(i).containsKey("week7")){
cell52.setCellValue(resList.get(i).get("week7").toString());
}else{
cell52.setCellValue("");
}
cell52.setCellStyle(setBorder4);
HSSFCell cell53 = row5.createCell(3);
if(resList.get(i) != null && resList.get(i).containsKey("week1")){
cell53.setCellValue(resList.get(i).get("week1").toString());
}else{
cell53.setCellValue("");
}
cell53.setCellStyle(setBorder4);
HSSFCell cell54 = row5.createCell(4);
if(resList.get(i) != null && resList.get(i).containsKey("week2")){
cell54.setCellValue(resList.get(i).get("week2").toString());
}else{
cell54.setCellValue("");
}
cell54.setCellStyle(setBorder4);
HSSFCell cell55 = row5.createCell(5);
if(resList.get(i) != null && resList.get(i).containsKey("week3")){
cell55.setCellValue(resList.get(i).get("week3").toString());
}else{
cell55.setCellValue("");
}
cell55.setCellStyle(setBorder4);
HSSFCell cell56 = row5.createCell(6);
if(resList.get(i) != null && resList.get(i).containsKey("week4")){
cell56.setCellValue(resList.get(i).get("week4").toString());
}else{
cell56.setCellValue("");
}
cell56.setCellStyle(setBorder4);
HSSFCell cell57 = row5.createCell(7);
if(resList.get(i) != null && resList.get(i).containsKey("week5")){
cell57.setCellValue(resList.get(i).get("week5").toString());
}else{
cell57.setCellValue("");
}
cell57.setCellStyle(setBorder4);
HSSFCell cell58 = row5.createCell(8);
if(resList.get(i) != null && resList.get(i).containsKey("week6")){
cell58.setCellValue(resList.get(i).get("week6").toString());
}else{
cell58.setCellValue("");
}
cell58.setCellStyle(setBorder4);
HSSFCell cell510 = row5.createCell(9);
if(resList.get(i) != null && resList.get(i).containsKey("strZZGZList")){
cell510.setCellValue(resList.get(i).get("strZZGZList").toString());
}else{
cell510.setCellValue("");
}
cell510.setCellStyle(setBorder5);
//設置單元格的寬度
sheet.setColumnWidth(2, 5 * 256);
sheet.setColumnWidth(3, 5 * 256);
sheet.setColumnWidth(4, 5 * 256);
sheet.setColumnWidth(5, 5 * 256);
sheet.setColumnWidth(6, 5 * 256);
sheet.setColumnWidth(7, 5 * 256);
sheet.setColumnWidth(8, 5 * 256);
sheet.setColumnWidth(9, 25 * 256);
sheet.setColumnWidth(10, 25 * 256);
sheet.setColumnWidth(11, 25 * 256);
sheet.setColumnWidth(12, 25 * 256);
sheet.setColumnWidth(13, 25 * 256);
sheet.setColumnWidth(14, 25 * 256);
HSSFCell cell5111 = row5.createCell(10);
if(resList.get(i) != null && resList.get(i).containsKey("strGBXRList")){
cell5111.setCellValue(resList.get(i).get("strGBXRList").toString());
}else{
cell5111.setCellValue("");
}
cell5111.setCellStyle(setBorder5);
HSSFCell cell512 = row5.createCell(11);
if(resList.get(i) != null && resList.get(i).containsKey("strGBJDList")){
cell512.setCellValue(resList.get(i).get("strGBJDList").toString());
}else{
cell512.setCellValue("");
}
cell512.setCellStyle(setBorder5);
HSSFCell cell513 = row5.createCell(12);
if(resList.get(i) != null && resList.get(i).containsKey("strJYPXList")){
cell513.setCellValue(resList.get(i).get("strJYPXList").toString());
}else{
cell513.setCellValue("");
}
cell513.setCellStyle(setBorder5);
HSSFCell cell514 = row5.createCell(13);
if(resList.get(i) != null && resList.get(i).containsKey("strZHGLList")){
cell514.setCellValue(resList.get(i).get("strZHGLList").toString());
}else{
cell514.setCellValue("");
}
cell514.setCellStyle(setBorder5);
HSSFCell cell515 = row5.createCell(14);
if(resList.get(i) != null && resList.get(i).containsKey("strZHGLList")){
cell515.setCellValue(resList.get(i).get("strZHGLList").toString());
}else{
cell515.setCellValue("");
}
cell515.setCellStyle(setBorder5);
}
}
}
//在sheet裏創建第index行
HSSFRow row6=sheet.createRow(index);
HSSFCell cell601 = row6.createCell(0);
cell601.setCellStyle(setBorder5);
String mark = "填表說明:";
mark=mark+"\n"+ " 1.請按工作類別填寫。";
mark=mark+"\n"+ " 2.僅填寫重點工作;常委會、幹部小組會議、跨部門會議等重要會議;全體人員參加的活動;其它重要事項。";
mark=mark+"\n"+"";
mark=mark+"\n"+"";
cell601.setCellValue(mark);
//設置單元格高度
row6.setHeight((short)1500);
HSSFCell cell6111 = row6.createCell(1);
cell6111.setCellStyle(setBorder5);
HSSFCell cell621 = row6.createCell(2);
cell621.setCellStyle(setBorder5);
HSSFCell cell631 = row6.createCell(3);
cell631.setCellStyle(setBorder5);
HSSFCell cell641 = row6.createCell(4);
cell641.setCellStyle(setBorder5);
HSSFCell cell651 = row6.createCell(5);
cell651.setCellStyle(setBorder5);
HSSFCell cell661 = row6.createCell(6);
cell661.setCellStyle(setBorder5);
HSSFCell cell671 = row6.createCell(7);
cell671.setCellStyle(setBorder5);
HSSFCell cell681 = row6.createCell(8);
cell681.setCellStyle(setBorder5);
HSSFCell cell691 = row6.createCell(9);
cell691.setCellStyle(setBorder5);
HSSFCell cell6101 = row6.createCell(10);
cell6101.setCellStyle(setBorder5);
HSSFCell cell60111 = row6.createCell(11);
cell60111.setCellStyle(setBorder5);
HSSFCell cell6121 = row6.createCell(12);
cell6121.setCellStyle(setBorder5);
HSSFCell cell6131 = row6.createCell(13);
cell6131.setCellStyle(setBorder5);
HSSFCell cell6141 = row6.createCell(14);
cell6141.setCellStyle(setBorder5);
//合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列(行合並)
CellRangeAddress region66= new CellRangeAddress(index,index,0,(short) 14); //給定要合並的單元格範圍
sheet.addMergedRegion(region66);
//輸出Excel文件
OutputStream output=response.getOutputStream();
response.reset();
String fileName = "中山大學黨委組織部部歷";
String agent = request.getHeader("USER-AGENT").toLowerCase();
response.setContentType("application/vnd.ms-excel");
String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls" );
} else {
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
}
response.setContentType("application/msexcel");
wb.write(output);
output.close();
return null;
}
poi單個導出Excel(klx的比較完整的例子)