- <input type="button" value=" 导 出 excel " class="button" onFocus="this.blur();" onclick="toExcel()">
-
-
//导出excel
-
function toExcel(){
-
-
location.href = "<%=request.getContextPath()%>/checkedman.do?operFlag=export";
-
- }
- //此处为判断转向
-
if("export".equals(operFlag)) {
-
forward = exportData(request, response);
-
//下面是exportData方法
-
public String exportData(HttpServletRequest request,
-
HttpServletResponse response) {
-
//通过BO层找到获取查询的list
-
ArrayList<CheckedmanPO> list = CheckedmanBO.exportExcel(request, response);
-
-
if (list == null || list.size() <= 0) {
-
-
} else {
-
//HttpServletResponse response = FacesUtils.getServletResponse();
-
String msg = "";
-
response.reset();
-
OutputStream os = null;
-
try {
-
os = response.getOutputStream();
-
-
} catch (IOException e1) {
-
msg = e1.getMessage();
-
}
-
-
response.setContentType("application/vnd.ms-excel;charset=utf-8");
-
response.setCharacterEncoding("utf-8");
-
-
try {
-
response.setHeader("Content-disposition",
-
"attachment; filename="
-
+ new String(
-
"受检者信息.xls".getBytes(),
-
"ISO8859_1"));
-
} catch (Exception e1) {
-
// TODO 自动生成 catch 块
-
e1.printStackTrace();
-
}
-
-
HSSFWorkbook wb = new HSSFWorkbook();
-
HSSFSheet hs = wb.createSheet();
-
-
-
HSSFRow firstRow = hs.createRow(0);
-
-
HSSFCell firstCell0 = firstRow.createCell((short) 0);
-
HSSFCell firstCell1 = firstRow.createCell((short) 1);
-
HSSFCell firstCell2 = firstRow.createCell((short) 2);
-
HSSFCell firstCell3 = firstRow.createCell((short) 3);
-
HSSFCell firstCell4 = firstRow.createCell((short) 4);
-
HSSFCell firstCell5 = firstRow.createCell((short) 5);
-
HSSFCell firstCell6 = firstRow.createCell((short) 6);
-
HSSFCell firstCell7 = firstRow.createCell((short) 7);
-
HSSFCell firstCell8 = firstRow.createCell((short) 8);
-
HSSFCell firstCell9 = firstRow.createCell((short) 9);
-
HSSFCell firstCell10 = firstRow.createCell((short) 10);
-
HSSFCell firstCell11 = firstRow.createCell((short) 11);
-
HSSFCell firstCell12 = firstRow.createCell((short) 12);
-
//以下代码处理数据的中文乱码
-
firstCell0.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell1.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell2.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell3.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell4.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell5.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell6.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell7.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell8.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell9.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell10.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell11.setEncoding(HSSFCell.ENCODING_UTF_16);
-
firstCell12.setEncoding(HSSFCell.ENCODING_UTF_16);
-
-
firstCell0.setCellValue("客户姓名");
-
firstCell1.setCellValue("性别");
-
firstCell2.setCellValue("民族");
-
firstCell3.setCellValue("证件类别");
-
firstCell4.setCellValue("证件号码");
-
firstCell5.setCellValue("出生日期");
-
firstCell6.setCellValue("婚姻状况");
-
firstCell7.setCellValue("手机号码");
-
firstCell8.setCellValue("家族病史");
-
firstCell9.setCellValue("个人病史");
-
firstCell10.setCellValue("药物过敏史");
-
firstCell11.setCellValue("主要体质");
-
firstCell12.setCellValue("辨识日期");
-
-
-
for (int i = 0; i < list.size() ; i++) {
-
HSSFRow oneRow = hs.createRow(i+1);
-
HSSFCell cell0 = oneRow.createCell((short) 0);
-
HSSFCell cell1 = oneRow.createCell((short) 1);
-
HSSFCell cell2 = oneRow.createCell((short) 2);
-
HSSFCell cell3 = oneRow.createCell((short) 3);
-
HSSFCell cell4 = oneRow.createCell((short) 4);
-
HSSFCell cell5 = oneRow.createCell((short) 5);
-
HSSFCell cell6 = oneRow.createCell((short) 6);
-
HSSFCell cell7 = oneRow.createCell((short) 7);
-
HSSFCell cell8 = oneRow.createCell((short) 8);
-
HSSFCell cell9 = oneRow.createCell((short) 9);
-
HSSFCell cell10 = oneRow.createCell((short) 10);
-
HSSFCell cell11 = oneRow.createCell((short) 11);
-
HSSFCell cell12 = oneRow.createCell((short) 12);
-
-
cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell9.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell10.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell11.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell12.setEncoding(HSSFCell.ENCODING_UTF_16);
-
-
-
CheckedmanPO po = (CheckedmanPO) list.get(i);
-
-
cell0.setCellValue(po.getCName());
-
cell1.setCellValue(po.get***());
-
cell2.setCellValue(po.getNationName());
-
cell3.setCellValue(po.getCCardTypeName());
-
cell4.setCellValue(po.getUCardNum());
-
cell5.setCellValue(po.getBirth());
-
cell6.setCellValue(po.getMarriage());
-
cell7.setCellValue(po.getMobileTel());
-
cell8.setCellValue(po.getFamilyIllnessIdList());
-
cell9.setCellValue(po.getIndividualIllnessIdList());
-
cell10.setCellValue(po.getMedicineSensitiveIdList());
-
cell11.setCellValue(po.getMainPhysical());
-
cell12.setCellValue(po.getTestDate());
-
}
-
try {
-
wb.write(os);
-
os.flush();
-
//FacesContext.getCurrentInstance().responseComplete();
-
} catch (IOException e) {
-
msg = msg + e.getMessage();
-
}
-
if (!"".equals(msg)) {
-
}
-
}
-
return null;
- }
关于导出excel,这里使用的是jxl。