public static void main(String[] args) {
Map<String, List<List<List<String>>>> r2 = ExcelToConsole.getData(); // Replace with your actual data
String outputFilePath = "src/main/resources/output.docx"; // Replace with your desired output file path
try (FileOutputStream outputStream = new FileOutputStream(outputFilePath)) {
XWPFDocument document = new XWPFDocument();
// Iterate through the map and write table for each entry
for (Map.Entry<String, List<List<List<String>>>> entry : r2.entrySet()) {
String key = entry.getKey();
List<List<List<String>>> value = entry.getValue();
// Create a paragraph with the key (file name)
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("File Name: " + key);
// Iterate through the value (list of tables) and write each table
for (List<List<String>> tableData : value) {
XWPFTable table = document.createTable();
writeDataToTable(table, tableData);
}
}
// Write the document content to the output file
document.write(outputStream);
System.out.println("Tables written to Word document successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeDataToTable(XWPFTable table, List<List<String>> data) {
for (List<String> row : data) {
XWPFTableRow tableRow = table.createRow();
for (String cellValue : row) {
XWPFTableCell cell = tableRow.createCell();
setCellTextStyle(cell, cellValue); // Apply custom style to cell text
}
}
}
public static void setCellTextStyle(XWPFTableCell cell, String text) {
XWPFParagraph paragraph = cell.addParagraph();
XWPFRun run = paragraph.createRun();
run.setText(text);
run.setFontSize(12); // Set font size to 12 points (小四)
run.setColor("FF0000"); // Set font color to red
run.setFontFamily("宋体"); // Set font family
}
帮看下有啥问题,为什么插入的时候,左面多了一列。