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
'code' 카테고리의 다른 글
플라스크에 저장하지 않고 파일 데이터 읽기 (0) | 2023.07.17 |
---|---|
numpy 배열에 0만 포함되어 있는지 테스트 (0) | 2023.07.17 |
이 앱은 Firebase 인증을 사용할 수 있는 권한이 없습니다.Firebase 콘솔에서 올바른 패키지 이름과 SHA-1이 구성되어 있는지 확인하십시오. (0) | 2023.07.07 |
application.properties outside jar 파일 방법 (0) | 2023.07.07 |
Next3 - 미들웨어에서 Vuex 스토어에 액세스하시겠습니까? (0) | 2023.07.07 |