I have written the following code which reads excel files from excelFilesPath, put the files with the same prefix in one excel file and rename the Excel file to have the prefix only. Example: The code merges the files a.b.1.xlsx and a.b.2.xlsx to one single excel file with the new name a.b.xlsx
private void mergeExcelFiles(String excelFilesPath) {
Map<String, List<List<String>>> moduleReportFiles = fileNames.entrySet().stream().collect(groupingBy(
e -> e.getKey().substring(0, e.getKey().lastIndexOf(".")), mapping(e -> e.getValue(), toList())));
Workbook workbookTo;
Workbook workbookFrom;
for (Entry<String, List<List<String>>> moduleNameReportsList : moduleReportFiles.entrySet()) {
List<String> files = moduleReportFiles.get(moduleNameReportsList.getKey()).stream().flatMap(List::stream)
.collect(Collectors.toList());
files = files.stream().sorted().collect(toList());
File f;
if (files.size() < 1) {// consider only modules that have at least 2 reports
continue;
} else if (files.size() == 1) { // if a module has only one report then rename it to the module name
f = new File(files.get(0));
f.renameTo(new File(excelFilesPath + "/" + moduleNameReportsList.getKey() + ".xlsx"));
} else {
try {
workbookTo = new XSSFWorkbook(new File(files.get(0)));
// workbookTo.setSheetOrder(files.get(0), 0);
f = new File(files.get(0));
f.delete();
files.remove(0);
for (String file : files) {
workbookFrom = new XSSFWorkbook(new File(file)); // workbook that needs to be merged
for (int sheetIndex = 0; sheetIndex < workbookFrom.getNumberOfSheets(); sheetIndex++) {
XSSFSheet sheetTo = (XSSFSheet) workbookTo
.createSheet(workbookFrom.getSheetAt(sheetIndex).getSheetName());
XSSFSheet sheetFrom = (XSSFSheet) workbookFrom.getSheetAt(sheetIndex);
XSSFRangeCopier xssfRangeCopier = new XSSFRangeCopier(sheetFrom, sheetTo);
int lastRow = sheetFrom.getLastRowNum();
int lastCol = 0;
for (int i = 0; i < lastRow; i++) {
Row row = sheetFrom.getRow(i);
if (row != null) {
if (row.getLastCellNum() > lastCol) {
lastCol = row.getLastCellNum();
}
sheetTo.setDefaultRowHeight(sheetFrom.getDefaultRowHeight());
}
}
for (int j = 0; j < lastCol; j++) {
sheetTo.setColumnWidth(j, sheetFrom.getColumnWidth(j));
}
CellRangeAddress cellAddresses = new CellRangeAddress(0, lastRow, 0, lastCol);
xssfRangeCopier.copyRange(cellAddresses, cellAddresses, true, true);
workbookTo.write(new FileOutputStream(
new File(excelFilesPath + "/" + moduleNameReportsList.getKey() + ".xlsx")));
}
f = new File(file);
if(f.delete())// delete older file
{
log.info("report file deleted successfully");
}
}
} catch (InvalidFormatException e1) {
log.error(e1.getMessage());
} catch (IOException e1) {
log.error(e1.getMessage());
} // the workbook that we merge all excel files into it
}
}
}
The code does that perfect. The Problem that I am facing now is that only one excel sheet has the original style. all other sheets shows borders some how which is wrong (find attached screenshot). How can I get the origin layout of all sheets?

From your picture the source sheets seems have set display gridlines to false. This is stored in sheet view settings of CTWorksheet for
XSSFSheet. So if the copied sheet also shall have set that so, then the sheet view settings of CTWorksheet also needs to be copied. To do so do:But
sheetViewsalso stores the setting about selected sheets, so set selected ofsheetTofalse, else multiple sheets will be selected in the result workbookBut additional you might get problems with the code lne:
What if multiple
workbookFromworkbooks have sheets with the same sheet name? Then this line will throwjava.lang.IllegalArgumentExceptionbecause theworkbookToworkbook already contains a sheet with this name. So the need is to consider that sheets from multiple workbookFrom could have the same name. If so, then append (n), where n is a counter number, to the name to make it unique in the result workbook.Complete example as Minimal, Reproducible Example:
Expects files
a.xlsx,b.xlsx,c.xlsxin directory./ExcelFilesand merges sheets formb.xlsxandc.xlsxintoa.xlsxand stores the result asresult.xlsx.