导出表格标签:style,sheet,cells,导出,cell,createCell,setCellValue,数据 From: https://www.cnblogs.com/123sougou/p/16810485.html
void export(){
//先从缓存拿数据
// Object commodity = redisUtil.get(String.format(RedisKeyConstant.PRODUCT_INFO, "202112150256", "*"));
Page page = redisUtil.redisPage(new Page(1, 500), String.format(RedisKeyConstant.PRODUCT_INFO, "202112150256", "*"));
// System.out.println(new Gson().toJson(page));
//1.先转换成map
Map map = JSON.parseObject(String.valueOf(new Gson().toJson(page)), Map.class);
//2.取出数组
JSONArray records = (JSONArray) map.get("records");
// 定义一个新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 创建一个Sheet页
XSSFSheet sheet = wb.createSheet("First sheet");
// 创建表头样式
XSSFCellStyle headersStyle = wb.createCellStyle();
// 表头内容对齐⽅式:居中
headersStyle.setAlignment(HorizontalAlignment.CENTER);
//设置行高
sheet.setDefaultRowHeight((short) (2 * 256));
//设置列宽
sheet.setColumnWidth(0, 24000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 6000);
sheet.setColumnWidth(5, 4000);
XSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
//创建样式对象
XSSFCellStyle style = wb.createCellStyle();
//设置样式对齐方式:水平垂直居中
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
//设定背景颜色
style.setFillForegroundColor(IndexedColors.PINK.getIndex());
//设定填充单色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//获得表格第一行
XSSFRow row = sheet.createRow(0);
//根据需要给第一行每一列设置标题
XSSFCell cell = row.createCell(0);
cell.setCellValue("商品名称");
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);
//为指定行设定样式
// row.setRowStyle(style);
//为指定单元格设定样式
// cell.setCellStyle(style);
XSSFRow rows;
XSSFCell cells;
// System.out.println(records);
//循环拿到的数据给所有行每一列设置对应的值
for (int i = 0; i < records.size(); i++) {
Commodity commodity = JSONUtil.toBean(JSONUtil.parseObj(records.get(i)), Commodity.class);
// 在这个sheet页里创建一行
rows = sheet.createRow(i + 1);
String itemName = commodity.getItemName();
String brandName = commodity.getBrandName();
String bid = commodity.getId();
String price = commodity.getSellPrice();
String jdPrice = commodity.getJdPrice();
cells = rows.createCell(0);
cells.setCellValue(itemName);
cells = rows.createCell(1);
cells.setCellValue(brandName);
cells = rows.createCell(2);
cells.setCellValue(bid);
cells = rows.createCell(3);
cells.setCellValue(price);
cells = rows.createCell(4);
cells.setCellValue(jdPrice);
cells = rows.createCell(5);
cells.setCellValue("已上架");
}
try {
String src = "D:/a.xls";
File file = new File(src);
FileOutputStream fileOutputStream = new FileOutputStream(file);
wb.write(fileOutputStream);
wb.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}