公司自己定义的list类型,我就不贴了,大家替换一下就可以
package com.example.demo.Dao;
import org.apache.poi.hssf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class ExcelUtil {
public static String formatToday(String format) {
format = format != null && format.trim().length() != 0 ? format : "yyyy-MM-dd";
SimpleDateFormat formater = new SimpleDateFormat(format);
return formater.format(new Date());
}
//时间格式化为文件名
public String getExportDate() {
return formatToday("-yyyyMMddHHmmss");
}
/**
* 导出方法
*
* @param list
* @return
*/
public String excel(PagedList list) {
String path = "D:/upload/";
File file = new File(path);
//文件夹路径是否存在,不存在则新建
if (!file.exists()) {
file.mkdirs();
}
//导出路径
String filepath = path + "购买清单" + this.getExportDate() + ".xls";
// 创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("购买清单");
// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//创建列标题
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("订单编号");
cell.setCellStyle(style);
//设置位置、宽度
sheet.setColumnWidth(0, 6000);
cell = row.createCell((short) 1);
cell.setCellValue("订单日期");
cell.setCellStyle(style);
sheet.setColumnWidth(1, 4500);
cell = row.createCell((short) 2);
cell.setCellValue("购买量");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("市场价");
cell.setCellStyle(style);
sheet.setColumnWidth(3, 3500);
cell = row.createCell((short) 4);
cell.setCellValue("执行价");
cell.setCellStyle(style);
sheet.setColumnWidth(4, 3500);
cell = row.createCell((short) 5);
cell.setCellValue("金额");
cell.setCellStyle(style);
sheet.setColumnWidth(5, 3500);
long totalBuyQty = 0;
double totalSkuPrice = 0;
double totalBuyPrice = 0;
double goodsSumAmount = 0;
for (int i = 0; i < list.getRows().size() + 1; i++) {
row = sheet.createRow(i + 1);
//订单列表结束最后一行添加合计
List<Order> order = list.getRows();
//不需要合计可忽略此段
if (list.getRows().size() == i) {
row.createCell((short) 2).setCellValue("总计:");
row.createCell((short) 3).setCellValue("购买量:" + totalBuyQty);
row.createCell((short) 4).setCellValue("金额:" + goodsSumAmount);
row.createCell((short) 5).setCellValue("节省金额:" + (totalSkuPrice - totalBuyPrice));
continue;
}
totalBuyQty += order.get(i).getBuyQty();//获取总购买量
totalSkuPrice += order.get(i).getMaxPrice() * order.get(i).getBuyQty();//获取总市场价
totalBuyPrice += order.get(i).getBuyPrice() * order.get(i).getBuyQty();//获取总购买价格
goodsSumAmount += order.get(i).getGoodsSumAmount();//获取产品总价
// 创建单元格,并设置值
row.createCell((short) 0).setCellValue(order.get(i).getOrderNo());
row.createCell((short) 1).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(order.get(i).getOrderTime()));
row.createCell((short) 2).setCellValue(order.get(i).getBuyQty());
row.createCell((short) 3).setCellValue(order.get(i).getMaxPrice());
row.createCell((short) 4).setCellValue(order.get(i).getBuyPrice());
row.createCell((short) 5).setCellValue(order.get(i).getGoodsSumAmount());
}
// 将文件存到指定位置
try {
FileOutputStream fout = new FileOutputStream(filepath);
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
//返回路径给前端,前端提供下载
String fileUrl = "D:/upload/购买清单" + this.getExportDate() + ".xls";
return fileUrl;
}
}
实体如下:
package com.example.demo.Dao;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* 购买清单实体
*/
public class Order implements Serializable {
private String orderNo;//编号
private Timestamp orderTime;//时间
private long buyQty;//购买数量
private double buyPrice;//购买时价格
private double maxPrice;//市场价
private double goodsSumAmount;//sku价格
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Timestamp getOrderTime() {
return orderTime;
}
public void setOrderTime(Timestamp orderTime) {
this.orderTime = orderTime;
}
public long getBuyQty() {
return buyQty;
}
public void setBuyQty(long buyQty) {
this.buyQty = buyQty;
}
public double getBuyPrice() {
return buyPrice;
}
public void setBuyPrice(double buyPrice) {
this.buyPrice = buyPrice;
}
public double getMaxPrice() {
return maxPrice;
}
public void setMaxPrice(double maxPrice) {
this.maxPrice = maxPrice;
}
public double getGoodsSumAmount() {
return goodsSumAmount;
}
public void setGoodsSumAmount(double goodsSumAmount) {
this.goodsSumAmount = goodsSumAmount;
}
}