728x90
반응형

엑셀을 쓸일이 있어서 테스트해 보았다 .

사이트는 다음을 참조

http://poi.apache.org/index.html

 

빠른 사용을 위한 퀵 가이드 .. 

http://poi.apache.org/spreadsheet/quick-guide.html


Date 타입을 넣기전까지 코드는 훨씬 간단했다 . ^^;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Test
public void ExcelWriteTest()
{
    int iColIdx = 0;
 
    // excel name
    String fileName = "d:\\MyExcelWriteTest.xls";
    HSSFWorkbook wb = new HSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row0 = sheet.createRow(0);
 
    // header setting
    row0.createCell(iColIdx++).setCellValue(new HSSFRichTextString("Name"));
    row0.createCell(iColIdx++).setCellValue(new HSSFRichTextString("Date"));
    row0.createCell(iColIdx++).setCellValue(new HSSFRichTextString("Age"));
 
    for (int i=1; i < 10; i++) {
        HSSFRow rowi = sheet.createRow(i);
 
        iColIdx = 0;
        rowi.createCell(iColIdx++).setCellValue(new HSSFRichTextString("[Name]" + i));
 
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
        Cell cell = rowi.createCell(iColIdx++);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(new Date());
        //  cell.setCellValue(Calendar.getInstance());
        rowi.createCell(iColIdx++).setCellValue(10+i);
    }
 
    // Write the output to a file
    try{
        FileOutputStream fileOut = new FileOutputStream(fileName);
        wb.write(fileOut);
        fileOut.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}
728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,