feat: get all marks

This commit is contained in:
zzs 2025-03-01 10:20:09 +08:00
parent a41c59287f
commit d10504beba
2 changed files with 46 additions and 29 deletions

View File

@ -61,6 +61,12 @@
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
<build>

View File

@ -8,20 +8,14 @@ import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import vin.vio.usepoitl.model.TableData;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
@ -65,32 +59,39 @@ public class ApplicationStartExec implements ApplicationListener<ApplicationStar
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
XWPFTemplate allTemplate = XWPFTemplate.compile(wordFile.getAbsolutePath());
List<MetaTemplate> list = allTemplate.getElementTemplates();
boolean onSameLine = false;
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
log.info("=====all tag=====");
for (MetaTemplate metaTemplate : list) {
if (metaTemplate instanceof ElementTemplate ele) {
log.info(ele.getTagName());
XWPFRun run = ((RunTemplate) ele).getRun();
if (run.getParent() instanceof XWPFTableCell) {
XWPFTableCell cell = (XWPFTableCell) run.getParent();
XWPFTableRow row = cell.getTableRow();
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell tableCell : cells) {
String cellText = tableCell.getText();
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher(cellText);
while (matcher.find()) {
String colTag = matcher.group(1);
log.info("colTag {} {}", ele.getTagName(),colTag);
}
if (!"table".equals(ele.getTagName())) {
continue;
}
}
if (metaTemplate instanceof RunTemplate runTemplate) {
XWPFRun run = runTemplate.getRun();
XWPFTableCell tagCell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
XWPFTable table = tagCell.getTableRow().getTable();
int templateRowIndex = getTemplateRowIndex(tagCell, onSameLine);
XWPFTableRow row = table.getRow(templateRowIndex);
for (XWPFTableCell cell : row.getTableCells()) {
String text = cell.getText();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String colTag = matcher.group(1);
log.info("colTag {}", colTag);
}
}
}
}
log.info("=====all tag=====END");
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy(onSameLine);
Configure config = Configure.builder()
.bind("table", policy).build();
@ -105,12 +106,12 @@ public class ApplicationStartExec implements ApplicationListener<ApplicationStar
BigDecimal amt = qty.multiply(price);
total_amt = total_amt.add(amt);
tableDate.add(TableData.builder()
.index(i)
.m_code("AAA__" + i)
.m_name("BBB___" + i)
.m_qty(qty.setScale(2, RoundingMode.HALF_UP).toPlainString())
.m_unit_price(price.setScale(2, RoundingMode.HALF_UP).toPlainString())
.m_amount(amt.setScale(2, RoundingMode.HALF_UP).toPlainString())
.index(i)
.m_code("AAA__" + i)
.m_name("BBB___" + i)
.m_qty(qty.setScale(2, RoundingMode.HALF_UP).toPlainString())
.m_unit_price(price.setScale(2, RoundingMode.HALF_UP).toPlainString())
.m_amount(amt.setScale(2, RoundingMode.HALF_UP).toPlainString())
.build());
}
@ -129,4 +130,14 @@ public class ApplicationStartExec implements ApplicationListener<ApplicationStar
}});
template.writeAndClose(new FileOutputStream("output.docx"));
}
private int getTemplateRowIndex(XWPFTableCell tagCell, boolean onSameLine) {
XWPFTableRow tagRow = tagCell.getTableRow();
return onSameLine ? getRowIndex(tagRow) : (getRowIndex(tagRow) + 1);
}
private int getRowIndex(XWPFTableRow row) {
List<XWPFTableRow> rows = row.getTable().getRows();
return rows.indexOf(row);
}
}