import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
public class ExcelReportGenerator {
private Workbook workbook;
private Sheet sheet;
private Map<String, Object> data;
public ExcelReportGenerator(String templatePath, Map<String, Object> data) throws IOException {
FileInputStream fileInputStream = new FileInputStream(templatePath);
this.workbook = new XSSFWorkbook(fileInputStream);
this.sheet = workbook.getSheetAt(0);
this.data = data;
}
public void generateReport(String outputFilePath) throws IOException {
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = cell.getStringCellValue();
if (cellValue.startsWith("$")) {
String key = cellValue.substring(1);
Object value = data.get(key);
if (value != null) {
cell.setCellValue(value.toString());
}
}
}
}
FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
workbook.write(fileOutputStream);
workbook.close();
fileOutputStream.close();
}
}
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String templatePath = "path/to/template.xlsx";
String outputFilePath = "path/to/output.xlsx";
Map<String, Object> data = new HashMap<>();
data.put("name", "John Doe");
data.put("age", 30);
data.put("company", "ABC Corporation");
try {
ExcelReportGenerator reportGenerator = new ExcelReportGenerator(templatePath, data);
reportGenerator.generateReport(outputFilePath);
System.out.println("Report generated successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}