java实现导出excel、word、 pdf
经过几天的学习终于可以实现java中将查询结果存储为Excel,将页面另存word,将html页面存为pdf格式这几个功能了,这是我的第一篇博客,为了共享代码,也为了以后自己的学习成果不会弄丢,现在将这几个功能贴在网上。
一、将查询结果存为excel,我的毕业设计是超市信息管理系统,一个页面将商品信息展示出来了,现在将结果存为excel
product.jsp页面
<html>
<title>this is my excel</title>
<head>
<script type="text/javascript">
function genExcel(){
window.open("productManage/creatExcel.jsp");
}
function genWord(){
window.open("productManage/creatWord.jsp");
}
function genPdf(){
window.open("productManage/creatPdf.jsp");
}
</script>
</head>
<body>
<form>
<input type="button" onclick="genExcel()" value="生成excel">
<input type="button" onclick="genWord()" value="生成word">
<input type="button" onclick="genPdf()" value="生成pdf">
</form>
</body>
</html>
*************************************************
//creatExcel.jsp
<%@page import="com.bean.Productbean"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.dao.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘creatExcel.jsp‘ starting page</title>
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%
response.setHeader("Content-Disposition", "attachment;filename=product.xls");
response.setContentType("application/vnd.ms-excel");
CreatExcel ce = new CreatExcel();
out.clear() ;
out = pageContext.pushBody();
ce.getExcel("product.xls",response.getOutputStream());
%>
</head>
<body>
</body>
</html>
*************************************************
//CreatExcel.java
package com.dao;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.bean.Productbean;
public class CreatExcel {
public void getExcel(String sheetname,OutputStream output){
int iPage = 1;
int iPageSize = 10000;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("sheet1");
HSSFCellStyle setBorder = wb.createCellStyle();
//居中设置
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFRow row = sheet1.createRow((short) 0);
//设置单元格的宽
sheet1.setColumnWidth(0, 10 * 256);
sheet1.setColumnWidth(1, 10 * 256);
sheet1.setColumnWidth(2, 10 * 256);
sheet1.setColumnWidth(3, 10 * 256);
sheet1.setColumnWidth(4, 10 * 256);
sheet1.setColumnWidth(5, 10 * 256);
sheet1.setColumnWidth(6, 10 * 256);
sheet1.setColumnWidth(7, 10 * 256);
sheet1.setColumnWidth(8, 10 * 256);
HSSFCell cell = row.createCell(0);
row.createCell(0).setCellValue("商品编号");
row.createCell(1).setCellValue("商品名称");
row.createCell(2).setCellValue("进价");
row.createCell(3).setCellValue("出售");
row.createCell(4).setCellValue("所属种类");
row.createCell(5).setCellValue("单位");
row.createCell(6).setCellValue("库存");
row.createCell(7).setCellValue("产地");
ProductDao pd = new ProductDao();
ArrayList plist = pd.getAllPro(-1);
if(plist.size()>0){
for(int i=0;i<plist.size()-1;i++){
row = sheet1.createRow(i + 1);
row.createCell(0).setCellValue(((Productbean)(plist.get(i))).getPro_id()+"");
row.createCell(1).setCellValue(((Productbean)(plist.get(i))).getPro_name()+"");
row.createCell(2).setCellValue(((Productbean)(plist.get(i))).getC_price()+"");
row.createCell(3).setCellValue(((Productbean)(plist.get(i))).getPrice()+"");
row.createCell(4).setCellValue(((Productbean)(plist.get(i))).getPro_varid()+"");
row.createCell(5).setCellValue(((Productbean)(plist.get(i))).getPro_unit()+"");
row.createCell(6).setCellValue(((Productbean)(plist.get(i))).getPro_count()+"");
row.createCell(7).setCellValue(((Productbean)(plist.get(i))).getPro_field()+"");
}
}
try {
output.flush();
wb.write(output);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
***********************************************
下面生成word pdf 留在下一篇文章中,所用的包放在附件中
- jar包.rar (7.1 MB)
- 下载次数: 4
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。