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