贴一下自己写的关于excel导出商品的类
来源:互联网 发布:飞歌导航 高德 端口 编辑:程序博客网 时间:2024/05/19 02:26
先是导出的几个方法
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());
}
}
}
阅读全文
0 0
- 贴一下自己写的关于excel导出商品的类
- 自己写的java excel导出工具类
- C#导出Excel自己写的小函数
- 自己写的Excel
- 自己写的一个导出的程序这里主要用到一下程序
- 自己写的数据库链接类,请高手指点一下。
- 自己写的数据库pdo操作类,显摆一下
- 谈一下自己写论文的经验
- 开始写一下自己的博客
- java excel导出 自己做的实例
- 以后自己用的Excel批量导出
- 用thinkphp写的导出excel示例
- 自己写的导出CSV文件
- 写一些关于自己的
- 优化了一下Gridview导出EXCEL的代码,现在导出的Excel是正确的格式
- 自己写的excel导入sqlserver函数
- 自己写的excel导入sqlserver函数
- 自己写的excel导入sqlserver函数
- Java显示星星
- GIS数据的查找,插入,删除,更新(ArcEngine)
- keycode键盘按钮
- 编码由来的历史
- matlab牛顿插值法
- 贴一下自己写的关于excel导出商品的类
- 状压dp——洛谷P3112 [USACO14DEC]后卫马克Guard Mark
- 第二周项目--程序的多文件组织
- 数字图像分类
- bzoj3990: [SDOI2015]排序
- 常用的类包
- 欢迎使用CSDN-markdown编辑器
- node服务器如何生成有logo和背景的带参数二维码
- bean 的生命周期和后处理器