JAVA导出查询结果到Excel并合并单元格

前端页面

 后端代码

 @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request,  HttpServletResponse response) throws UnsupportedEncodingException {
       List<StuRank> stuList =scoreService.stuRankList();
       List<String> headList = new ArrayList<String>();
        headList.add("姓名");
        headList.add("科目");
        headList.add("分数");
        headList.add("总分");
        headList.add("排名");
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("学生成绩");
        //表头样式
        XSSFFont headFont = workbook.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBold(true);
        headFont.setFontHeightInPoints((short) 13);
        XSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setFont(headFont);// 设置字体css
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 竖向居中
        headStyle.setAlignment(HorizontalAlignment.CENTER);      // 横向居中
         headStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //数据样式
        XSSFFont font = workbook.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 11);
        XSSFCellStyle style = workbook.createCellStyle();
        style.setFont(font); // 设置字体css
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 竖向居中
        style.setAlignment(HorizontalAlignment.CENTER); // 横向居中
        //创建Excel行和单元格
        for (int i = 0; i < stuList.size() + 2; i++)
        {
            XSSFRow newRow = sheet.createRow(i);
            for (int j = 0; j <headList.size()+1; j++)
            {
                newRow.createCell(j);
            }
        }
        //设置Excel表头
        for (int i = 0; i < headList.size(); i++)
        {
            sheet.addMergedRegion(new CellRangeAddress(0, 1, i, i)); //起始行  结束行 起始列 结束列
            sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
            sheet.setColumnWidth(i,4000);//设置列宽
            sheet.getRow(0).getCell(i).setCellValue(headList.get(i));//具体课程
            sheet.getRow(0).getCell(i).setCellStyle(headStyle);
        }
        //导入数据
        List<Stuinfo> stuNameList=stuinfoService.findStuinfo();
        int startRow=2;
        int endRow=1;
        for (int i = 0; i < stuNameList.size(); i++)
        {
              for (int j = 0; j < stuList.size(); j++)
            {
                if(stuNameList.get(i).getName().equals(stuList.get(j).getStuName())){
                     endRow++;
               }
              sheet.getRow(j+2).getCell(1).setCellValue(stuList.get(j).getCname());  //科目
              sheet.getRow(j+2).getCell(1).setCellStyle(style);
              sheet.getRow(j+2).getCell(2).setCellValue(stuList.get(j).getScore());   //分数
              sheet.getRow(j+2).getCell(2).setCellStyle(style);
            }
              sheet.addMergedRegion(new CellRangeAddress(startRow, endRow,  0, 0)); //起始行  结束行 起始列 结束列
             sheet.getRow(startRow).getCell(0).setCellValue(stuNameList.get(i).getName());       //姓名
            sheet.getRow(startRow).getCell(0).setCellStyle(style);
            sheet.addMergedRegion(new CellRangeAddress(startRow, endRow,  3, 3));
             sheet.getRow(startRow).getCell(3).setCellValue(stuList.get(startRow).getSumScore());  //总分
            sheet.getRow(startRow).getCell(3).setCellStyle(style);
            sheet.addMergedRegion(new CellRangeAddress(startRow, endRow,  4, 4));
            sheet.getRow(startRow).getCell(4).setCellValue("第"+(i+1)+"名"); //排名
            sheet.getRow(startRow).getCell(4).setCellStyle(style);
            startRow=endRow+1;
        }
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = "成绩.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="  + fileName);
        try {
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

POM依赖

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>4.1.0</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.1.0</version>
</dependency>

Excel表格

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java导出Excel合并单元格的示例代码: ```java import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelExportDemo { public static void main(String[] args) throws Exception { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet1"); // 创建单元格样式 XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle(); // 设置单元格对齐方式 style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); // 创建字体 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 14); font.setBold(true); // 设置字体颜色 font.setColor(IndexedColors.BLACK.getIndex()); // 将字体应用到单元格样式 style.setFont(font); // 创建单元格合并区域 CellRangeAddress region = new CellRangeAddress(0, 0, 0, 3); // 将单元格合并到工作表 sheet.addMergedRegion(region); // 创建表头行 List<String> headers = new ArrayList<>(); headers.add("序号"); headers.add("姓名"); headers.add("年龄"); headers.add("性别"); // 创建表头单元格 for (int i = 0; i < headers.size(); i++) { sheet.createRow(1).createCell(i).setCellValue(headers.get(i)); } // 写入数据 for (int i = 0; i < 10; i++) { sheet.createRow(i + 2).createCell(0).setCellValue(i + 1); sheet.getRow(i + 2).createCell(1).setCellValue("张三" + i); sheet.getRow(i + 2).createCell(2).setCellValue(20 + i); sheet.getRow(i + 2).createCell(3).setCellValue("男"); } // 设置表头样式 for (int i = 0; i < headers.size(); i++) { sheet.getRow(1).getCell(i).setCellStyle(style); } // 设置列宽 for (int i = 0; i < headers.size(); i++) { sheet.setColumnWidth(i, 20 * 256); } // 输出Excel文件 FileOutputStream outputStream = new FileOutputStream("test.xlsx"); workbook.write(outputStream); outputStream.close(); workbook.close(); } } ``` 这段代码将创建一个名为“Sheet1”的Excel工作表,并在第一行合并四个单元格,作为表头。然后,它将在第二行创建表头单元格,并在第三行及以下写入数据。最后,将输出Excel文件“test.xlsx”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值