Java 将表格数据导入word文档中

个人觉得这个功能实在搞笑,没什么意义,没办法提了需求就要实现,(太好说话了把我)

我的实现是再word中生成一个与 excel行,列 一样的一个表格,然后把从excel拿到的数据(exList参数)依次放到word表格中

    public static void createFile(HttpServletResponse response, String fileName, List<List<String>> exList) {


        try {
            setResponseHeader(response, fileName);
            //生成一个word模版文件
            XWPFDocument document = new XWPFDocument();

            XWPFTable table = document.createTable(exList.size(), exList.get(0).size());
            XWPFTableRow row;

            for (int i = 0; i < exList.size(); i++) {

                List<String> sdf = exList.get(i);

                row = table.getRow(i);

                for (int j = 0; j < exList.get(i).size(); j++) {
                    String s =sdf.get(j);
                    row.getCell(j).setText(s);
                    row.getCell(j).setWidthType(TableWidthType.AUTO);
                }
                //将数据插入表格中  pos:0 表示 第一个表格
                document.setTable(0,table);

            }

            ServletOutputStream outputStream = response.getOutputStream();

            BufferedOutputStream bufferStream = new BufferedOutputStream(outputStream, 1024);

            document.write(bufferStream);
            document.close();
            bufferStream.close();;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setResponseHeader(HttpServletResponse response, String name) {

        try {
            name = new String(name.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 response.setContentType("multipart/form-data");
        //要保存的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + name + ".docx");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    }

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库实现将数学公式转换到 Word 文档。具体步骤如下: 1. 首先,需要在 pom.xml 文件添加 Apache POI 的依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个 Word 文档,并添加一个段落: ``` XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); ``` 3. 创建一个数学公式对象,并设置公式内容: ``` XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); ``` 其,"数学公式内容" 是你要插入的数学公式的内容,可以使用 LaTeX 语法编写。 4. 将 Word 文档保存到本地: ``` FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); ``` 完整代码示例: ``` import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath; public class MathFormulaToWord { public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFMath math = paragraph.createRun().getCTR().addNewOMath(); CTOMath ctomath = CTOMath.Factory.parse("数学公式内容"); math.setOMath(ctomath); FileOutputStream out = new FileOutputStream("文件路径"); document.write(out); out.close(); } } ``` 注意:在将 LaTeX 语法转换为 MathML 语法时,需要借助一些工具,例如 MathJax、LaTeX2MathML 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值