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;
public class ExcelUtil {
public static void exportData(String templatePath, String outputPath, Data data) throws IOException {
FileInputStream fis = new FileInputStream(templatePath);
Workbook workbook = new XSSFWorkbook(fis);
fis.close();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.createCell(1);
cell.setCellValue(data.getName());
FileOutputStream fos = new FileOutputStream(outputPath);
workbook.write(fos);
fos.close();
workbook.close();
}
public static Data importData(String templatePath) throws IOException {
FileInputStream fis = new FileInputStream(templatePath);
Workbook workbook = new XSSFWorkbook(fis);
fis.close();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell = row.getCell(1);
String name = cell.getStringCellValue();
Data data = new Data();
data.setName(name);
workbook.close();
return data;
}
public static void main(String[] args) {
try {
Data data = new Data();
exportData("template.xlsx", "output.xlsx", data);
Data importedData = importData("output.xlsx");
System.out.println(importedData.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}