JXLを使用してExcelファイルを読みます

**

JXLは、Javaを使用してExcelファイルを読み込みます

**

JXLファイルをエクセル読み取るためのJavaライブラリを使用して、この記事では、ファイル名の接尾辞.XLSエクセル、.xlsx形式のサポートされていません。

コードは以下の通りであります:

パブリッククラスExcelReader {

Workbook rwb;	

public  String[][] readExcel(String filePath) throws IOException {
	if (filePath.endsWith(".xls")) {
		return this.reader(filePath);
	} else {
		return null;
	}	
}

public  String[][] reader(String filePath) throws IOException {
	
	try{
		
		InputStream is = new FileInputStream(filePath);
		
         rwb = jxl.Workbook.getWorkbook(is); 
				
		Sheet readsheet = rwb.getSheet(0);

		int rsColumns = readsheet.getColumns();	
		
		int rsRows = readsheet.getRows();
		
		String[][] data = new String[rsRows][rsColumns];
		
		for(int i = 0; i < rsRows; i++)
		{
			for(int j = 0; j < rsColumns; j++)
			{
				data[i][j] = readsheet.getCell(j, i).getContents(); 
			}
		}
		
		return data;
		
	} catch(Exception e){
		e.printStackTrace();
		return null;
	} finally {
		rwb.close();
	}	
}

数値の場合、セル。より正確には、次のコードを使用する場合のgetContents()は自動的に、小数点以下3桁を四捨五入します読み取ります。

				Cell cell = readsheet.getCell(j, i);
				
				if(cell.getType() == CellType.NUMBER)
				{
					 NumberCell numberCell = (NumberCell) cell; 
					 data[i][j] = String.valueOf(numberCell.getValue());
				}

おすすめ

転載: blog.csdn.net/yuailing98/article/details/95310103