poi 操作office word表格 - SpringBoot

1.mvn 配置

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

2.参考代码

 

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
 *
 * Created by yulf on 2018/12/18.
 */
public class PlanTaskBookUtils {
    //系统临时文件目录
    private static final String systemDmpDir = System.getProperty("java.io.tmpdir");
    //word 文件后缀
    private static final String extension = ".docx";
    //任务书模板项目路径
    private static final String tplProjectPath = "/ExcelTpl/TpmPmPrevPlanTaskBookTemplate.docx";

    /**
     * 保存模板到系统临时目录
     *
     * @param businessKey 任务书ID
     * @return 新文件的路径
     * @throws IOException 异常
     */
    public static String saveTemplateToSystemDmpDir(String businessKey) {
        try {
            String localFilePath = getPath(systemDmpDir + "\\" + businessKey + extension);
            InputStream templateInputStream = getProPathInputStream(tplProjectPath);
            writeToLocal(localFilePath, templateInputStream);
            templateInputStream.close();
            return localFilePath;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取path
     *
     * @param path 路径字符串
     * @return path
     */
    public static String getPath(String path) {
        Path returnPath = Paths.get(path);
        return returnPath.toString();
    }

    /**
     * 获取 Word Document
     *
     * @param inputStream word 文件流
     * @return Word Document
     * @throws IOException 文件异常
     */
    public static XWPFDocument getXWPFDocument(InputStream inputStream) throws IOException {
        XWPFDocument document = new XWPFDocument(inputStream);
        return document;
    }

    /**
     * 插入表格数据到Word文档
     *
     * @param insertTableDataToWordDto 插入表格的数据
     */
    public static void insertTableDataToWord(InsertTableDataToWordDto insertTableDataToWordDto) {
        try {
            //临时文件地址
            String localFilePath = getPath(systemDmpDir + "\\" + insertTableDataToWordDto.getBusinessKey() + extension);
            InputStream inputStream = getFilePathInputStream(localFilePath);
            //临时文件内容
            XWPFDocument document = getXWPFDocument(inputStream);
            //临时文件中的表格
            List<XWPFTable> xwpfTables = document.getTables();

            insertTable1DataToWord(xwpfTables.get(3), insertTableDataToWordDto.getTpmPmPrevTotalFundingList(), insertTableDataToWordDto.getValueCodeTableListTable1());
            insertTable2DataToWord(xwpfTables.get(4), insertTableDataToWordDto.getTpmPmPrevFundingGrantList());
            insertTable3DataToWord(xwpfTables.get(5), insertTableDataToWordDto.getTpmPmPrevCapitalGrantList());
            insertTable4DataToWord(xwpfTables.get(6), insertTableDataToWordDto.getTpmPmProjectBaseList(), insertTableDataToWordDto.getValueCodeTableListTable4());
            insertTable5DataToWord(xwpfTables.get(7), insertTableDataToWordDto.getTpmPmPrevOilSelfBudgetList());
            inputStream.close();

            //保存插入数据过后的word文件
            saveDocument(document, localFilePath);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 表一  总经费  数据插入word
     *
     * @param table1                    表格一
     * @param tpmPmPrevTotalFundingList 费用
     * @param ValueCodeTableList        费用来源
     */
    private static void insertTable1DataToWord(XWPFTable table1, List<TpmPmPrevTotalFunding> tpmPmPrevTotalFundingList, List<ValueCodeTable> ValueCodeTableList) {
        if (tpmPmPrevTotalFundingList == null || tpmPmPrevTotalFundingList.size() <= 0 || ValueCodeTableList == null || ValueCodeTableList.size() <= 0) {
            return;
        }
        int rowIndex = 1;//表格数据行行标
        Double selfResearchExpensesSum = 0.0;//部门自研费用 合计
        Double oilSelfExpensesSum = 0.0;//油田自筹 合计
        Double groupExpensesSum = 0.0;//集团)股份项目 合计
        Double countryExpensesSum = 0.0;//国家项目 合计
        Double cellSum = 0.0;//列合计
        for (TpmPmPrevTotalFunding tpmPmPrevTotalFunding : tpmPmPrevTotalFundingList) {
            String useNature = "";
            Double selfResearchExpenses = tpmPmPrevTotalFunding.getSelfResearchExpenses() == null ? 0 : tpmPmPrevTotalFunding.getSelfResearchExpenses();
            Double oil
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值