Java导出

servlet 层:
第一步:查询要导出的数据
List exportList = this.commodityService.selectForExport(参数);

第二步:导出时间格式
SimpleDateFormat birthdayFormat = new SimpleDateFormat(“yyyy-MM-dd”);
//导出时间的格式化
SimpleDateFormat createFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
//导出时间
Date exportDate = new Date();

第三步:使用POI导出到excel
1-创建工作簿
SXSSFWorkbook workbook = new SXSSFWorkbook(100);//保存在内存中,直到刷新的行数。
2-创建工作表对象
SXSSFSheet sheet = workbook.createSheet(“商品信息”);
3-写入excel的标题
3.1-创建第一行为标题行
SXSSFRow titleRow = sheet.createRow(0);
3.2-设置行高使用HSSFRow对象的setHeight和setHeightInPoints方法,(这两个方法的区别在于setHeightInPoints的单位是点,而setHeight的单位是1/20个点)
titleRow.setHeightInPoints(30.0f);
3.3-合并列 第一步:创建单元格
SXSSFCell cell = titleRow.createCell(0);//序号从0开始
第二步:指定单元格合并范围
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 13);
sheet.addMergedRegion(region);
3.4-合并单元格
cell.setCellValue(“商品数据 (” + createFormat.format(exportDate) + “)”);
//单元格样式
CellStyle titleStyle = workbook.createCellStyle();

    titleStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中

    titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 
    Font titleFont = workbook.createFont();
    titleFont.setFontHeightInPoints((short) 24);//字体大小
    titleStyle.setFont(titleFont);//字体
    titleStyle.setBorderLeft(BorderStyle.THIN);//边框 左 
    titleStyle.setBorderTop(BorderStyle.THIN);//边框 上
    titleStyle.setBorderRight(BorderStyle.THIN);//边框 右
    titleStyle.setBorderBottom(BorderStyle.THIN);//边框 下
  3.5-把样式设置给单元格
    titleRow.getCell(0).setCellStyle(titleStyle);

4-表头
SXSSFRow headerNameRow = sheet.createRow(1);
headerNameRow.createCell(0).setCellValue(“序号”);

headerNameRow.createCell(13).setCellValue(“备注”);
4.1-表头样式
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setBorderLeft(BorderStyle.THIN);//边框 左
headerStyle.setBorderTop(BorderStyle.THIN);//边框 上
headerStyle.setBorderRight(BorderStyle.THIN);//边框 右
headerStyle.setBorderBottom(BorderStyle.THIN);//边框 下

    headerStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());//背景颜色
    headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    Font headerFont = workbook.createFont();
    headerFont.setBold(true);//加粗
    headerStyle.setFont(headerFont);

4.2-通过循环给表头设置样式 getPhysicalNumberOfCells()获取单元格个数
for (int i = 0; i < headerNameRow.getPhysicalNumberOfCells(); i++) {
headerNameRow.getCell(i).setCellStyle(headerStyle);
}

5-写数据
if (exportList != null && exportList.size() > 0) {
5.1-数据部分 样式
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setBorderLeft(BorderStyle.THIN);//边框 左
dateStyle.setBorderTop(BorderStyle.THIN);//边框 上
dateStyle.setBorderRight(BorderStyle.THIN);//边框 右
dateStyle.setBorderBottom(BorderStyle.THIN);//边框 下
dateStyle.setAlignment(HorizontalAlignment.LEFT);//左对齐

        for (int i = 0; i < exportList.size(); i++) {
         CommodityTableVo commodityTableVo = exportList.get(i);
              Row rowNow = sheet.createRow(2 + i);//第一行标题,第二行是表头

5-2设置值
rowNow.createCell(0).setCellValue(i + 1);//序号
rowNow.createCell(1).setCellValue(commodityTableVo.getCommodityName());//商品名称

rowNow.createCell(13).setCellValue(commodityTableVo.getRemark());//备注

5-3设置样式
for (int j = 0; j < rowNow.getPhysicalNumberOfCells(); j++) {
rowNow.getCell(j).setCellStyle(dateStyle);
}
}
5.4-设置自动列宽 如果使了SXSSF,需要把所有数据加载到内存,数据比较多时容易内存条溢出 sheet.trackAllColumnsForAutoSizing();//缓存所有数据 for 自动宽度
for (int i = 0; i < headerNameRow.getPhysicalNumberOfCells(); i++) {
sheet.autoSizeColumn(i);//设置自动列宽
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10);
}

6-输出
SimpleDateFormat fileFormat = new SimpleDateFormat(“yyyyMMddHHmmss”);
String lastFileName = “商品数据” + fileFormat.format(new Date()) + “.xlsx”;//文件名
response.setContentType(“application/msexcel;charset=UTF-8”);//指定xlsx的MIME类型
response.setHeader(“Content-Disposition”, “attachment; filename=” + URLEncoder.encode(lastFileName, “UTF-8”));//指定文件下载名称

   OutputStream out = response.getOutputStream();//获取输出流
   workbook.write(out);//把工作簿写到输出流
   out.flush();
   out.close();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值