经过几天的学习终于可以实现java中将查询结果存储为Excel,将页面另存word,将html页面存为pdf格式这几个功能了,这是我的第一篇博客,为了共享代码,也为了以后自己的学习成果不会弄丢,现在将这几个功能贴在网上。
一、将查询结果存为excel,我的毕业设计是超市信息管理系统,一个页面将商品信息展示出来了,现在将结果存为excel
product.jsp页面
this is my excel
*************************************************
//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+"/";
%>
My JSP 'creatExcel.jsp' starting page
<%
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());
%>
*************************************************
//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
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 留在下一篇文章中,所用的包放在附件中