最近项目验收遇到了很扯的事,要从2017年开始补5年的项目日志,找了几个同事,每人负责写一年(抓狂),作为一个程序猿怎么可能一天一天的去写
首先把刚补上的月报统一整理到excel,一行作为一个日报内容。
第一列是内容,第二列是日报日期(同事通过excel,自动跳过周末生成的)
1、引入jar包依赖
<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>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
2、读取excel
public static void readExcel(){
// 定义一个数据格式化对象
XSSFWorkbook wb = null;
try {
//excel模板路径
File cfgFile = new File("d:/a.xlsx");
InputStream in = new FileInputStream(cfgFile);
//读取excel模板
wb = new XSSFWorkbook(in);
//获取sheet表格,及读取单元格内容
XSSFSheet sheet = null;
sheet = wb.getSheetAt(0);
//先将获取的单元格设置为String类型,下面使用getStringCellValue获取单元格内容
//如果不设置为String类型,如果单元格是数字,则报如下异常
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
sheet.getRow(i).getCell(0).setCellType(CellType.STRING);
//读取单元格内容
String cellValue = sheet.getRow(i).getCell(0).getStringCellValue();
String time = sheet.getRow(i).getCell(1).getStringCellValue();
System.out.println(time+"---"+cellValue);
writeWordTable(cellValue,time);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3、创建word模板,原理是日志文件内容大体是相同的,只有几项需要修改,如下图,只需要填充“日志日期”、“日报内容”、“明日计划” 3处。那么这几处的文件就相当于占位符,生成日志时直接替换这3处文字即可。
4、读取word模板,生成新文件
public static void writeWordTable(String content,String wordName){
String srcPath = "E:\\wordDocx\\info.docx";//模板路径
String destPath = "E:\\wordExport\\" +wordName + "施工日志.docx";
//明日计划
String[] tomorrow = {"持续推进系统推广工作","日常响应用户各问题咨询","持续进行应用系统巡检监控" ,"配合客户查询问题,出所需报表。","协助平台用户使用系统,解决操作问题。","协调部门进行数据对接", "推送上级平台错误数据进行汇总修改"};
Random random = new Random();
try {
InputStream inputStream = new FileInputStream(srcPath);
FileOutputStream outputStream = new FileOutputStream(destPath);
XWPFDocument document = new XWPFDocument(inputStream);
//1. 替换段落中的指定文字
Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
// 替换表格中的指定文字
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
if("日报内容".equals(cell.getText())){
//清楚表格内原来的内容
cell.removeParagraph(0);
cell.setText(content);
}
if("日志日期".equals(cell.getText())){
cell.removeParagraph(0);
cell.setText(wordName);
}
if("明日计划".equals(cell.getText())){
cell.removeParagraph(0);
//随机取几条明日计划
int num = random.nextInt(2)+2;
String jihua = "";
for (int j = 0; j < num; j++) {
int index = random.nextInt(6);
jihua+=j+1+"、"+tomorrow[index]+"\n\r";
}
cell.setText(jihua);
}
}
}
}
//3.输出流
document.write(outputStream);
System.out.println("写入word完成");
} catch (Exception e) {
e.printStackTrace();
}
}
这里cell.setText("")是追加的,不会覆盖原来的内容,所以需要先删除占位的文字,cell.removeParagraph(0);
目前存在的一个问题就是不会换行,即便加了“\n”、“\n\r”都不行
上面的方法只能读取替换word表格中的内容,如果是正常的行内容,可以用下面这个方法
public static void writeWord(String content,String wordName){
String srcPath = "E:\\wordDocx\\info.docx";//模板路径
String destPath = "E:\\wordExport\\" +wordName + "施工日志.docx";
try {
InputStream inputStream = new FileInputStream(srcPath);
FileOutputStream outputStream = new FileOutputStream(destPath);
XWPFDocument document = new XWPFDocument(inputStream);
//1. 替换段落中的指定文字(本人模板中 对应的编号)
Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
String text;
XWPFParagraph paragraph;
List<XWPFRun> run;
while (itPara.hasNext()) {
paragraph = itPara.next();
run = paragraph.getRuns();
for (int i = 0, runSie = run.size(); i < runSie; i++) {
text = run.get(i).getText(run.get(i).getTextPosition());
if (text != null && text.equals("日报内容")) {
run.get(i).setText(content, 0);
}
}
}
//3.输出流
document.write(outputStream);
System.out.println("写入word完成");
} catch (Exception e) {
e.printStackTrace();
}
}