2013-10-07 74 views
-5
public class ExcelLibrary{ 

/** 
* @param args 
*/ 
//method which accepts a sheet name, row number and cell number 
// and returns the string value present in that cell 
public String getExcelData(String sheetName,int rowNum, int cellNum){ 
    String retVal=null; 
    try { 
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx"); 
     //get the workbook object 
     Workbook wb = WorkbookFactory.create(fis); 
     //get the sheet [a particular sheet in the workbook] 
     Sheet s = wb.getSheet(sheetName); 
     //get the row [a particular row in the sheet] 
     Row r = s.getRow(rowNum); 
     //get the cell [a particular cell of the row obtained above] 
     Cell c = r.getCell(cellNum); 
     //get the string cell value from the cell 
     retVal = c.getStringCellValue();    
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return retVal; 
} 
public int getRowCount(String sheetName){ 
    int lastRow=0; 
    try { 
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx"); 
     //get the workbook object 
     Workbook wb = WorkbookFactory.create(fis); 
     //get the sheet [a particular sheet in the workbook] 
     Sheet s = wb.getSheet(sheetName); 
     //return the last row in which data is present counted form 0 
     lastRow = s.getLastRowNum();   
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return lastRow; 
} 
public void writeDataToExcel(String sheetName, int rowNum, int cellNum, String data){ 
    try { 
    FileInputStream fis = new FileInputStream("D:\\seleniumbsw9\\data.xlsx"); 
     //get the workbook object 
     Workbook wb = WorkbookFactory.create(fis); 
     //get the sheet [a particular sheet in the workbook] 
     Sheet s = wb.getSheet(sheetName); 
     //get the row where data needs to be written. This step 
     //assumes that we are writing to a cell in an existing row 
     Row r = s.getRow(rowNum);   
     //Create the cell in that row. If we are trying to write to 
     //a cell which has not been created, it will throw error. First 
     //we need to create a cell, then set the value of the cell 
     //This step is not needed if we are writing to an existing cell 
     Cell c = r.createCell(cellNum);   
     c.setCellValue(data); 
    FileOutputStream fos=new FileOutputStream("D:\\seleniumbsw9\\data.xlsx"); 
     wb.write(fos); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }  
} 

}寫一個excel文件

+3

請註明你想做的事和正確地格式化什麼的問題。閱讀你的問題是不可能的!這不是一個問題! – opalenzuela

+0

加入評論是否有使用jxl的特定原因?因爲這個API有更多的限制去與apache poi api。 – Vinay

回答

0

要獲取Excel文件中使用的行使用JXL .....

package Testpackage; 

import java.io.FileInputStream; 
import java.io.IOException; 
import jxl.Workbook; 
import jxl.Sheet; 
import jxl.read.biff.BiffException; 

public class Rowcount { 
    public static void main(String[] args) throws BiffException, IOException { 
     FileInputStream exo=new FileInputStream("E:\\test1.xls"); 
     Workbook wbo=Workbook.getWorkbook(exo); 
     Sheet wso=wbo.getSheet(0); 
     int lastrownum; 
     lastrownum= wso.getRows(); 
     System.out.println(lastrownum); 
    } 
}