基于POI的XSSFWorkbook的excel生成 您所在的位置:网站首页 Java生成execl文件内容生僻字乱码 基于POI的XSSFWorkbook的excel生成

基于POI的XSSFWorkbook的excel生成

2023-10-21 11:25| 来源: 网络整理| 查看: 265

前言

最近在工作中遇到要导出excel的情况,在写完导出excel之后记录一下用到的方法。

一、所用的依赖

我在做导出excel的时候用到了POI这个依赖,下边为依赖坐标.

org.apache.poi poi-ooxml 3.17 org.apache.poi poi 3.17 org.apache.poi poi-ooxml-schemas 3.17

在引入依赖之后我选择了XSSFWorkbook这个实现类用来导出excel。

二、导出excel的基本流程

  首先要理解生成excel的流程。   要先创建一个工作簿,有了工作簿之后就可以创建工作表,一个工作表中再去创建工作行,有了行之后就可以在单元格中绘制数据了。   工作簿:XSSFWorkbook   工作表:XSSFSheet   工作行:XSSFRow   单元格:XSSFCell

三、用到的API //新建表格 XSSFWorkbook wb = new XSSFWorkbook(); //创建工作表sheet页 XSSFSheet sheet = wb.createSheet("sheet1"); sheet.setDefaultRowHeight((short) (2 * 256));// 设置行高 sheet.setColumnWidth(0, 4000);// 设置列宽 //表格样式 XSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(BorderStyle.THIN);//下边框 style.setBorderLeft(BorderStyle.THIN);//左边框 style.setBorderTop(BorderStyle.THIN);//上边框 style.setBorderRight(BorderStyle.THIN);//右边框 style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //设置填充方案 style.setFillForegroundColor(new XSSFColor(new Color(100,149,237))); //设置填充颜色 title.setAlignment(HorizontalAlignment.CENTER);//设置左右居中 title.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中 //设置字体 XSSFFont font = wb.createFont(); font.setFontName("宋体");//字体样式 font.setFontHeightInPoints((short) 16);//字体大小 //合并单元格 CellRangeAddress cal = new CellRangeAddress(0,0,0,1);//参数含义:起始列,结束列,起始行,结束行。 sheet.addMergedRegion(cal); //注意在合并单元格后对表格添加样式会发现样式不能添加需要添加一下代码 RegionUtil.setBorderBottom(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderTop(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderLeft(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderRight(BorderStyle.THIN, cal, sheet); 四代码示例

代码如下(示例):

public void exprot(HttpServletRequest request, HttpServletResponse response,List list) { try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); String fileName ="导出excel"; // 第一步:定义一个新的工作簿 XSSFWorkbook wb = new XSSFWorkbook(); // 第二步:创建一个Sheet页 response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1") + ".xlsx"); XSSFSheet sheet = wb.createSheet("sheet1"); sheet.setDefaultRowHeight((short) (2 * 256));// 设置行高 sheet.setColumnWidth(0, 3000); sheet.setColumnWidth(1, 15000);// 设置列宽 sheet.setColumnWidth(2, 3000); //设置样式 XSSFCellStyle title = wb.createCellStyle(); title.setBorderBottom(BorderStyle.THIN);//设置边框 title.setBorderLeft(BorderStyle.THIN); title.setBorderTop(BorderStyle.THIN); title.setBorderRight(BorderStyle.THIN); title.setFillPattern(FillPatternType.SOLID_FOREGROUND); //设置填充方案 title.setFillForegroundColor(new XSSFColor(new Color(100,149,237))); //设置填充颜色 title.setAlignment(HorizontalAlignment.CENTER);//设置左右居中 title.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中 //设置字体 XSSFFont font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 16); //创建行 XSSFRow row3 = sheet.createRow(0); //创建列 XSSFCell cell3 = row3.createCell(0); cell3.setCellValue("导出excel"); CellRangeAddress cal = new CellRangeAddress(0,0,0,2);//合并单元格 sheet.addMergedRegion(cal); bord(row3,title,0); RegionUtil.setBorderBottom(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderTop(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderLeft(BorderStyle.THIN, cal, sheet); RegionUtil.setBorderRight(BorderStyle.THIN, cal, sheet);//解决单元格合并后没有样式问题 XSSFRow row4 = sheet.getRow(0); row4.setHeightInPoints(30);//设置高度 XSSFRow row = sheet.createRow(1); XSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); cell = row.createCell(1); cell.setCellValue("年龄"); cell = row.createCell(2); cell.setCellValue("地址"); for (int j = 0; j // TODO Auto-generated catch block e.printStackTrace(); } } public static void bord(Row row,XSSFCellStyle cellStyle,int j){ for (int i = 0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有