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 oilSelfExpenses = tpmPmPrevTotalFunding.getOilSelfExpenses() == null ? 0 : tpmPmPrevTotalFunding.getOilSelfExpenses();
Double groupExpenses = tpmPmPrevTotalFunding.getGroupExpenses() == null ? 0 : tpmPmPrevTotalFunding.getGroupExpenses();
Double countryExpenses = tpmPmPrevTotalFunding.getCountryExpenses() == null ? 0 : tpmPmPrevTotalFunding.getCountryExpenses();
Double rowSum = selfResearchExpenses + oilSelfExpenses + groupExpenses + countryExpenses;
selfResearchExpensesSum += selfResearchExpenses;
oilSelfExpensesSum += oilSelfExpenses;
groupExpensesSum += groupExpenses;
countryExpensesSum += countryExpenses;
cellSum += rowSum;
for (ValueCodeTable valueCodeTable : ValueCodeTableList) {
if (valueCodeTable.getCode().equals(tpmPmPrevTotalFunding.getUseNature())) {
useNature = valueCodeTable.getCodeName();
break;
}
}
table1.createRow();//表格新增一行
XWPFTableRow newRow = table1.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText(useNature);//费用来源
newRow.getCell(1).setText(selfResearchExpenses.toString());//部门自研费用
newRow.getCell(2).setText(oilSelfExpenses.toString());//油田自筹
newRow.getCell(3).setText(groupExpenses.toString());//集团)股份项目
newRow.getCell(4).setText(countryExpenses.toString());//国家项目
newRow.getCell(5).setText(rowSum.toString());//合计
rowIndex++;
}
table1.createRow();//插入合计行
XWPFTableRow newRow = table1.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText("总计");
newRow.getCell(1).setText(selfResearchExpensesSum.toString());//部门自研费用 合计
newRow.getCell(2).setText(oilSelfExpensesSum.toString());//油田自筹 合计
newRow.getCell(3).setText(groupExpensesSum.toString());//集团)股份项目 合计
newRow.getCell(4).setText(countryExpensesSum.toString());//国家项目 合计
newRow.getCell(5).setText(cellSum.toString());//总合计
}
/**
* 表二 费用化经费年度拨款安排 数据插入word
*
* @param table 表二
* @param tpmPmPrevFundingGrantList 费用化经费年度拨款安排数据
*/
private static void insertTable2DataToWord(XWPFTable table, List<TpmPmPrevFundingGrant> tpmPmPrevFundingGrantList) {
if (tpmPmPrevFundingGrantList == null || tpmPmPrevFundingGrantList.size() <= 0) {
return;
}
int rowIndex = 1;//表格数据行行标
Double year1FundingSum = 0.0;
Double year2FundingSum = 0.0;
Double year3FundingSum = 0.0;
Double cellSum = 0.0;
for (TpmPmPrevFundingGrant tpmPmPrevFundingGrant : tpmPmPrevFundingGrantList) {
String recipientDeptName = tpmPmPrevFundingGrant.getRecipientDeptName() == null ? "" : tpmPmPrevFundingGrant.getRecipientDeptName();
Double year1Funding = tpmPmPrevFundingGrant.getYear1Funding() == null ? 0 : tpmPmPrevFundingGrant.getYear1Funding();
Double year2Funding = tpmPmPrevFundingGrant.getYear2Funding() == null ? 0 : tpmPmPrevFundingGrant.getYear2Funding();
Double year3Funding = tpmPmPrevFundingGrant.getYear3Funding() == null ? 0 : tpmPmPrevFundingGrant.getYear3Funding();
String remarks = tpmPmPrevFundingGrant.getRemarks() == null ? "" : tpmPmPrevFundingGrant.getRemarks();
Double rowSum = year1Funding + year2Funding + year3Funding;
year1FundingSum += year1Funding;
year2FundingSum += year2Funding;
year3FundingSum += year3Funding;
cellSum += rowSum;
table.createRow();//表格新增一行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText(String.valueOf(rowIndex));//序号
newRow.getCell(1).setText(recipientDeptName);//受款单位
newRow.getCell(2).setText(rowSum.toString());//合 计
newRow.getCell(3).setText(year1Funding.toString());//年1
newRow.getCell(4).setText(year2Funding.toString());//年2
newRow.getCell(5).setText(year3Funding.toString());//年3
newRow.getCell(6).setText(remarks);//备注
rowIndex++;
}
table.createRow();//插入合计行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText("总计");//合计
newRow.getCell(1).setText("");
newRow.getCell(2).setText(cellSum.toString());//合 计
newRow.getCell(3).setText(year1FundingSum.toString());//年1 合 计
newRow.getCell(4).setText(year2FundingSum.toString());//年2 合 计
newRow.getCell(5).setText(year3FundingSum.toString());//年3 合 计
newRow.getCell(6).setText("");//备注
mergeCellsHorizontal(table, rowIndex, 0, 1);//合并合计行的第一列和第二列
}
/**
* 表三 资本化经费年度拨款安排 数据插入word
*
* @param table 三
* @param tpmPmPrevCapitalGrantList 资本化经费年度拨款安排数据
*/
private static void insertTable3DataToWord(XWPFTable table, List<TpmPmPrevCapitalGrant> tpmPmPrevCapitalGrantList) {
if (tpmPmPrevCapitalGrantList == null || tpmPmPrevCapitalGrantList.size() <= 0) {
return;
}
int rowIndex = 1;//表格数据行行标
Double year1FundingSum = 0.0;
Double year2FundingSum = 0.0;
Double year3FundingSum = 0.0;
Double cellSum = 0.0;
for (TpmPmPrevCapitalGrant tpmPmPrevCapitalGrant : tpmPmPrevCapitalGrantList) {
String recipientDeptName = tpmPmPrevCapitalGrant.getRecipientDeptName() == null ? "" : tpmPmPrevCapitalGrant.getRecipientDeptName();
Double year1Funding = tpmPmPrevCapitalGrant.getYear1Funding() == null ? 0 : tpmPmPrevCapitalGrant.getYear1Funding();
Double year2Funding = tpmPmPrevCapitalGrant.getYear2Funding() == null ? 0 : tpmPmPrevCapitalGrant.getYear2Funding();
Double year3Funding = tpmPmPrevCapitalGrant.getYear3Funding() == null ? 0 : tpmPmPrevCapitalGrant.getYear3Funding();
Double rowSum = year1Funding + year2Funding + year3Funding;
year1FundingSum += year1Funding;
year2FundingSum += year2Funding;
year3FundingSum += year3Funding;
cellSum += rowSum;
table.createRow();//表格新增一行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText(String.valueOf(rowIndex));//序号
newRow.getCell(1).setText(recipientDeptName);//受款单位
newRow.getCell(2).setText(rowSum.toString());//合 计
newRow.getCell(3).setText(year1Funding.toString());//年1
newRow.getCell(4).setText(year2Funding.toString());//年2
newRow.getCell(5).setText(year3Funding.toString());//年3
rowIndex++;
}
table.createRow();//插入合计行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText("合计");//合计
newRow.getCell(1).setText("");
newRow.getCell(2).setText(cellSum.toString());//合 计
newRow.getCell(3).setText(year1FundingSum.toString());//年1 合 计
newRow.getCell(4).setText(year2FundingSum.toString());//年2 合 计
newRow.getCell(5).setText(year3FundingSum.toString());//年3 合 计
mergeCellsHorizontal(table, rowIndex, 0, 1);//合并合计行的第一列和第二列
}
/**
* 表四 外协课题经费安排 数据插入word
*
* @param table 表四
* @param tpmPmProjectBaseList 外协课题经费安排费用
* @param ValueCodeTableList 外协课题经费安排课题名称
*/
private static void insertTable4DataToWord(XWPFTable table, List<TpmPmProjectBase> tpmPmProjectBaseList, List<ValueCodeTable> ValueCodeTableList) {
if (tpmPmProjectBaseList == null || tpmPmProjectBaseList.size() <= 0 || ValueCodeTableList == null || ValueCodeTableList.size() <= 0) {
return;
}
int rowIndex = 1;//表格数据行行标
Double costSum = 0.0;
for (TpmPmProjectBase tpmPmProjectBase : tpmPmProjectBaseList) {
String projectName = tpmPmProjectBase.getProjectName() == null ? "" : tpmPmProjectBase.getProjectName();
Double cost = tpmPmProjectBase.getCost() == null ? 0 : tpmPmProjectBase.getCost();
String fundsSources = "";
costSum += cost;
for (ValueCodeTable valueCodeTable : ValueCodeTableList) {
if (valueCodeTable.getCode().equals(tpmPmProjectBase.getFundsSources())) {
fundsSources = valueCodeTable.getCodeName();
break;
}
}
table.createRow();//表格新增一行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText(String.valueOf(rowIndex));//序号
newRow.getCell(1).setText(projectName);//外协课题名称
newRow.getCell(2).setText(cost.toString());//经费预算
newRow.getCell(3).setText(fundsSources);//经费来源
rowIndex++;
}
table.createRow();//插入合计行
XWPFTableRow newRow = table.getRow(rowIndex);//得到刚刚新增的行
newRow.getCell(0).setText("总计");
newRow.getCell(1).setText("");//部门自研费用 合计
newRow.getCell(2).setText(costSum.toString());//经费预算 合计
newRow.getCell(3).setText("");
mergeCellsHorizontal(table, rowIndex, 0, 1);//合并合计行的第一列和第二列
}
/**
* 表五 油田自筹经费预算表(单位:万元) 数据插入word
*
* @param table 表格五
* @param tpmPmPrevOilSelfBudgetList 油田自筹经费预算数据
*/
private static void insertTable5DataToWord(XWPFTable table, List<TpmPmPrevOilSelfBudget> tpmPmPrevOilSelfBudgetList) {
int rowSum = 0, cellSum = 0;//表格的行和列
Map<String, Map<String, Double>> rowDatas = new HashMap();//表格行数据
Map<String, Double> cellDataSum = new HashMap<>();//表格列数据
Map<String, Double> tableDept = new HashMap<>();//表格动态表头(单位)
//表格宽度
int tableWidth = 9000;
if (tpmPmPrevOilSelfBudgetList == null || tpmPmPrevOilSelfBudgetList.size() <= 0) {
return;
}
for (TpmPmPrevOilSelfBudget tpmPmPrevOilSelfBudget : tpmPmPrevOilSelfBudgetList) {
tableDept.put(tpmPmPrevOilSelfBudget.getDeptName(), 0.0);
// 每行数据
Map<String, Double> itemDept = new HashMap();
for (TpmPmPrevOilSelfBudget tpmPmPrevOilSelfBudget1 : tpmPmPrevOilSelfBudgetList) {
if (tpmPmPrevOilSelfBudget.getFundStructureName().equals(tpmPmPrevOilSelfBudget1.getFundStructureName())) {
boolean isExist = itemDept.containsKey(tpmPmPrevOilSelfBudget1.getDeptName());
if (isExist) {
Double funding = itemDept.get(tpmPmPrevOilSelfBudget1.getDeptName()) + tpmPmPrevOilSelfBudget1.getFunding();
itemDept.put(tpmPmPrevOilSelfBudget1.getDeptName(), funding);
} else {
itemDept.put(tpmPmPrevOilSelfBudget1.getDeptName(), tpmPmPrevOilSelfBudget1.getFunding());
}
itemDept = sortMapByKey(itemDept);
}
}
boolean isExist = cellDataSum.containsKey(tpmPmPrevOilSelfBudget.getDeptName());
if (isExist) {
Double funding = cellDataSum.get(tpmPmPrevOilSelfBudget.getDeptName()) + tpmPmPrevOilSelfBudget.getFunding();
cellDataSum.put(tpmPmPrevOilSelfBudget.getDeptName(), funding);
} else {
cellDataSum.put(tpmPmPrevOilSelfBudget.getDeptName(), tpmPmPrevOilSelfBudget.getFunding());
}
cellDataSum = sortMapByKey(cellDataSum);
rowDatas.put(tpmPmPrevOilSelfBudget.getFundStructureName(), itemDept);
}
tableDept = sortMapByKey(tableDept);
rowSum = rowDatas.size() + 2;//表格行数
cellSum = tableDept.size() + 2;//表格列数
//绘制表格5
drawTable5(table, cellSum, tableWidth, rowSum);
//给表格的每个单元格设定宽度
setRowCellWidth(table, tableWidth);
//给表格的单元格赋值
setTable5Value(table, cellSum, tableDept, rowDatas, cellDataSum);
//合并表头
mergeCellsVertically(table, 0, 0, 1);//行合并成“经费结构科目”列
mergeCellsHorizontal(table, 0, 1, 2);//列合并成“年份”列
mergeCellsVertically(table, cellSum - 1, 0, 1);//合并成“合计”列
}
/**
* 绘制表格5
*
* @param table 表格5
* @param cellSum 列总数
* @param tableWidth 表格宽度
* @param rowSum 行总数
*/
private static void drawTable5(XWPFTable table, int cellSum, int tableWidth, int rowSum) {
//给第一行表格添加列
XWPFTableRow row1 = table.getRow(0);
for (int i = 1; i < cellSum; i++) {
row1.addNewTableCell();
}
//表格属性
CTTbl ttbl = table.getCTTbl();
CTTblPr tablePr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
//表格宽度
CTTblWidth tblWidth = tablePr.isSetTblW() ? tablePr.getTblW() : tablePr.addNewTblW();
tblWidth.setW(BigInteger.valueOf(tableWidth));
//设置表格宽度为非自动
tblWidth.setType(STTblWidth.DXA);
//添加表格所需的行列
for (int r = 1; r <= rowSum; r++) {
table.createRow();//创建新行
}
}
/**
* 给表格5单元格赋值
*
* @param table 表格5
* @param cellSum 表格列的个数
* @param tableDept 表格动态列的个数(单位是动态的)
* @param rowDatas 表格行的值
* @param cellDataSum 表格动态列的合计,作为最后一行合计行的值
*/
private static void setTable5Value(XWPFTable table,
int cellSum,
Map<String, Double> tableDept,
Map<String, Map<String, Double>> rowDatas,
Map<String, Double> cellDataSum) {
XWPFTableRow row1 = table.getRow(0);
row1.getCell(0).setText("经费结构科目");
row1.getCell(1).setText(String.valueOf(DateTime.getNow().getYear()));
row1.getCell((cellSum - 1)).setText("合计");
int deptCellBeginIndex = 1;
XWPFTableRow row2 = table.getRow(1);
for (String dept : tableDept.keySet()) {
row2.getCell(deptCellBeginIndex).setText(dept);
deptCellBeginIndex++;
}
int rowIndex = 2;
for (Map.Entry<String, Map<String, Double>> rowEntry : rowDatas.entrySet()) {
int cellIndex = 1;
Double rowDataSum = 0.0;
table.getRow(rowIndex).getCell(0).setText(rowEntry.getKey());//每行经费结构科目赋值
for (Map.Entry<String, Double> cellEntry : rowEntry.getValue().entrySet()) {
table.getRow(rowIndex).getCell(cellIndex).setText(cellEntry.getValue().toString());//每行的单位赋值
rowDataSum += cellEntry.getValue();
cellIndex++;
}
table.getRow(rowIndex).getCell(cellIndex).setText(rowDataSum.toString());//每行合计赋值
rowIndex++;
}
//合计行赋值
table.getRow(rowIndex).getCell(0).setText("合计");
int cellIndex = 1;
Double rowDataSum = 0.0;
for (Map.Entry<String, Double> cellData : cellDataSum.entrySet()) {
table.getRow(rowIndex).getCell(cellIndex).setText(cellData.getValue().toString());
rowDataSum += cellData.getValue();
cellIndex++;
}
table.getRow(rowIndex).getCell(cellIndex).setText(rowDataSum.toString());
}
/**
* @param document word document
* @param savePath 保存路径
* @throws Exception 异常
*/
public static void saveDocument(XWPFDocument document, String savePath)
throws Exception {
FileOutputStream fos = new FileOutputStream(savePath);
document.write(fos);
fos.close();
}
/**
* 设置表格每列的宽度
*
* @param table XWPFTable
* @param rowWidth 行宽度
*/
public static void setRowCellWidth(XWPFTable table, int rowWidth) {
List<XWPFTableRow> xwpfTableRows = table.getRows();
for (XWPFTableRow row : xwpfTableRows) {
int cellSize = row.getTableCells().size();
int cellWidth = rowWidth / cellSize;
for (int i = 0; i < row.getTableCells().size(); i++) {
setCellText(row.getCell(i), "", "", cellWidth);
}
}
}
/**
* 列设置值,宽度,背景色
*
* @param cell XWPFTableCell
* @param text 单元格的值
* @param bgcolor 单元格的背景
* @param width 单元格的宽度
*/
public static void setCellText(XWPFTableCell cell, String text, String bgcolor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
CTTcPr ctPr = cttc.addNewTcPr();
CTShd ctshd = ctPr.addNewShd();
ctshd.setFill(bgcolor);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
List<CTP> ctpList = cttc.getPList();
ctpList.get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cell.setText(text);
}
/**
* word跨列合并单元格
*
* @param table 表格
* @param row 需要合并行的index
* @param fromCell 合并开始列
* @param toCell 合并结束列
*/
public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if (cellIndex == fromCell) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* word跨行并单元格
*
* @param table 表格
* @param col 要合并的列的index
* @param fromRow 开始行的index
* @param toRow 结束行的index
*/
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == fromRow) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* 获取项目目录下的文件流
*
* @param proPath 项目目录的文件路径
* @return InputStream
* @throws IOException 异常
*/
public static InputStream getProPathInputStream(String proPath) throws IOException {
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists())
path = new File("");
String name = path.getAbsolutePath() + proPath;
InputStream is = new FileInputStream(name);
return is;
}
/**
* 获取磁盘文件目录下的文件流
*
* @param filePath 磁盘文件目录路径
* @return InputStream
* @throws IOException 异常
*/
public static InputStream getFilePathInputStream(String filePath) throws IOException {
File file = new File(filePath);
InputStream input = new FileInputStream(file);
return input;
}
/**
* 将InputStream写入本地文件
*
* @param destination 写入本地目录
* @param input 输入流
* @throws IOException 异常
*/
public static void writeToLocal(String destination, InputStream input) throws IOException {
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(destination);
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
input.close();
downloadFile.close();
}
/**
* Map按key进行排序
*
* @param map 需要排序的map
* @return 排序过后的map
*/
public static Map<String, Double> sortMapByKey(Map<String, Double> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, Double> sortMap = new TreeMap<String, Double>(
new MapKeyComparator());
sortMap.putAll(map);
return sortMap;
}
}
/**
* 比较器类
*/
class MapKeyComparator implements Comparator<String> {
/**
* @param s1 比较参数1
* @param s2 比较参数2
* @return
*/
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2); //从小到大排序
}
}
3.说明
1,如果是动态列的合并,wps打开会有问题。