贴一下自己写的关于excel导出商品的类

本文介绍了一种使用Java和POI库实现从数据库获取商品信息并导出到Excel文件的方法。该方法首先创建了一个带有样式设定的工作簿和工作表,然后通过调用服务层获取所有商品信息,并将这些信息填充到Excel表格中。




先是导出的几个方法


import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.sljr.slmall.data.model.product.Product;
import com.sljr.slmall.data.vo.product.ImportProductVo;
import com.sljr.slmall.data.vo.product.ProductVo;
import com.sljr.slmall.product.srv.service.ProductService;


import groovy.lang.TracingInterceptor;
import lombok.extern.slf4j.Slf4j;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by dell、 on 2017/9/12.
 */
@RestController
@Slf4j
public class ExportExcel {


@Autowired
ProductService productService;


/***
* 创建表头

* @param workbook
* @param sheet
*/
private void createTitle(HSSFWorkbook workbook, HSSFSheet sheet) {
HSSFRow row = sheet.createRow(0);
// 设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(0, 16 * 256);
sheet.setColumnWidth(1, 95 * 256);
sheet.setColumnWidth(2, 30 * 256);
sheet.setColumnWidth(3, 12 * 256);
sheet.setColumnWidth(4, 12 * 256);
sheet.setColumnWidth(5, 12 * 256);
sheet.setColumnWidth(6, 12 * 256);
sheet.setColumnWidth(7, 20 * 256);
sheet.setColumnWidth(8, 20 * 256);
sheet.setColumnWidth(9, 20 * 256);
sheet.setColumnWidth(10, 20 * 256);


// 设置为居中加粗
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);


HSSFCell cell;
cell = row.createCell(0);
cell.setCellValue("商品编号(sku)");
cell.setCellStyle(style);


cell = row.createCell(1);
cell.setCellValue("商品名称");
cell.setCellStyle(style);


cell = row.createCell(2);
cell.setCellValue("规格");
cell.setCellStyle(style);


cell = row.createCell(3);
cell.setCellValue("销售价");
cell.setCellStyle(style);


cell = row.createCell(4);
cell.setCellValue("京东价");
cell.setCellStyle(style);


cell = row.createCell(5);
cell.setCellValue("实时价");
cell.setCellStyle(style);


cell = row.createCell(6);
cell.setCellValue("协议价");
cell.setCellStyle(style);


cell = row.createCell(7);
cell.setCellValue("是否上架(京东)");
cell.setCellStyle(style);


cell = row.createCell(8);
cell.setCellValue("是否上架(善林)");
cell.setCellStyle(style);


cell = row.createCell(9);
cell.setCellValue("所属分类");
cell.setCellStyle(style);


cell = row.createCell(10);
cell.setCellValue("所属平台");
cell.setCellStyle(style);


}


@RequestMapping("getExcel")
public void getExcel(HttpServletResponse response) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("商品表");
createTitle(workbook, sheet);
CellStyle center = workbook.createCellStyle();
center.setAlignment(HSSFCellStyle.ALIGN_CENTER);//字体居中
CellStyle left = workbook.createCellStyle();
left.setAlignment(HSSFCellStyle.ALIGN_LEFT);//字体左对齐

List<ImportProductVo> productList = productService.getAllProductIsMarkable();
if (productList == null) {


}
// 新增数据行,并且设置单元格数据
int rowNum = 1;
for (ImportProductVo product : productList) {


HSSFRow row = sheet.createRow(rowNum);

addCell(row, 0, product.getSku(), workbook,center);
addCell(row, 1, product.getName(), workbook,center);
addCell(row, 2, product.getSpec(), workbook,center);
addCell(row, 3, product.getPrice(), workbook,left);
addCell(row, 4, product.getJdPrice(), workbook,left);
addCell(row, 5, product.getRealPrice(), workbook,left);
addCell(row, 6, product.getProtocolPrice(), workbook,left);
addCell(row, 7, product.getIsMarketable(), workbook,center);
addCell(row, 8, product.getIsSlMarketable(), workbook,center);
addCell(row, 9, product.getCategory(), workbook,center);
addCell(row, 10, product.getPlatform(), workbook,center);
rowNum++;
}
OutputStream outputStream = null;
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=product.xls");
outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();


if (outputStream != null) {
outputStream.close();
}
}


public void addCell(HSSFRow row, int column, Object val, HSSFWorkbook wb,CellStyle style) {
Cell cell = row.createCell(column);
try {
if (val == null) {
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
if ((Integer) val == 1 ) {
String a = "是";
cell.setCellValue(a);
}else if ((Integer) val == 0) {
String a = "否";
cell.setCellValue(a);
}else {
cell.setCellValue((Integer) val);
}
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
} else if (val instanceof Float) {
cell.setCellValue((Float) val);
} else if (val instanceof Date) {
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/MM/dd"));
cell.setCellValue((Date) val);
} else if (val instanceof BigDecimal) {
double doubleVal = ((BigDecimal) val).doubleValue();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("¥#,##0.00"));
cell.setCellValue(doubleVal);
}
cell.setCellStyle(style);
} catch (Exception ex) {
log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
cell.setCellValue(val.toString());
}


}


}
【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)》的技术资源,聚焦于电力系统中低碳经济调度问题,结合N-1安全准则与分布鲁棒机会约束(DRCC)方法,提升调度模型在不确定性环境下的鲁棒性和可行性。该资源提供了完整的Matlab代码实现,涵盖建模、优化求解及仿真分析全过程,适用于复杂电力系统调度场景的科研复现与算法验证。文中还列举了大量相关领域的研究主题与代码资源,涉及智能优化算法、机器学习、电力系统管理、路径规划等多个方向,展示了广泛的科研应用支持能力。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源调度、智能电网相关工作的工程师。; 使用场景及目标:①复现高水平期刊(如EI/SCI)关于低碳经济调度的研究成果;②深入理解N-1安全约束与分布鲁棒优化在电力调度中的建模方法;③开展含新能源接入的电力系统不确定性优化研究;④为科研项目、论文撰或工程应用提供可运行的算法原型和技术支撑。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码与案例数据,按照目录顺序逐步学习,并重点理解DRCC建模思想与Matlab/YALMIP/CPLEX等工具的集成使用方式,同时可参考文中列出的同研究方向拓展研究思路。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值