I am seeking a solution to replicate an entire Excel sheet, inclusive of its tables and charts, in a Word document while maintaining the original styles.

I have attempted the following code to transfer Excel sheet data to a Word document. However, the resulting cell style and table structure differ somewhat from the original Excel format. I am looking for a solution to maintain the exact Excel format in the Word document.

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class CopyExcelTableToWord {

    public static void main(String[] args) {
        try {
            FileInputStream excelFile = new FileInputStream(
                    new File("D:\GIT\modules\core\bin\logs\Test.xlsx"));
            try (XSSFWorkbook workbook = new XSSFWorkbook(excelFile)) {
                XSSFSheet sheet = workbook.getSheetAt(0);

                try (XWPFDocument document = new XWPFDocument()) {
                    XWPFTable table = document.createTable();

                    // Iterate through rows and cells in the Excel table
                    for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                        XWPFTableRow row = table.createRow();

                        Row excelRow = sheet.getRow(rowNum);
                        if (excelRow != null) {
                            // Iterate through cells in the Excel row
                            for (int cellNum = 0; cellNum < excelRow.getLastCellNum(); cellNum++) {
                                Cell excelCell = excelRow.getCell(cellNum);
                                XWPFTableCell wordCell = row.createCell();

                                if (excelCell != null) {
                                    // Copy cell value
                                    wordCell.setText(getCellValue(excelCell));

                                    // Copy cell style
                                    copyCellStyle(excelCell, wordCell);
                                }
                            }
                        }
                    }

                    // Save the Word document
                    File file = new File("output.docx");
                    FileOutputStream outFile = new FileOutputStream(file);
                    document.write(outFile);
                    outFile.close();
                    Desktop.getDesktop().open(file);
                }
            }
            System.out.println("Excel table copied to Word document successfully.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return "";
        }
    }

    private static void copyCellStyle(Cell sourceCell, XWPFTableCell targetCell) {
        XSSFCellStyle sourceStyle = (XSSFCellStyle) sourceCell.getCellStyle();
        XWPFParagraph targetParagraph = targetCell.getParagraphs().get(0);

        if (sourceStyle != null) {
            // Copy font
            XSSFFont sourceFont = sourceStyle.getFont();
            XWPFRun run = targetParagraph.createRun();
            run.setFontFamily(sourceFont.getFontName());
            run.setFontSize(sourceFont.getFontHeightInPoints());

            // Copy fill color
            if (sourceStyle.getFillForegroundColorColor() != null) {
                XSSFColor sourceColor = (XSSFColor) sourceStyle.getFillForegroundColorColor();
                if (sourceColor != null) {
                    targetParagraph.getCTP().getPPr().addNewShd().setFill(sourceColor.getRGB());
                }
            }

            // Copy text alignment
            if (sourceStyle.getAlignment() != null) {
                targetParagraph.setAlignment(getParagraphAlignment(sourceStyle.getAlignment()));
            }
        }
    }

    private static ParagraphAlignment getParagraphAlignment(HorizontalAlignment horizontalAlignment) {
        switch (horizontalAlignment) {
            case LEFT:
                return ParagraphAlignment.LEFT;
            case CENTER:
                return ParagraphAlignment.CENTER;
            case RIGHT:
                return ParagraphAlignment.RIGHT;
            default:
                return ParagraphAlignment.LEFT;
        }
    }

}