code

Apache POI와 함께 Excel 템플릿 사용

starcafe 2023. 7. 17. 21:16
반응형

Apache POI와 함께 Excel 템플릿 사용

기본 질문:POI와 함께 사용하기 위해 Excel 템플릿을 로드한 다음 XLS 파일에 저장하려면 어떻게 해야 합니까?

편집:

답은 다음과 같습니다.

FileInputStream inputStream = new FileInputStream(new File(templateFile));
Workbook workbook = new HSSFWorkbook(inputStream);

(템플릿을 워크북으로 로드한 다음 워크북을 다른 곳에서 XLS 파일로 작성하기만 하면 됩니다.)

템플릿으로 사용할 .xls를 직접 로드하고 수정할 수 있습니다.

POIFSFileSystem fs = new POIFSFileSystem(
                new FileInputStream("template.xls"));
HSSFWorkbook wb = new  HSSFWorkbook(fs, true);

구조(매크로 포함)를 보존하면서 xl을 로드합니다.그런 다음 수정할 수 있습니다.

HSSFSheet sheet1 = wb.getSheet("Data");
...

그런 다음 저장합니다.

FileOutputStream fileOut = new FileOutputStream("new.xls"); 
wb.write(fileOut);
fileOut.close();

이게 도움이 되길 바랍니다.

POI를 사용하여 표준 .xls로 로드하고 수정한 후 저장해 본 적이 있습니까?

POI 생성 .xls에 매크로를 삽입할 때 사용한 접근 방식입니다.매크로로 파일을 만든 다음(.xls로 인정) 앱에 로드하여 데이터를 채우고 새로 만든 .xls로 저장합니다.다 잘 됐어요.

내부 템플릿을 리소스로 사용할 수도 있습니다.

InputStream fis = ChartSample.class.getResourceAsStream("/templates.xls");
HSSFWorkbook wb = new HSSFWorkbook(fis);        
fis.close();
HSSFSheet sh = wb.getSheetAt(0); 
//Here you go

그리고 그것을 저장합니다.

out = new FileOutputStream("./new.xls");
wb.write(out);
out.close();

XLS 템플릿에서 XLS 파일을 생성할 수 있습니다.

그러나 이렇게 하려면 템플릿을 사용해야 할 때마다 템플릿의 복사본을 만들어야 합니다.그렇지 않은 경우 원본 템플릿(원하지 않는 템플릿)을 편집합니다.

먼저 템플릿 파일을 가져와야 합니다.

URL url = Thread.currentThread().getContextClassLoader().getResource("templates/template.xls");
File file = new File(url.getPath());

템플릿 파일 복사:

try (FileOutputStream fileOutputStream = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {

    Files.copy(file.toPath(), fileOutputStream);

    Workbook workbook = new HSSFWorkbook();
    workbook.write(fileOutputStream);
}

새 복사된 파일에 액세스합니다.

FileInputStream inp = new FileInputStream("/home/jake/fileCopiedFromTemplate.xls");

작성Workbook새 파일에 다음과 같이 기록할 수 있습니다.

Workbook workbook = WorkbookFactory.create(inp);

워크북에 다음을 기록합니다.

try (FileOutputStream fileOut = new FileOutputStream("/home/jake/fileCopiedFromTemplate.xls")) {
    workbook.write(fileOut);
}

XLS 템플릿 파일을 만드는 팁은 채울 위치를 현지화할 수 있도록 템플릿에 몇 가지 변수를 표시하는 것입니다.예:

------------------------------------
|   | Columna A     | Column B     |
------------------------------------
| 1 |  Some description            | 
------------------------------------
| 2 | {person.name} | {person.age} |
------------------------------------

의 Excel 파일의 경우.xlsx다음을 사용합니다.

FileInputStream inputStream = new FileInputStream(new File("template.xlsx"));
        @SuppressWarnings("resource")
        Workbook wb = new XSSFWorkbook(inputStream);  
        Sheet sheet = wb.getSheet("sheet1");

2007+ 형식으로 저장하려면 maven pom.xml에 poi-ooxml 종속성을 추가하여 XSSF를 사용할 수 있습니다.

템플릿이 있는 경우.이 xmls에 요약 시트가 있는 xlsx 파일에서 특정 셀을 변경하려는 경우 다음과 같이 수행할 수 있습니다.

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("template.xlsx"));           
FileOutputStream fileOut = new FileOutputStream("new.xlsx");
XSSFSheet sheet1 = wb.getSheet("Summary");
XSSFRow row = sheet1.getRow(15);
XSSFCell cell = row.getCell(3);
cell.setCellValue("Bharthan");

wb.write(fileOut);
log.info("Written xls file");
fileOut.close();

언급URL : https://stackoverflow.com/questions/714172/using-excel-templates-with-apache-poi

반응형