烦人的word

tech2022-09-22  71

写过的最复杂的word表格的导出。。。。。终于完事了。

感觉自己就像那个count,在for循环里迷了路。。。

Document doc = new Document(); try { doc.loadFromFile(Global.getDownloadPath() + "wordtemp/" + equnum + "装备定期维护检查.doc"); } catch (Exception e) { e.printStackTrace(); throw new BusinessException("模板导入有误"); } ParagraphCollection paragraphCollection = doc.getSections().get(0).getParagraphs(); //填写封面 for (int i = 0; i < paragraphCollection.getCount(); i++) { String str = paragraphCollection.get(i).getText(); if (str.contains("装备名称")) { paragraphCollection.get(i).setText("装备名称: " + de.getEqName()); } else if (str.contains("装备编号")) { paragraphCollection.get(i).setText("装备编号: " + de.getEqNum()); } else if (str.contains("生产厂家")) { paragraphCollection.get(i).setText("生产厂家: " + de.getProducer()); } else if (str.contains("出厂日期")) { paragraphCollection.get(i).setText("出厂日期: " + CaseDataTime.caseymd(de.getManufactureTime())); } else if (str.contains("启用日期")) { paragraphCollection.get(i).setText("启用日期: " + CaseDataTime.caseymd(de.getStartTime())); } else if (str.contains("使用单位")) { paragraphCollection.get(i).setText("使用单位: " + unit); } else if (str.contains("填写时间")) { paragraphCollection.get(i).setText("填写时间: " + CaseDataTime.caseymd(list.get(0).getTaskName()) + "至" + CaseDataTime.caseymd(list.get(list.size()-1).getTaskName())); } } /** * 加载文档/填写_____年 */ for (int i = 1; i < doc.getSections().getCount(); i++) { int j = doc.getSections().get(i).getParagraphs().getCount(); for (int k = 0; k < j; k++) { Paragraph paragraph = doc.getSections().get(i).getParagraphs().get(k); paragraph.getText().indexOf("年"); doc.setReplaceFirst(true); if (list.size()>0){ paragraph.replace(" ", " " + list.get(0).getTaskName().split("-")[0], false, true); }else { paragraph.replace(" ", " " + list1.get(0).getTaskName().split("-")[0], false, true); } } } /** * 删除表头的 月 日 */ Table table = null; for (int k = 0; k < doc.getSections().getCount(); k++) { for (int t = 0; t < doc.getSections().get(k).getTables().getCount(); t++) { try { table = doc.getSections().get(k).getTables().get(t); //遍历表格每行 for (int j = 0; j < table.getRows().getCount(); j++) { if (j == 0) { for (int d = 3; d < table.getRows().get(0).getCells().getCount(); d++) { TableCell cell = table.getRows().get(0).getCells().get(d); cell.getChildObjects().clear(); } } } } catch (Exception e) { System.out.println("这一页没表格"); } } } /** * 插入数据 */ int count = 0; int count1 = 0; table = null; for (int k = 1; k < doc.getSections().getCount(); k++) { if (k == 1) { for (int t = 0; t < doc.getSections().get(k).getTables().getCount(); t++) { table = doc.getSections().get(k).getTables().get(t); //遍历表格每行 for (int j = 0; j < table.getRows().getCount(); j++) { if (j == 0) { for (int d = 3; d < table.getRows().get(0).getCells().getCount(); d++) { TableCell cell = table.getRows().get(0).getCells().get(d); if (list.size() > d - 3) { cell.addParagraph().appendText(CaseDataTime.casemd(list.get(d - 3).getTaskName()));//月日 } } } else if (IsNumber.isNumeric(table.getRows().get(j).getCells().get(0).getFirstParagraph().getText().trim())) {//判断第一列是不是序号,数字(区分开执行人和备注) if (table.getRows().get(j).getCells().getCount() == table.getRows().get(0).getCells().getCount()) {//判断一行几列,第三列是否是第二列的子项,这是否的情况下,从第四列开始填 for (int q = 3; q < table.getRows().get(j).getCells().getCount(); q++) {// if (lists.size() > q - 3) { if (lists.get(q - 3).size() > j - 1 + count) { table.getRows().get(j).getCells().get(q).getFirstParagraph().setText(CaseDataTime.Istrue(lists.get(q - 3).get(j - 1 + count).getResult().toString().trim())); } } } } else {//第三列是第二列的子项,所以列数多一位,应该从第五列开始 for (int q = 4; q < table.getRows().get(j).getCells().getCount(); q++) {//CaseDataTime.Istrue(lists.get(j - 1).get(q - 4).getResult().toString().trim()) if (lists.size() > q - 4) { if (lists.get(q - 4).size() > j - 1 + count) { table.getRows().get(j).getCells().get(q).getFirstParagraph().appendText(CaseDataTime.Istrue(lists.get(q - 4).get(j - 1 + count).getResult().toString().trim())); } } } } } else if (table.getRows().get(j).getRowIndex() == table.getRows().getCount() - 2) {//如果行标等于总行数-1,说明是倒数第二行 for (int q = 1; q < table.getRows().get(j).getCells().getCount(); q++) { if (list.size() > q - 1) { table.getRows().get(j).getCells().get(q).getFirstParagraph().appendText(list.get(q - 1).getExecutionUser()); } } } else if (table.getRows().get(j).getRowIndex() == table.getRows().getCount() - 1) {//如果行标等于总行数-1,说明是倒数第二行list.get(q).getRemark() for (int q = 0; q < 7; q++) { if (list.size() > q) { table.getRows().get(j).getCells().get(1).addParagraph().appendText(list.get(q).getTaskName() + ": " + list.get(q).getRemark()); } } } } if (table != null) { count = count + table.getRows().getCount() - 1; } } } if (k == 2) { for (int t = 0; t < doc.getSections().get(k).getTables().getCount(); t++) { table = doc.getSections().get(k).getTables().get(t); //遍历表格每行 for (int j = 0; j < table.getRows().getCount(); j++) { if (j == 0) { for (int d = 3; d < table.getRows().get(0).getCells().getCount(); d++) { TableCell cell = table.getRows().get(0).getCells().get(d); if (list1.size() > d - 3) { cell.addParagraph().appendText(CaseDataTime.casemd(list1.get(d - 3).getTaskName()));//月日 } } } else if (IsNumber.isNumeric(table.getRows().get(j).getCells().get(0).getFirstParagraph().getText().trim())) {//判断第一列是不是序号,数字(区分开执行人和备注) if (table.getRows().get(j).getCells().getCount() == table.getRows().get(0).getCells().getCount()) {//判断一行几列,第三列是否是第二列的子项,这是否的情况下,从第四列开始填 for (int q = 3; q < table.getRows().get(j).getCells().getCount(); q++) {// if (lists1.size() > q - 3) { if (lists1.get(q - 3).size() > j - 1 + count1) { table.getRows().get(j).getCells().get(q).getFirstParagraph().setText(CaseDataTime.Istrue(lists1.get(q - 3).get(j - 1 + count1).getResult().toString().trim())); } } } } else {//第三列是第二列的子项,所以列数多一位,应该从第五列开始 for (int q = 4; q < table.getRows().get(j).getCells().getCount(); q++) {//CaseDataTime.Istrue(lists.get(j - 1).get(q - 4).getResult().toString().trim()) if (lists1.size() > q - 4) { if (lists1.get(q - 4).size() > j - 1 + count1) { table.getRows().get(j).getCells().get(q).getFirstParagraph().appendText(CaseDataTime.Istrue(lists1.get(q - 4).get(j - 1 + count1).getResult().toString().trim())); } } } } } else if (table.getRows().get(j).getRowIndex() == table.getRows().getCount() - 2) {//如果行标等于总行数-1,说明是倒数第二行 for (int q = 1; q < table.getRows().get(j).getCells().getCount(); q++) { if (list1.size() > q - 1) { table.getRows().get(j).getCells().get(q).getFirstParagraph().appendText(list1.get(q - 1).getExecutionUser()); } } } else if (table.getRows().get(j).getRowIndex() == table.getRows().getCount() - 1) {//如果行标等于总行数-1,说明是倒数第二行list.get(q).getRemark() for (int q = 0; q < 7; q++) { if (list1.size() > q) { TableRow tableRow = table.getRows().get(j); System.out.println(tableRow.getCells().getCount()); TableCell tableCell = tableRow.getCells().get(2); DmRegularTask dmRegularTask = list1.get(q); tableCell.addParagraph().appendText(dmRegularTask.getTaskName() + ": " + dmRegularTask.getRemark()); } } } } if (table != null) { count1 = count1 + table.getRows().getCount() - 1; } } } } //保存文档 if (list.size()>0){ doc.saveToFile(path + list.get(0).getTaskName() + "--" + list.get(list.size() - 1).getTaskName() + ").doc", FileFormat.Docx_2013); }else { doc.saveToFile(path + list1.get(0).getTaskName() + "--" + list1.get(list1.size() - 1).getTaskName() + ").doc", FileFormat.Docx_2013); } doc.dispose(); }
最新回复(0)