2014-06-16 3 views
2

이 코드를 사용하여 Apache POI를 사용하여 xlsm 파일을 다시 작성합니다. 내가 결과 세트에서 Sheet1에 대한 데이터를 다시 작성 해야이 코드는 템플릿 xlsm 파일의 복사본을 만들고 모든 처리 않습니다.Apache POI를 사용하여 .xlsm 파일을 다시 작성하는 방법은 무엇입니까?

하지만 생성 된 XLSM 파일을 열 때 그것은 나에게이 메시지를 보여줍니다

우리는 'FileName.xlsm'의 일부 내용에 문제가 있음을 발견했다. 우리가 최대한 많이 복구하기를 원합니까? 이 통합 문서의 원본을 신뢰하는 경우 예를 클릭하십시오.

여기 내 코드입니다. 제발 내가해야 할 일을 제안하십시오. 완료

public void dbConnect(String driver_connect_string, String db_connect_string, String db_userid, String db_password){ 
    try{ 
    Class.forName(driver_connect_string); 
    Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password); 
    System.out.println("connected"); 
    Statement statement = conn.createStatement(); 
    Properties propq = new Properties();  
    FileInputStream fisq = new FileInputStream("query.properties"); 
    propq.load(fisq); 
    String queryString = propq.getProperty("myQueryString"); 
    ResultSet rs = statement.executeQuery(queryString); 

    Properties propf2 = new Properties(); 
    FileInputStream fisf2 = new FileInputStream("file.properties"); 
    propf2.load(fisf2); 

    OPCPackage pkg = OPCPackage.open(new File(propf2.getProperty("sourceFile"))); 
    XSSFWorkbook wb_template; 
    wb_template = new XSSFWorkbook(pkg); 
    System.out.println("package loaded"); 
    SXSSFWorkbook wb = new SXSSFWorkbook(wb_template); 
    wb.setCompressTempFiles(true); 

    Sheet sheet = wb.getSheetAt(0); 
    Row rowhead = sheet.createRow((short) 0); 
     rowhead.createCell((short) 0).setCellValue("EmpId"); 
     rowhead.createCell((short) 1).setCellValue("EmaName"); 
     rowhead.createCell((short) 2).setCellValue("Department"); 
     rowhead.createCell((short) 3).setCellValue("Job Title"); 
     rowhead.createCell((short) 4).setCellValue("DOB"); 
     int index = 1; 
     while (rs.next()) { 
       Row row = sheet.createRow((short) index); 
       row.createCell((short) 0).setCellValue(rs.getString(1)); 
       row.createCell((short) 1).setCellValue(rs.getString(2)); 
       row.createCell((short) 2).setCellValue(rs.getString(3)); 
       row.createCell((short) 3).setCellValue(rs.getString(4)); 
       row.createCell((short) 4).setCellValue(rs.getString(5)); 
       index++; 
      } 


     FileOutputStream out = new FileOutputStream(new File(propf2.getProperty("destFile"))); 
     System.out.println("XLSM created Successfully"); 
     wb.write(out); 
     out.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

답변

1

.......

public void dbConnect(String driver_connect_string, String db_connect_string, String db_userid, String db_password){ 
try{ 
    Class.forName(driver_connect_string); 
    Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password); 
    System.out.println("connected"); 
    Statement statement = conn.createStatement(); 
    Properties propq = new Properties();  
    FileInputStream fisq = new FileInputStream("query.properties"); 
    propq.load(fisq); 
    String queryString = propq.getProperty("myQueryString"); 
    ResultSet rs = statement.executeQuery(queryString); 

    Properties propf2 = new Properties(); 
    FileInputStream fisf2 = new FileInputStream("file.properties"); 
    propf2.load(fisf2); 

    OPCPackage pkg = OPCPackage.open(new File(propf2.getProperty("sourceFile"))); 
    XSSFWorkbook wb_template; 
    wb_template = new XSSFWorkbook(pkg); 
    System.out.println("package loaded"); 


    Sheet sheet = wb_template.getSheetAt(0); 

    Row rowhead = sheet.createRow(0); 
    rowhead.createCell(0).setCellValue("EmpId"); 
    rowhead.createCell(1).setCellValue("EmaName"); 
    rowhead.createCell(2).setCellValue("Department"); 
    rowhead.createCell(3).setCellValue("Job Title"); 
    rowhead.createCell(4).setCellValue("DOB"); 
    int index = 1; 
    while (rs.next()) { 
      Row row = sheet.createRow((short) index); 
      row.createCell(0).setCellValue(rs.getString(1)); 
      row.createCell(1).setCellValue(rs.getString(2)); 
      row.createCell(2).setCellValue(rs.getString(3)); 
      row.createCell(3).setCellValue(rs.getString(4)); 
      row.createCell(4).setCellValue(rs.getString(5)); 
      index++; 
     } 


    FileOutputStream out = new FileOutputStream(new File(propf2.getProperty("destFile"))); 
    System.out.println("XLSM created Successfully"); 
    wb.write(out); 
    out.close(); 
    }catch(Exception e){ 
    e.printStackTrace(); 
    } 
} 
관련 문제