package com.gxuwz.core.util;
import java.io.IOException;
import java.io.OutputStream;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 使用方法:1.先执行带参数的构造函数:public Excle(String sheetName);
* 2.添加头标题addTitle(String[] title);
* 3.添加数据:addData(String[] title);
* 4.设置响应头让浏览器下载:export();
*/
public class Excle {
HSSFWorkbook wb;
HSSFSheet sheet;
HSSFCellStyle style;
public Excle() {
}
public Excle(String sheetName) {
wb = new HSSFWorkbook();
sheet = wb.createSheet(sheetName);
style = wb.createCellStyle();
}
/**
* 添加标题
*/
public void addTitle(String[] title) {
HSSFRow row = sheet.createRow((int) 0);
for (int i = 0; i < title.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
sheet.autoSizeColumn(i); // 自动调整列宽
}
}
/**
* r:行 l:列 data:添加的数据
* @param r
* @param l
* @param data String
*/
public void addData(int r, int l, String data) {
sheet.createRow(r + 1).createCell(l).setCellValue(data);
}
/**
* r行数从0开始,l列数从1开始
* @param r
* @param data
*/
public void addData(Integer r, String... data) {
HSSFRow row = sheet.createRow((int) r + 1);
for (int i = 0; i < data.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(data[i]);
}
}
/**
* @param r
* @param l
* @param data Integer
*/
public void addData(int r, int l, Integer data) {
sheet.createRow(r + 1).createCell(l).setCellValue(data);
}
/**
* @param r
* @param l
* @param data double
*/
public void addData(int r, int l, double data) {
sheet.createRow(r + 1).createCell(l).setCellValue(data);
}
/**
* @param r
* @param l
* @param data float
*/
public void addData(int r, int l, float data) {
sheet.createRow(r + 1).createCell(l).setCellValue(data);
}
/**
* 设置响应头返回浏览器下载
* @param request
* @param response
* @throws IOException
*/
public void exportExcle(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");// setContentType
DateUtil dateUtil = new DateUtil();
response.setHeader("Content-disposition", "attachment;filename="+dateUtil.createNumber()+".xls");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush(); // 清空
ouputStream.close();
}
public HSSFWorkbook getWb() {
return wb;
}
public void setWb(HSSFWorkbook wb) {
this.wb = wb;
}
public HSSFSheet getSheet() {
return sheet;
}
public void setSheet(HSSFSheet sheet) {
this.sheet = sheet;
}
public HSSFCellStyle getStyle() {
return style;
}
public void setStyle(HSSFCellStyle style) {
this.style = style;
}
}
实现部分
public String export() throws IOException {
Excle excle = new Excle("商品信息");
List<CommodityDetails> comDetailList = commodityService.exportExcle(type);
//导出表格的字段名称
String title[] = {"商品编号","商品名称","商品简称","商品类型","规格","价类","品牌系类","批发价格","建议零销售(元)","销量"};
excle.addTitle(title);
for (int i = 0; i < comDetailList.size();i++) {
excle.addData(i, comDetailList.get(i).getCommodityNumber(),comDetailList.get(i).getCommodityName(),
comDetailList.get(i).getCommoditySimpleName(), comDetailList.get(i).getClassType(),
comDetailList.get(i).getSpecifications(), comDetailList.get(i).getPriceType(),
comDetailList.get(i).getBrandSeries(), comDetailList.get(i).getWholesalePrice()+"",
comDetailList.get(i).getRetailPrice()+"", comDetailList.get(i).getSalesVolume()+"");
}
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
excle.exportExcle(request, response);
return null;
}