[ Java ] 자바로 엑셀파일 생성하기


코드를 다 공개할수가 없어서 많이 뺐지만..

String gourl = "";
String id = "";
String name = "";
String title = "";
ArrayList List_email = new ArrayList();
ArrayList List_id = new ArrayList();
ArrayList List_title = new ArrayList();
for(int p = 1; p <= 80; p++) {
    gourl = "타겟페이지?parm="+p;
    driver.get(gourl);

    WebElement iframe = driver.findElement(By.cssSelector("#main"));
    driver.switchTo().frame(iframe);
    Thread.sleep(1000);

    ParsingResult pr = parser.parse(driver.getPageSource(), "정보추출");
    if (pr != null && pr.getAttributeSize("아이디") > 1) {
        for(int i=0; i<pr.getAttributeSize("아이디"); i++) {
            id = pr.getAttribute("아이디",i);
            name = pr.getAttribute("닉네임",i);
            title = pr.getAttribute("제목",i);

            List_email.add(id+"@gmail.com");
            List_id.add(name);
            List_title.add(title);
        }

        String excelFileName = "D:\\myfolder\\test.xlsx";//엑셀파일 이름지정

        String sheetName = "Sheet1";//시트이름

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName) ;


        for (int r=0;r < List_email.size()-1; r++ )
        {
            XSSFRow row = sheet.createRow(r);

            for (int c=0;c < 3; c++ )
            {
                XSSFCell cell_email = row.createCell(0);
                XSSFCell cell_id = row.createCell(1);
                XSSFCell cell_title = row.createCell(2);

                cell_email.setCellValue(List_email.get(r)+"");
                cell_id.setCellValue(List_id.get(r)+"");
                cell_title.setCellValue(List_title.get(r)+"");
            }
        }

        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream(excelFileName);
            try {
                wb.write(fileOut);
                fileOut.flush();
                fileOut.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}





© 2018. by sora

Powered by sora