java通过poi来设置表格边框

本文介绍了如何使用Java的POI库来设置.xlsx文件中表格的边框。通过XSSFCellStyle对象的setBorder方法,可以分别设置单元格的上、下、左、右边框,并提供了设置边框样式的示例代码。最后展示了导出带有边框的Excel表格效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、通过XSSFCellStyle(因为是.xlsx版本的excel,所以用XSSFCellStyle)的【setBorderBottom】、【setBorderLeft】、【setBorderRight】、【setBorderTop】方法来设置下、左、右、上的边框样式

2、增加样式的代码,如下红色框里面的代码
1:是设置边框
2:给指定行设置边框
3:给新增的行列设置边框
在这里插入图片描述

3、代码如下:

 private static void setClassDataToWorkBook(List data, XSSFWorkbook workbook, XSSFSheet sheet, Integer index){
        XSSFRow row;
        //遍历集合数据,产生数据行
        Iterator it = data.iterator();
        while (it.hasNext()) {
            //---------------------------设置行列的边框
            XSSFCellStyle setBorder = workbook.createCellStyle();
            setBorder.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
            setBorder.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
            setBorder.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
            setBorder.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
            index++;
            row = sheet.createRow(index);
            XSSFCell indexCell=row.createCell(0);
            indexCell.setCellValue(index-1);
            indexCell.setCellStyle(setBorder);//-----------------给行列添加边框
            Object t = it.next();
            //利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
            Field[] fields = t.getClass().getDeclaredFields();
            for (short i = 1; i <= 14; i++) {
                XSSFCell cell = row.createCell(i);
                Field field = fields[i];
                String fieldName = field.getName();
                String getMethodName = "get"
                        + fieldName.substring(0, 1).toUpperCase()
                        + fieldName.substring(1);
                try {
                    Class tCls = t.getClass();
                    Method getMethod = tCls.getMethod(getMethodName,
                            new Class[]{});
                    Object value = getMethod.invoke(t, new Object[]{}) == null ? "" : getMethod.invoke(t, new Object[]{});
                    String textValue;
                    if (value instanceof Date) {
                        Date date = (Date) value;
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        textValue = sdf.format(date);
                    } else {
                        //其它数据类型都当作字符串简单处理
                        textValue = value.toString();
                    }
                    XSSFRichTextString richString = new XSSFRichTextString(textValue);
                    XSSFFont font3 = workbook.createFont();
                    font3.setColor(HSSFColor.BLACK.index);//定义Excel数据颜色
                    richString.applyFont(font3);
                    cell.setCellValue(richString);
                    cell.setCellStyle(setBorder);//----------------给行列添加边框

                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

4、导出excel表格如下图
在这里插入图片描述

要在Java后台中生成Excel文件并设置全局宽度和边框样式,可以使用Apache POI库来完成。以下是一个简单的示例代码: ```java // 创建工作簿对象 Workbook workbook = new XSSFWorkbook(); // 创建工作表对象 Sheet sheet = workbook.createSheet("Sheet1"); // 设置列宽 sheet.setColumnWidth(0, 5000); // 第1列宽度为5000 sheet.setColumnWidth(1, 8000); // 第2列宽度为8000 sheet.setDefaultColumnWidth(10); // 其他列默认宽度为10 // 创建单元格样式对象 CellStyle style = workbook.createCellStyle(); style.setBorderTop(BorderStyle.THIN); // 上边框样式 style.setBorderBottom(BorderStyle.THIN); // 下边框样式 style.setBorderLeft(BorderStyle.THIN); // 左边框样式 style.setBorderRight(BorderStyle.THIN); // 右边框样式 // 创建标题行 Row titleRow = sheet.createRow(0); titleRow.createCell(0).setCellValue("标题1"); titleRow.createCell(1).setCellValue("标题2"); // 设置标题行样式 for (Cell cell : titleRow) { cell.setCellStyle(style); } // 创建数据行 for (int i = 1; i <= 10; i++) { Row dataRow = sheet.createRow(i); dataRow.createCell(0).setCellValue("数据" + i + "-1"); dataRow.createCell(1).setCellValue("数据" + i + "-2"); // 设置数据行样式 for (Cell cell : dataRow) { cell.setCellStyle(style); } } // 输出Excel文件 FileOutputStream outputStream = new FileOutputStream("output.xlsx"); workbook.write(outputStream); outputStream.close(); ``` 这段代码创建了一个名为`Sheet1`的工作表,并设置了第1列宽度为5000,第2列宽度为8000,其他列默认宽度为10。同时,创建了一个单元格样式对象,设置了上下左右四个边框的样式为细线。然后创建了一个标题行和10行数据行,并给它们设置了相同的样式。最后将工作簿对象输出到文件中。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值