참고용 예제
public void saveExcel() {
String excelFile = "helllooo" + ".xlsx";
int rowNum = 0;
for (int i = 0; i < saleList.size(); i++) {
dayList.get(i).setCount(dayList.get(i).getCount() + saleList.get(i).getCount());
}
SXSSFWorkbook wb = new SXSSFWorkbook(100);
// 셀 스타일
CellStyle ct = wb.createCellStyle();
ct.setAlignment(CellStyle.ALIGN_CENTER);
Sheet sheet = wb.createSheet("First sheet");
if (sheet == null) {
System.out.println("sheet is null!");
return;
}
sheet.setColumnWidth(1, 9000);
Row row1 = sheet.createRow(rowNum);
rowNum++;
Cell cell_1 = row1.createCell(0);
cell_1.setCellValue("제품번호");
cell_1.setCellStyle(ct);
Cell cell_2 = row1.createCell(1);
cell_2.setCellValue("이름");
cell_2.setCellStyle(ct);
Cell cell_3 = row1.createCell(2);
cell_3.setCellValue("구매 개수");
cell_3.setCellStyle(ct);
Cell cell_4 = row1.createCell(3);
cell_4.setCellValue("가격");
cell_4.setCellStyle(ct);
for (int i = 0; i < dayList.size(); i++) {
Row row = sheet.createRow(rowNum);
for (int j = 0; j < 4; j++) {
Cell cel_1 = row.createCell(0);
cel_1.setCellValue(dayList.get(i).getSerial_no());
Cell cel_2 = row.createCell(1);
cel_2.setCellValue(dayList.get(i).getName());
Cell cel_3 = row.createCell(2);
cel_3.setCellValue(dayList.get(i).getCount());
cel_3.setCellStyle(ct);
Cell cel_4 = row.createCell(3);
cel_4.setCellValue(dayList.get(i).getPrice() * dayList.get(i).getCount());
}
rowNum++;
}
try {
FileOutputStream fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
fileOut.close();
wb.dispose();
JOptionPane.showMessageDialog(null, "엑셀로 저장 완료 (파일 이름 :" + excelFile + ")");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
'공부 > JAVA' 카테고리의 다른 글
(spring boot) org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stackResourceRegistryFactoryBean' (0) | 2019.03.25 |
---|---|
(java) txt파일 읽기 tab구분 (0) | 2016.08.15 |
(java) 현재 시간 가져오기 (0) | 2016.08.14 |
(java) 문자열 byte크기 알아내기 (0) | 2016.08.14 |
(함수) 배열 복사 System.arraycopy() (2) | 2016.03.09 |