2014-02-24 1 views
4

문서를 가지고 편집 중이거나 자바를 사용하여 데이터를 입력하고 싶습니다. 시도하지만 모두 할 수 있습니다. 내 Excel에서 데이터를 삭제하고 방금 입력 한 데이터로 바꿉니다. Netbeans를 사용하여 제 컴파일러를 사용하고 있습니다. 내 출력은 GUI/JFrame 특정 열, 행 및 시트에 데이터를 삽입하는 방법 ?? 예를 들어 나는아파치 POI - 특정 열/행 및 시트에 데이터 삽입

..

은 내가 많이 모르는 그 이유는 코딩에만 자바 자체 연구를하고 있어요 ... 감사 C12, E11, 시트 1 등

에 삽입 할 여기

답변

5

이 XLS, XLSX하지 않는 단지 예입니다. here을 참조 할 수 있습니다. xlsx에서 데이터를 편집/입력하려면 XSSFWorkbook 및 XSSFSheet를 사용하십시오.

try { 
     //Get the excel file. 
     FileInputStream file = new FileInputStream(new File("(which sheet you want to modify or edit(put the path here)")); 

     //Get workbook for XLS file. 
     HSSFWorkbook yourworkbook = new HSSFWorkbook(file); 

     //Get first sheet from the workbook. 
     //If there have >1 sheet in your workbook, you can change it here IF you want to edit other sheets. 
     HSSFSheet sheet1 = yourworkbook.getSheetAt(0); 

     // Get the row of your desired cell. 
     // Let's say that your desired cell is at row 2. 
     Row row = sheet1.getRow(1); 
     // Get the column of your desired cell in your selected row. 
     // Let's say that your desired cell is at column 2. 
     Cell column = row.getCell(1); 
     // If the cell is String type.If double or else you can change it. 
     String updatename = column.getStringCellValue(); 
     //New content for desired cell. 
     updatename="Lala"; 
     //Print out the updated content. 
     System.out.println(updatename); 
     //Set the new content to your desired cell(column). 
     column.setCellValue(updatename); 
     //Close the excel file. 
     file.close(); 
     //Where you want to save the updated sheet. 
     FileOutputStream out = 
      new FileOutputStream(new File("(where you want to save?.Put the path here)")); 
     yourworkbook.write(out); 
     out.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
1

가 도와 니펫 :

public void write() throws IOException, WriteException { 
    File file = new File("file_location"); 
    WorkbookSettings wbSettings = new WorkbookSettings(); 
    wbSettings.setLocale(new Locale("en", "EN")); 
    WritableWorkbook wb= Workbook.createWorkbook(file, wbSettings); 
    wb.createSheet("My Spreadsheet", 0); 
    WritableSheet excel = wb.getSheet(0); 
    createLabel(excel); 
    createContent(excel); 
    wb.write(); 
    wb.close(); 
}