利用servlet动态的导出Excel表格
首先,我们需要创建一个实体类,其与servlet中设置的Excel的列名一样,方便后边的操作,需要导入
poi-3.17-beta1.jar
poi-examples-3.17-beta1.jar
poi-excelant-3.17-beta1.jar
poi-ooxml-3.17-beta1.jar
poi-ooxml-schemas-3.17-beta1.jar
poi-scratchpad-3.17-beta1.jar
poi-examples-3.17-beta1.jar
poi-excelant-3.17-beta1.jar
poi-ooxml-3.17-beta1.jar
poi-ooxml-schemas-3.17-beta1.jar
poi-scratchpad-3.17-beta1.jar
这几个jar包
package com.qf.hhh;
/*Excel导出表格*/
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
public class PoiServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("deprecation")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建文档对象
HSSFWorkbook wb=new HSSFWorkbook();//无参数,创建表格,否则,反之
//设置样式
HSSFCellStyle cellStyle=wb.createCellStyle();
//对齐方式 水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//设置填充图案
cellStyle.setFillPattern(HSSFCellStyle.DIAMONDS);
cellStyle.setFillForegroundColor(HSSFColor.CORAL.index);
HSSFFont font=wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short)16);//字体高度
font.setColor(HSSFColor.BLUE_GREY.index);
cellStyle.setFont(font);//将字体设置到cell样式中
//创建sheet
HSSFSheet sheet=wb.createSheet("哈哈哈");
// 第几列,从0开始 宽度256*
sheet.setColumnWidth(0, 256*25);
sheet.setColumnWidth(1, 256*25);
sheet.setColumnWidth(2, 256*25);
sheet.setColumnWidth(3, 256*25);
sheet.setColumnWidth(4, 256*25);
sheet.setColumnWidth(5, 256*25);
//列中创建行
HSSFRow row=sheet.createRow(0);
//创建列
HSSFCell cell= row.createCell(0);
cell.setCellValue("班级成员");//给列设置值
cell.setCellStyle(cellStyle);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 4));
//创建表头行
HSSFRow title= sheet.createRow(2);
HSSFCell cl= title.createCell(0);
cl.setCellStyle(cellStyle);
cl.setCellValue("姓名");
HSSFCell c2= title.createCell(1);
c2.setCellStyle(cellStyle);
c2.setCellValue("班级");
HSSFCell c3= title.createCell(2);
c3.setCellStyle(cellStyle);
c3.setCellValue("性别");
HSSFCell c4= title.createCell(3);
c4.setCellStyle(cellStyle);
c4.setCellValue("年龄");
HSSFCell c5= title.createCell(4);
c5.setCellStyle(cellStyle);
c5.setCellValue("住址");
for(int i=3;i<1000;i++){
HSSFRow rows= sheet.createRow(i);
HSSFCell cc0= rows.createCell(0);
cc0.setCellStyle(cellStyle);
cc0.setCellValue("王大小"+i);
HSSFCell cc1= rows.createCell(1);
cc1.setCellStyle(cellStyle);
cc1.setCellValue("王大小"+i);
HSSFCell cc2= rows.createCell(2);
cc2.setCellStyle(cellStyle);
cc2.setCellValue("王大小"+i);
HSSFCell cc3= rows.createCell(3);
cc3.setCellStyle(cellStyle);
cc3.setCellValue("王大小"+i);
HSSFCell cc4= rows.createCell(4);
cc4.setCellStyle(cellStyle);
cc4.setCellValue("王大小"+i);
}
//设置响应头
ServletOutputStream outputStream= response.getOutputStream();
response.setHeader("content-disposition","attachment;filename=student.xls");
response.setContentType(getServletContext().getMimeType("student.xls"));
wb.write(outputStream);
wb.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}