HSSFWorkbook的excel表格的导出
Posted 普通网友
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HSSFWorkbook的excel表格的导出相关的知识,希望对你有一定的参考价值。
HSSFWorkbook的excel表格的导出
导入jar包
jar包下载 链接:https://pan.baidu.com/s/1LcaOhETqJTGTTc5O2EaFUA
提取码:bux1
复制这段内容后打开百度网盘手机App,操作更方便哦
jsp页面设置 (jdbc很简单数据库连接我就不写了,只写导出页面吧)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="org.apache.poi.hssf.usermodel.*" %>
<%@ page import="java.io.*" %>
<jsp:directive.page import="db.DB"/>
<jsp:directive.page import="java.sql.ResultSet"/>
<jsp:directive.page import="model.TJiaoshi"/>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
List jiaoshiList=new ArrayList();
String sql="select * from t_jiaoshi where del='no'";
Object[] params=;
DB mydb=new DB();
try
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
while(rs.next())
TJiaoshi jiaoshi=new TJiaoshi();
jiaoshi.setId(rs.getInt("id"));
jiaoshi.setGonghao(rs.getString("gonghao"));
jiaoshi.setXingming(rs.getString("xingming"));
jiaoshi.setXingbie(rs.getString("xingbie"));
jiaoshi.setNianling(rs.getString("nianling"));
jiaoshi.setShengri(rs.getString("shengri"));
jiaoshi.setGongzuo(rs.getString("gongzuo"));
jiaoshi.setZhuanye(rs.getString("zhuanye"));
jiaoshi.setZhaopian(rs.getString("zhaopian"));
jiaoshi.setBeizhu(rs.getString("beizhu"));
jiaoshiList.add(jiaoshi);
rs.close();
catch(Exception e)
e.printStackTrace();
mydb.closed();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("教师号");
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("性别");
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("出生日期");
cell=row.createCell((short)5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("参加工作日期");
cell=row.createCell((short)6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("专业");
cell=row.createCell((short)7);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("备注");
for(int i=0;i<jiaoshiList.size();i++)
TJiaoshi jiaoshi = (TJiaoshi)jiaoshiList.get(i);
row=sheet.createRow(i+1);
cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getGonghao());
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getXingming());
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getXingbie());
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getNianling());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getShengri());
cell=row.createCell((short)5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getGongzuo());
cell=row.createCell((short)6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getZhuanye());
cell=row.createCell((short)7);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(jiaoshi.getBeizhu());
String fujianPath="D:\\\\教师信息.xls";
try
FileOutputStream fileOut = new FileOutputStream(fujianPath);
wb.write(fileOut);
fileOut.close();
catch (Exception e)
e.printStackTrace();
SmartUpload su = new SmartUpload(); // 新建一个SmartUpload对象
su.initialize(pageContext); // 初始化
su.setContentDisposition(null);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,将用acrobat打开
//su.downloadFile("/uploadPath/file/liu.doc"); // 下载英文文件
su.downloadFile(fujianPath, null, new String(java.net.URLDecoder.decode("教师信息.xls","UTF-8").getBytes(), "ISO8859-1")); // 下载中文文件
//downloadFile(String sourceFilePathName, String contentType, String destFileName)
out.clear();
out=pageContext.pushBody();
%>
</body>
</html>
效果图
以上是关于HSSFWorkbook的excel表格的导出的主要内容,如果未能解决你的问题,请参考以下文章
HSSFWorkbook-SXSSFWorkbook导出excel文件获取大小记录
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载