• springboot npoi 合并单元格 之后设置单元格居中


    //设置style
    ICellStyle cellstyle = workbook.CreateCellStyle();
    cellstyle.VerticalAlignment = VerticalAlignment.Center;
    cellstyle.Alignment = HorizontalAlignment.Center;
    
    //合并操作
    
    sheet.AddMergedRegion(new CellRangeAddress(index["firstRow"], index["lastRow"], index["firstCol"], index["lastCol"]));//起始行,结束行,起始列,结束列
    
    //设置合并后style
    var cell = sheet.GetRow(index["firstRow"]).GetCell(index["firstCol"]);
    cell.CellStyle = cellstyle;
        @GetMapping("/a")
        public void a(HttpServletResponse response) {
            // 创建工作簿类
            XSSFWorkbook wb = new XSSFWorkbook();
            // 创建工作表并设置表名
            XSSFSheet sheet = wb.createSheet("订单");
            // 创建行,下标从0开始
            XSSFRow row = sheet.createRow(0);
            // 第四步,创建单元格,并设置值表头 设置表头居中
            XSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
            //声明列对象
            XSSFCell cell = null;
    
            //标题
            String[] title = {"姓名", "年龄", "性别", "物品名称", "数量"};
            //创建标题
            for (int i = 0; i < title.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(style);
            }
    
            //设置合并后居中显示样式
            XSSFCellStyle cellstyle = wb.createCellStyle();
            cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellstyle.setAlignment(HorizontalAlignment.CENTER);
    
            int RowCount=1;
            List<order> orders = getOrders();
            for (int i = 0; i < orders.size(); i++) {
                row = sheet.createRow(RowCount);
    
                cell = row.createCell(0);
                cell.setCellValue(orders.get(i).name);
                cell.setCellStyle(style);
    
                cell = row.createCell(1);
                cell.setCellValue(orders.get(i).age);
                cell.setCellStyle(style);
    
                cell = row.createCell(2);
                cell.setCellValue(orders.get(i).sex);
                cell.setCellStyle(style);
    
                for(int j=0;j<orders.get(i).goods.size();j++) {
                    if(j==0) {
                        cell = row.createCell(3);
                        cell.setCellValue(orders.get(i).goods.get(j).goodsName);
                        cell.setCellStyle(style);
                        cell = row.createCell(4);
                        cell.setCellValue(orders.get(i).goods.get(j).quantity);
                        cell.setCellStyle(style);
                        RowCount = RowCount + 1;
                    }
                    else{
                        row = sheet.createRow(RowCount);
                        cell = row.createCell(3);
                        cell.setCellValue(orders.get(i).goods.get(j).goodsName);
                        cell.setCellStyle(style);
                        cell = row.createCell(4);
                        cell.setCellValue(orders.get(i).goods.get(j).quantity);
                        cell.setCellStyle(style);
                        RowCount = RowCount + 1;
                    }
                }
    
                //合并单元格
                if(orders.get(i).goods.size()>1) {
                    for (int colnum = 0; colnum < 3; colnum++) {
                        //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列
                        int firstRow = RowCount - orders.get(i).goods.size();
                        int lastRow = RowCount - 1;
                        sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, colnum, colnum));
                        //设置样式
                        sheet.getRow(firstRow).getCell(colnum).setCellStyle(cellstyle);
                    }
                }
            }
            //响应到客户端
            try {
                String fileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()) + ".xlsx";
                this.setResponseHeader(response, fileName);
                OutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
                os.close();
                wb.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         *
         * @return
         */
        private List<order> getOrders() {
            List<order> orderList = new ArrayList<order>();
            for (int i = 0; i < 10; i++) {
                order order = new order(String.valueOf(i), String.valueOf(i), String.valueOf(i));
                List<goods> goodsList = new ArrayList<goods>();
                if (i == 0 || i == 5 || i == 7) {
                    goodsList.add(new goods("篮球", 1));
                    goodsList.add(new goods("足球", 1));
                    goodsList.add(new goods("运动鞋", 2));
                    order.setGoods(goodsList);
                } else {
                    goodsList.add(new goods("其它", i));
                    order.setGoods(goodsList);
                }
                orderList.add(order);
            }
            return orderList;
        }
    
        //发送响应流方法
        public void setResponseHeader(HttpServletResponse response, String fileName) {
            try {
                try {
                    fileName = new String(fileName.getBytes("iso8859-1"), "utf-8");
                    System.out.println(fileName);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                response.setContentType("application/octet-stream;charset=ISO8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
  • 相关阅读:
    Android Studio cannot launch avd in emulator问题解决
    ios设备安装破解软件及自己下载的软件ipa教程
    解决数据库连接错误 您在wp-config.php文件中提供的数据库用户名和密码可能不正确,或者无法连接到localhost上的数据库服务器,这意味着您的主机数据库服务器已停止工作。
    地图工具类大集合
    在家云看景点的皮蛋TV
    在线本地视频翻译
    南瓜影视特权
    韩剧tv特权来了
    网盘下载合集,宽带多快,我多快
    白嫖永久免费电脑端美图秀秀
  • 原文地址:https://www.cnblogs.com/xubao/p/14949057.html
Copyright © 2020-2023  润新知