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>

效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值