excel文件导出demo

前端html代码

<div style="width: 1230px;">
	<form action="" id="exportForm" method="post">
		<table id="QueryCondition" cellpadding="0" cellspacing="0" border="0" width="100%" class="ui-table">
			<!-- 第一行 -->
       		<tr>
            	<!-- 学生id -->
                <th width="13%">学生id:</th>
                <th width="1%"></th>
                <td width="20%">
                	<input style="width: 120px;" id="studentId" type="text" maxlength=20>
                </td>
                <!-- 联系电话 -->
                <th width="13%">联系电话:</th>
                <th width="1%"></th>
                <td width="20%">
                	<input style="width: 120px;" id="phone" type="text" maxlength=20>
                </td>
                <!-- 学生姓名 -->
                <th width="13%">学生姓名:</th>
                <th width="1%"></th>
                <td width="20%">
                	<input style="width: 120px;" id="studenName" type="text" maxlength=20>
                </td>
           	</tr>
           	<!-- 第二行-->
            <tr class="ui-function-box ui-pager">
            	<td colspan="7"></td>
                <td  colspan = "2">
                	<div style="text-align: right;">
						<!-- 导出 -->
						<shiro:hasPermission name="stuManage:stuManage:export">
					    	<input type="button" id="export" class="ui-btn ui-btn ui-btn-export" onclick="exportData()" value="导出" />
					   </shiro:hasPermission>
   					</div>
   				</td>
			</tr>
		</table>
	</form> 
</div>
<div id="div-gridview" style="margin-top: 0px;">
	<table id="div-list" id="list4">
    </table>
    <div id="div-pager" style="overflow: visible; border-top-width: 0;" class="ui-function-box ui-pager">
    </div>
</div>

前端js代码

/*导出*/
function exportData() {
	// 获取tr的长度
    if ($("#div-list tr").length == 1) {
    	// layer.msg("")为layer弹出层框架的方法
    	layer.msg("暂无导出数据!");
    	return;
    }
	var studentId = $("#studentId").val();      // 学生id
    var phone = $("#phone").val();              // 联系电话
    var studentName = $("#studentName").val();  // 学生姓名
    studentName = encodeURI(studentName);       // 此处需要转码两次
    studentName = encodeURI(studentName);
    var url="${ctx}/stuManage/stuManage/exportData?" +
    		"studentId="+ studentId+ "&&" +
            "phone="+ phone + "&&" +
            "studentName="+ studentName;
    // 修改form标签的属性action的值
	$("#exportForm").attr("action",url);
	// 提交
	$("#exportForm").submit();
}
</script>

后端Java代码

	@RequestMapping("exportData")
	public void exportData(Student param, HttpServletRequest request,HttpServletResponse response, Model model) throws UnsupportedEncodingException {
		// 设置字符集
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 导出弹出的确定保存提示框
		response.setContentType("application/x-download");

		// 设置导出excle文件名
		String fileName = "student" + DateFormatUtils.format(new Date(), "yyyyMMdd") + DateFormatUtils.format(new Date(), "HHmmss") + ".xls";
		response.addHeader("Content-Disposition", "attachment;filename="+ fileName);

		// 第一步创建workbook对象
		XSSFWorkbook wb = new XSSFWorkbook();
		
		// 第二步创建表sheet
		XSSFSheet sheet = wb.createSheet("学员列表");
		// 设置表的列宽
		// 1.学生id
		sheet.setColumnWidth(0, 100 * 50);
		// 2.联系电话
		sheet.setColumnWidth(1, 150 * 50);
		// 3.学生姓名
		sheet.setColumnWidth(2, 100 * 50);
		
		// 第三步创建行row:添加表头0行
		XSSFRow row = sheet.createRow(0);
		XSSFCellStyle style = wb.createCellStyle();
		// 居中
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 第四步创建单元格
		// 1.学生id
		XSSFCell cell = row.createCell(0);
		cell.setCellValue("学生id");
		cell.setCellStyle(style);
		// 2.联系电话
		cell = row.createCell(1);
		cell.setCellValue("联系电话");
		cell.setCellStyle(style);
		// 2.学生姓名
		cell = row.createCell(2);
		cell.setCellValue("学生姓名");
		cell.setCellStyle(style);

		// 获取查询列表信息
		List<Student> resultLsit = this.ccStudentService.obtainList(param);
		// 实例化临时接收变量
		Student temp = new Student();

		// 循环取出查询的列表信息并向excel表里赋值
		for (int i = 0; i < resultLsit.size(); i++) {
			// 临时接收变量
			temp = resultLsit.get(i);
			// 创建行(第一行开始,0行为表头)
			row = sheet.createRow(i + 1);
			// 创建单元格并且添加数据
			// 1.学生id
			row.createCell(0).setCellValue(temp.getStudentId());
			// 2.联系电话
			row.createCell(1).setCellValue(temp.getPhone());
			// 3.学生姓名
			row.createCell(2).setCellValue(temp.getStudentName());
		}
		// 第六步将生成excel文件保存到指定路径下
		try {
			// 输出流
			OutputStream out = response.getOutputStream();
			// 写
			wb.write(out);
			// 关闭流
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值