DEV ๐/JAVA
[JAVA] ์์ ํ์ผ(.xlsx) ์ฐ๊ธฐ / ์คํ์ผ ์ ์ฉ
DONI.
2020. 3. 17. 21:34
๋ฐ์ํ
โถ previous
1 POI ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค์ด๋ก๋ & ํ๋ก์ ํธ์ ์ ์ฉ
2 ์์
ํ์ผ(.xlsx) ์ฝ์ด์ค๊ธฐ & list์ ์ ์ฅ
[ ์์ ํ์ผ(.xlsx) ์ฐ๊ธฐ(์์ฑ)/์ ์ฅ/์คํ์ผ ์ ์ฉ ]
1 Font (๊ธ๊ผด)
XSSFFont Font = workbook.createFont();
→ ํฐํธ ์คํ์ผ ์์ฑ
Font.setFontHeightInPoints((short)14);
→ ๊ธ์ ํฌ๊ธฐ
Font.setFontName("๋ง์ ๊ณ ๋");
→ ๊ธ๊ผด ์ง์
Font.setBold(true);
→ ์งํ๊ฒ(true)
Style.setFont(Font);
→ ๋ง๋ค์ด๋์ CellStyle์ ํฐํธ ์ ์ฉ
public void writeExcel(ArrayList<exData> list) {
String path = Work4.class.getResource("").getPath(); //์ ์ฅํ ํ์ผ ๊ฒฝ๋ก
try {
File file = new File(path + "ExcelExFile.xlsx");
FileOutputStream fileout = new FileOutputStream(file);
XSSFWorkbook xworkbook = new XSSFWorkbook();
XSSFSheet xsheet = xworkbook.createSheet("๋ฉ๊ฐ๋ฐ์ค"); //์ํธ ์์ฑ
XSSFRow curRow;
int row = list.size(); //๋ฆฌ์คํธ ํฌ๊ธฐ
Cell cell = null;
//Title
curRow = xsheet.createRow(0);
cell = curRow.createCell(0);
cell.setCellValue("์ ๊ตญ ๋ฉ๊ฐ๋ฐ์ค ์ง์ ");
//Head
curRow = xsheet.createRow(1);
cell = curRow.createCell(0);
cell.setCellValue("์ด๋ฆ");
cell = curRow.createCell(1);
cell.setCellValue("๋น์จ");
cell = curRow.createCell(2);
cell.setCellValue("%");
//Body
for(int i=2;i<row;i++){
curRow = xsheet.createRow(i); // row ์์ฑ
cell = curRow.createCell(0);
cell.setCellValue(list.get(i).getName());
//์ด๋ฆ์ด '๋ํ'์ง์ ์ ํ์ PointStyle ์ ์ฉ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
cell = curRow.createCell(1);
cell.setCellValue(list.get(i).getNum());
//์
์คํ์ผ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
cell = curRow.createCell(2);
cell.setCellValue(list.get(i).getPer());
//์
์คํ์ผ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
}
//์ด ๋๋น ์ค์
for(int i=0;i<3;i++) {
xsheet.autoSizeColumn(i);
xsheet.setColumnWidth(i, (xsheet.getColumnWidth(i))+256);
}
xworkbook.write(fileout);
fileout.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
wirteExcel() ์ ์ฒด ์ฝ๋
public void writeExcel(ArrayList<exData> list) {
String path = Work4.class.getResource("").getPath(); //์ ์ฅํ ํ์ผ ๊ฒฝ๋ก
try {
File file = new File(path + "ExcelExFile.xlsx");
FileOutputStream fileout = new FileOutputStream(file);
XSSFWorkbook xworkbook = new XSSFWorkbook();
//Title
XSSFFont TitleFont = xworkbook.createFont();
TitleFont.setFontHeightInPoints((short)14);
TitleFont.setFontName("๋ง์ ๊ณ ๋");
TitleFont.setBold(true);
CellStyle TitleStyle = xworkbook.createCellStyle();
TitleStyle.setAlignment(HorizontalAlignment.CENTER);
TitleStyle.setFont(TitleFont);
//Body
XSSFFont BodyFont = xworkbook.createFont();
BodyFont.setFontHeightInPoints((short)11);
BodyFont.setFontName("๋ง์ ๊ณ ๋");
CellStyle BodyStyle = xworkbook.createCellStyle();
BodyStyle.setAlignment(HorizontalAlignment.CENTER);
BodyStyle.setBorderTop(BorderStyle.THIN);
BodyStyle.setBorderBottom(BorderStyle.THIN);
BodyStyle.setBorderLeft(BorderStyle.THIN);
BodyStyle.setBorderRight(BorderStyle.THIN);
BodyStyle.setFont(BodyFont);
//Point
CellStyle PointStyle = xworkbook.createCellStyle();
PointStyle.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
PointStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
PointStyle.setAlignment(HorizontalAlignment.CENTER);
PointStyle.setBorderTop(BorderStyle.THIN);
PointStyle.setBorderBottom(BorderStyle.THIN);
PointStyle.setBorderLeft(BorderStyle.THIN);
PointStyle.setBorderRight(BorderStyle.THIN);
PointStyle.setFont(BodyFont);
//Header
XSSFFont HeadFont = xworkbook.createFont();
HeadFont.setBold(true);
HeadFont.setFontHeightInPoints((short)11);
HeadFont.setFontName("๋ง์ ๊ณ ๋");
CellStyle HeadStyle = xworkbook.createCellStyle();
HeadStyle.setAlignment(HorizontalAlignment.CENTER);
HeadStyle.setFillForegroundColor(HSSFColorPredefined.GREY_25_PERCENT.getIndex());
HeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
HeadStyle.setBorderTop(BorderStyle.THIN);
HeadStyle.setBorderBottom(BorderStyle.THIN);
HeadStyle.setBorderLeft(BorderStyle.THIN);
HeadStyle.setBorderRight(BorderStyle.THIN);
HeadStyle.setFont(HeadFont);
XSSFSheet xsheet = xworkbook.createSheet("๋ฉ๊ฐ๋ฐ์ค"); //์ํธ ์์ฑ
XSSFRow curRow;
int row = list.size(); //๋ฆฌ์คํธ ํฌ๊ธฐ
Cell cell = null;
//Title
curRow = xsheet.createRow(0);
cell = curRow.createCell(0);
cell.setCellValue("์ ๊ตญ ๋ฉ๊ฐ๋ฐ์ค ์ง์ ");
cell.setCellStyle(TitleStyle);
//Head
curRow = xsheet.createRow(1);
cell = curRow.createCell(0);
cell.setCellValue("์ด๋ฆ");
cell.setCellStyle(HeadStyle);
cell = curRow.createCell(1);
cell.setCellValue("๋น์จ");
cell.setCellStyle(HeadStyle);
cell = curRow.createCell(2);
cell.setCellValue("%");
cell.setCellStyle(HeadStyle);
//Body
for(int i=2;i<row;i++){
curRow = xsheet.createRow(i); // row ์์ฑ
cell = curRow.createCell(0);
cell.setCellValue(list.get(i).getName());
//์
์คํ์ผ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
cell = curRow.createCell(1);
cell.setCellValue(list.get(i).getNum());
//์
์คํ์ผ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
cell = curRow.createCell(2);
cell.setCellValue(list.get(i).getPer());
//์
์คํ์ผ
if(list.get(i).getName().equals("๋ํ")) {
cell.setCellStyle(PointStyle);
} else{ cell.setCellStyle(BodyStyle); }
}
//์ด ๋๋น ์ค์
for(int i=0;i<3;i++) {
xsheet.autoSizeColumn(i);
xsheet.setColumnWidth(i, (xsheet.getColumnWidth(i))+256);
}
xworkbook.write(fileout);
fileout.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
๋ฐ์ํ