poi导出word带复选框控件

网上搜了很多很多资料,都没有实现。机缘巧合之下发现一个特简单的方法。直接接将复选框当成string写进去就可以了。

给个简单的例子,有遇到过这种需求的可以试一下。

导入相关jar包

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

上代码

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author DaGangCai
 * @Date 2020/9/17-18:31
 */
public class MarcCheckWord {
    //调用测试
    public static void main(String[] args) throws Exception {
        String path = "E:/wordCheckBox.docx";
        XWPFDocument document = new XWPFDocument();

        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun titleFun = titleParagraph.createRun();
        titleFun.setText("选中的复选框:☑" + "\r\r\r" + "未选中的复选框:□");
        titleFun.setFontSize(12);
        titleFun.setFontFamily("宋体");
        titleFun.addBreak();
        try {
            document.createStyles();
            System.out.println();

            File file = new File(path);
            if (file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(path);
            document.write(out);
            out.close();
            System.out.println("生成成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

拿走不谢,哈哈哈!

POI 中,可以通过创建单元格样式和设置数据验证来实现在 Excel 中添加复选框。具体步骤如下: 1. 创建工作簿和工作表 ``` Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); ``` 2. 创建单元格样式并设置复选框样式 ``` CellStyle checkboxStyle = workbook.createCellStyle(); checkboxStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); checkboxStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); checkboxStyle.setBorderBottom(BorderStyle.THIN); checkboxStyle.setBorderTop(BorderStyle.THIN); checkboxStyle.setBorderLeft(BorderStyle.THIN); checkboxStyle.setBorderRight(BorderStyle.THIN); // 设置复选框样式 checkboxStyle.setLocked(false); checkboxStyle.setIndention((short) 1); checkboxStyle.setAlignment(HorizontalAlignment.LEFT); ``` 3. 在需要添加复选框的单元格中设置该样式 ``` Row row = sheet.createRow(0); Cell cell = row.createCell(0); // 设置单元格样式为复选框样式 cell.setCellStyle(checkboxStyle); ``` 4. 设置数据验证 ``` DataValidationHelper validationHelper = sheet.getDataValidationHelper(); DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(new String[]{"true","false"}); CellRangeAddressList rangeList = new CellRangeAddressList(0, 0, 0, 0); // 设置数据验证为复选框 DataValidation validation = validationHelper.createValidation(constraint, rangeList); validation.setShowErrorBox(true); sheet.addValidationData(validation); ``` 通过以上步骤,就可以在 Excel 中添加复选框了。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值