用JAVA代碼來導出EXCEL – JAVA編程語言程序開發技術文章

有些情況下我們需要將數據導出到excel中

 

例子: 根據一個班級ID  來導出全班學生的成績

 

兩個實體類

 

班級實體類

Class.java

 

public class Class {
 private int classId;//班級ID
 private String className;//班級名稱
 。。。

 }
}

 

學生實體類

Student.java

public class Student {
 private int stuId;//學號
 private String stuName;//姓名
 private double scode;//得分
 private String classID;//班級ID

 。。。

}

 

導出excel方法(這裡是struts框架的Action中的方法 可以直接調用保存excel)

public ActionForward exportExcel(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  //假設掉用方法時 傳入班級編號

   String classId= request.getParameter("id");
   Class class= classManager.getClassById(classId);//此方法自行實現s
   if (class== null)
    return null;
   List<Student> list = stuManager .getStuListByClassId(classId);//此方法自行實現

   String[] columnHeads = { "學號", "姓名", "得分"};
   int columnsize = columnHeads.length, rowsize = list.size(), rowindex = 0;
   // 創建一個HSSFWorbook對象s
   HSSFWorkbook hWorkbook = new HSSFWorkbook();

   // 創建一個HSSFSheet對象(相當於作業本的某一頁)
   HSSFSheet hSheet = hWorkbook.createSheet();
   // 創建行(此行作為頭)—————-創建頭開始
   HSSFRow hRow = hSheet.createRow(rowindex++);
   //創建單元格(第一(0)個)

   HSSFCell hCell = hRow.createCell((short) 0);

   //設置字符集
   hCell.setEncoding(HSSFCell.ENCODING_UTF_16);

   //調用getCellStyle()方法獲得一個格式對象
   HSSFCellStyle cellStyle = getCellStyle(hWorkbook, (short) 320,(short) 700);

   //將上面獲得的格式對象給對應單元格

   hCell.setCellStyle(cellStyle);

   hCell.setCellValue("YYS學校"+class.getClassName()+"班成績單"); 

   //批量創建單元格———–到後面合並單元格
   for (int i = 1; i < columnsize; i++) {
    hCell = hRow.createCell((short) i);
    hCell.setCellStyle(cellStyle);
   }

   // 創建行—————-創建頭結束

 

   //創建表頭  ( 學號   姓名   得分 )

   hRow = hSheet.createRow(rowindex++);
   cellStyle = getCellStyle(hWorkbook, (short) 220, (short) 600);
   for (int i = 0; i < columnsize; i++) {
    hCell = hRow.createCell((short) i);
    hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    hCell.setCellValue(columnHeads[i]);
    hCell.setCellStyle(cellStyle);
   }

 

   //遍歷所有學生
   cellStyle = getCellStyle(hWorkbook, (short) 220, (short) 500);//設置單元格格式

   for (int i = 0, j = 0; i < rowsize; i++, j = 0) {
    hRow = hSheet.createRow(rowindex++);
    Student stu =  list.get(i);
    // 學號
    hCell = hRow.createCell((short) j);
    hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    hCell.setCellValue(stu.getStuId());
    hCell.setCellStyle(cellStyle);
    hSheet.setColumnWidth((short) j++, (short) 2000);

    // 姓名
    hCell = hRow.createCell((short) j);
    hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    hCell.setCellValue(stu.getStuName());
    hCell.setCellStyle(cellStyle);
    hSheet.setColumnWidth((short) j++, (short) 3000);

    // 得分

    hCell = hRow.createCell((short) j);
    hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    hCell.setCellValue(stu.getScode());
    hCell.setCellStyle(cellStyle);
    hSheet.setColumnWidth((short) j++, (short) 3000);

   }

 

   //合並單元格 四個參數的意思是(X1,Y1,X2,Y2)
   hSheet.addMergedRegion(new Region(0, (short) 0, 0,
     (short) (columnsize – 1)));
  

   //固定表頭(前一個參數代表列,後一個參數單表行)
   hSheet.createFreezePane(0, 1);

   response.setContentType("application/x-download");

   String filename = URLEncoder.encode(class.getClassName()+ ".xls", "UTF-8");

   response.addHeader("Content-Disposition", "attachment;filename="+ filename);
   response.setContentType("application/msexcel"); // 定義輸出類型
   response.setContentType("UTF-8");
   hWorkbook.write(response.getOutputStream());
   response.getOutputStream().flush();
   response.getOutputStream().close();
  }
  return null;
 }

 

 

//設置樣式方法 

public HSSFCellStyle getCellStyle(HSSFWorkbook hWorkbook, short fontHeight,
   short boldWeight) {
  HSSFCellStyle cellStyle = hWorkbook.createCellStyle();
  HSSFFont font = hWorkbook.createFont();
  cellStyle = hWorkbook.createCellStyle();
  cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
  cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  font = hWorkbook.createFont();
  font.setFontHeight(fontHeight);
  font.setBoldweight(boldWeight);
  font.setFontName("宋體");
  cellStyle.setFont(font);
  cellStyle.setWrapText(true);
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER)

  return cellStyle;
 }

 

摘自 xuliangwen的專欄

發佈留言