使用POI导出考勤数据

要求表明:

应用POI导出来考勤管理数据信息,储存为xls格式(Excel文本文档)

POI详细介绍:

Apache POI是开源软件,提供API给Java程序流程对Microsoft Office格式档案读和写的功能

包表明:

HSSF提供读写Microsoft Excel XLS格式档案的功能。

XSSF提供读写Microsoft Excel OOXML XLSX格式档案的功能。

HWPF提供读写Microsoft Word DOC格式档案的功能。

HSLF提供读写Microsoft PowerPoint格式档案的功能。

常见API:

HSSFWorkbook 工作簿文本文档

HSSFSheet 工作表

HSSFRow 行

HSSFCell 表格中

HSSFCellStyle 单元格款式

编码完成:

1.将poi-3.9-20121203.jar导到WEB-INF/lib下

poi-3.9-20121203.jar

2.查看出的考勤管理数据信息如下图

3.“导出来”按键编码

<input name="" type="button" id= "btn2" class="scbtn2" value="导出来"/>

4.“导出”按键关联点击事情编码

<script type="text/javascript"> $(function(){ //给导出来按键关联点击事情 $("#btn2").click(function(){ //获得三个查询条件的值 var empId = $("#empId").val();//职工序号 var deptno = $("#deptno").val();//单位序号 var dtDate = $("#dtDate").val();//考勤管理日期 //浏览选定的Servlet,不应用Ajax(由于Ajax是根据调用函数处置结果的,导出来XLS是立即回到流,因此都不应用分享和跳转) location.href="DutyServlet?method=exportXls&empId=" empId "&deptno=" deptno "&dtDate=" dtDate; }); });</script>

5.后台管理编码

@WebServlet("/DutyServlet")public class DutyServlet extends BaseServlet { private static final long serialVersionUID = 1L; public void exportXls(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得三个查询条件 String empId = request.getParameter("empId"); String sdeptno = request.getParameter("deptno"); int deptno = 0; try{ deptno = Integer.parseInt(sdeptno); }catch(NumberFor ** tException e){ e.printStackTrace(); } String sdtDate = request.getParameter("dtDate"); java.sql.Date dtDate = null; try{ dtDate = java.sql.Date.valueOf(sdtDate); }catch(RuntimeException e){ e.printStackTrace(); } //启用业务流程层进行查看实际操作 DutyService dutyService = new DutyServiceImpl(); List<Duty> dutyList = dutyService.findDuty(empId,deptno,dtDate); //回到outputStream createExcel(dutyList,response); } private static void createExcel(List<Duty> list,HttpServletResponse response) { // 建立一个Excel文档 HSSFWorkbook workbook = new HSSFWorkbook(); // 建立一个工作表 HSSFSheet sheet = workbook.createSheet("考勤管理信息内容"); CellRangeAddress region = new CellRangeAddress(0, // first row 0, // last row 0, // first column 5// last column ); sheet.addMergedRegion(region); HSSFRow hssfRow = sheet.createRow(0); HSSFCell headCell = hssfRow.createCell(0); headCell.setCellValue("员工考勤信息"); // 设定表格中格式垂直居中 HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headCell.setCellStyle(cellStyle); // 加上表头行 hssfRow = sheet.createRow(1); // 添加表头內容 headCell = hssfRow.createCell(0); headCell.setCellValue("登录名"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(1); headCell.setCellValue("真实身份"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(2); headCell.setCellValue("隶属单位"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(3); headCell.setCellValue("出勤率日期"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(4); headCell.setCellValue("每日签到時间"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(5); headCell.setCellValue("签退时间"); headCell.setCellStyle(cellStyle); // 加上数据信息內容 for (int i = 0; i < list.size(); i ) { hssfRow = sheet.createRow((int) i 2); Duty duty = list.get(i); // 建立表格中,并设定值 HSSFCell cell = hssfRow.createCell(0); cell.setCellValue(duty.getEmp().getEmpId()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(1); cell.setCellValue(duty.getEmp().getRealName()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(2); cell.setCellValue(duty.getEmp().getDept().getDeptName()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(3); // 日期格式变为字符串数组輸出 SimpleDateFor ** t sdf = new SimpleDateFor ** t("yyyy-MM-dd"); String time= sdf.for ** t(duty.getDtDate()); cell.setCellValue(time); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(4); cell.setCellValue(duty.getSigninTime()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(5); cell.setCellValue(duty.getSignoutTime()); cell.setCellStyle(cellStyle); } // 储存Excel文档 try { response.setContentType("application/vnd.ms-excel");//返回的是Excel文档 response.setHeader("Content-disposition", "attachment;filename=duty.xls");//附件形式下载,文件名叫duty.xls //OutputStream outputStream = new FileOutputStream("D:/duty.xls");//保存到本地(服务器端) OutputStream outputStream = response.getOutputStream(); //写到客户端 workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } }} 点击导出按钮打开文档

加我们 免费用

源码出售 支持二开

立即获取免费试用

Copyright © All Rights Reserved 皖ICP备2021007790号-4

立即咨询