1
This commit is contained in:
@@ -8,6 +8,9 @@ import com.example.ts_obj.bean.ReturnCodeAndMsgEnum;
|
||||
import com.example.ts_obj.bean.ReturnValue;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -15,12 +18,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dataApi")
|
||||
@@ -28,7 +30,8 @@ import java.util.Map;
|
||||
public class DataapiController {
|
||||
private final static Logger logger = LoggerFactory.getLogger(DataapiController.class);
|
||||
|
||||
|
||||
private static final String INPUT_DIR = "D:/11/";
|
||||
private static final String OUTPUT_DIR = "D:/22/";
|
||||
// private String BASE_DIR="d:/upload/";
|
||||
private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
|
||||
|
||||
@@ -60,4 +63,68 @@ public class DataapiController {
|
||||
}
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.Success,result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@PostMapping("/clearDocx")
|
||||
public void clearDocx(){
|
||||
File inputFolder = new File(INPUT_DIR);
|
||||
File outputFolder = new File(OUTPUT_DIR);
|
||||
if (!outputFolder.exists()) {
|
||||
outputFolder.mkdirs();
|
||||
}
|
||||
|
||||
File[] files = inputFolder.listFiles((dir, name) -> name.toLowerCase().endsWith(".docx"));
|
||||
if (files == null || files.length == 0) {
|
||||
System.out.println("未找到 .docx 文件");
|
||||
return;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
System.out.println("正在处理:" + file.getName());
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
XWPFDocument document = new XWPFDocument(fis)) {
|
||||
|
||||
for (XWPFParagraph para : document.getParagraphs()) {
|
||||
List<XWPFRun> runs = para.getRuns();
|
||||
for (int i = runs.size() - 1; i >= 0; i--) {
|
||||
XWPFRun run = runs.get(i);
|
||||
boolean hasDrawing = run.getCTR().getDrawingList().size() > 0;
|
||||
boolean hasPicture = run.getEmbeddedPictures().size() > 0;
|
||||
boolean hasPict = run.getCTR().getPictList().size() > 0;
|
||||
|
||||
if (hasDrawing || hasPicture || hasPict) {
|
||||
para.removeRun(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// List<XWPFParagraph> paragraphs = document.getParagraphs();
|
||||
|
||||
// for (XWPFParagraph para : paragraphs) {
|
||||
// String originalText = para.getText();
|
||||
// String cleaned = originalText
|
||||
// .replaceAll("\\s+", " ") // 合并空白
|
||||
// .replaceAll("[^\\p{L}\\p{N}\\s]", "") // 去除特殊字符
|
||||
// .trim();
|
||||
// // 替换段落内容(注意:POI 不允许直接 setText,需要新建 run)
|
||||
// para.removeRun(0);
|
||||
// para.createRun().setText(cleaned);
|
||||
// }
|
||||
|
||||
File outputFile = new File(OUTPUT_DIR + file.getName());
|
||||
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
|
||||
document.write(fos);
|
||||
}
|
||||
|
||||
System.out.println("已保存至:" + outputFile.getAbsolutePath());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("处理失败:" + file.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.example.ts_obj.service.TypesetService;
|
||||
import com.example.ts_obj.service.UserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.models.auth.In;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
@@ -65,12 +66,12 @@ public class TypesetController {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
// private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
|
||||
// private String IMG_BASE_DIR = "/home/wwwroot/api.tmrjournals.com/public/articleImage/";
|
||||
private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
|
||||
private String IMG_BASE_DIR = "/home/wwwroot/api.tmrjournals.com/public/articleImage/";
|
||||
|
||||
|
||||
private String BASE_DIR = "D:/tsfile/";
|
||||
private String IMG_BASE_DIR = "D:/phpstudy_pro/WWW/tougao/public/articleImage/";
|
||||
// private String BASE_DIR = "D:/tsfile/";
|
||||
// private String IMG_BASE_DIR = "D:/phpstudy_pro/WWW/tougao/public/articleImage/";
|
||||
|
||||
|
||||
@Autowired
|
||||
@@ -1374,25 +1375,81 @@ public class TypesetController {
|
||||
Map<String, String> stringStringMap = map.get(tid);
|
||||
ArrayList<ArrayList<Map<String, String>>> tableData = JSON.parseObject(stringStringMap.get("table_data"), new TypeReference<ArrayList<ArrayList<Map<String, String>>>>() {});
|
||||
TableRenderData tableRenderData = new TableRenderData();
|
||||
MergeCellRule.MergeCellRuleBuilder builder = MergeCellRule.builder();
|
||||
int t_row = 0;
|
||||
int t_col = 0 ;
|
||||
HashMap<Integer, HashMap<Integer, List<ParagraphRenderData>>> mm = new HashMap<>();
|
||||
for (ArrayList<Map<String, String>> row:tableData){
|
||||
RowRenderData rowRenderData = new RowRenderData();
|
||||
t_col = 0;
|
||||
if(!mm.containsKey(t_row)){
|
||||
mm.put(t_row,new HashMap<Integer,List<ParagraphRenderData>>());
|
||||
}
|
||||
for (Map<String,String> rowBean:row){
|
||||
CellRenderData cellRenderData = new CellRenderData();
|
||||
ParagraphRenderData text = crowStrNew(rowBean.get("text").trim(), map,images);
|
||||
while (mm.get(t_row).containsKey(t_col)&&mm.get(t_row).get(t_col)==null){
|
||||
t_col++;
|
||||
}
|
||||
ParagraphRenderData text = crowStrNew(rowBean.get("text"), map,images);
|
||||
List<ParagraphRenderData> plist = new ArrayList<>();
|
||||
plist.add(text);
|
||||
cellRenderData.setParagraphs(plist);
|
||||
mm.get(t_row).put(t_col,plist);
|
||||
if(Integer.valueOf(rowBean.get("colspan"))>1){
|
||||
int cc_i = Integer.valueOf(rowBean.get("colspan"));
|
||||
for (int o = 1;o<cc_i;o++){
|
||||
mm.get(t_row).put(t_col+o,null);
|
||||
}
|
||||
}
|
||||
if(Integer.valueOf(rowBean.get("rowspan"))>1){
|
||||
int cc_j = Integer.valueOf(rowBean.get("rowspan"));
|
||||
for (int o = 1 ; o<cc_j;o++){
|
||||
if(!mm.containsKey(t_row+o)){
|
||||
mm.put(t_row+o,new HashMap<Integer,List<ParagraphRenderData>>());
|
||||
}
|
||||
mm.get(t_row+o).put(t_col,null);
|
||||
}
|
||||
}
|
||||
if(Integer.valueOf(rowBean.get("colspan"))>1||Integer.valueOf(rowBean.get("rowspan"))>1){
|
||||
int cc_r = t_row+Integer.valueOf(rowBean.get("rowspan"))-1;
|
||||
int cc_c = t_col+Integer.valueOf(rowBean.get("colspan"))-1;
|
||||
builder.map(MergeCellRule.Grid.of(t_row,t_col), MergeCellRule.Grid.of(cc_r,cc_c));
|
||||
}
|
||||
t_col++;
|
||||
}
|
||||
t_row++;
|
||||
}
|
||||
|
||||
|
||||
for (Map.Entry<Integer,HashMap<Integer,List<ParagraphRenderData>>> entry: mm.entrySet()){
|
||||
RowRenderData rowRenderData = new RowRenderData();
|
||||
for (Map.Entry<Integer,List<ParagraphRenderData>> en:entry.getValue().entrySet()){
|
||||
CellRenderData cellRenderData = new CellRenderData();
|
||||
cellRenderData.setParagraphs(en.getValue());
|
||||
rowRenderData.addCell(cellRenderData);
|
||||
}
|
||||
RowStyle rowStyle = new RowStyle();
|
||||
rowStyle.setHeight(12);
|
||||
rowRenderData.setRowStyle(rowStyle);
|
||||
tableRenderData.addRow(rowRenderData);
|
||||
}
|
||||
// TableStyle tableStyle = new TableStyle();
|
||||
// tableStyle.setAlign(TableRowAlign.CENTER);
|
||||
// tableRenderData.setTableStyle(tableStyle);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for (ArrayList<Map<String, String>> row:tableData){
|
||||
// RowRenderData rowRenderData = new RowRenderData();
|
||||
// for (Map<String,String> rowBean:row){
|
||||
// CellRenderData cellRenderData = new CellRenderData();
|
||||
// ParagraphRenderData text = crowStrNew(rowBean.get("text").trim(), map,images);
|
||||
// List<ParagraphRenderData> plist = new ArrayList<>();
|
||||
// plist.add(text);
|
||||
// cellRenderData.setParagraphs(plist);
|
||||
// rowRenderData.addCell(cellRenderData);
|
||||
// }
|
||||
// RowStyle rowStyle = new RowStyle();
|
||||
// rowStyle.setHeight(12);
|
||||
// rowRenderData.setRowStyle(rowStyle);
|
||||
// tableRenderData.addRow(rowRenderData);
|
||||
// }
|
||||
// builder.map(MergeCellRule.Grid.of(0,1), MergeCellRule.Grid.of(0,2));
|
||||
|
||||
tableRenderData.setMergeRule(builder.build());
|
||||
p.addTable(tableRenderData);
|
||||
return p;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user