我有一個excel中包含一些值文件,其中來自Excel中的值:如何格式化用java
**Status Code** **Method Name**
400 createRequest
401 testRequest
402 mdm
403 fileUpload
和下面的代碼讀取和打印數據[後期以後,我要對他們的HashMap ]
package com.poc.excelfun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class ReadExcelData {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("attachment_status.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
/*
* The following code to create a new work book with the value fetched from the given work book
* FileOutputStream out =
new FileOutputStream(new File("attachment_status_new.xls"));
workbook.write(out);
out.close();*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
,但上面的代碼返回如下:
Status Code Method Name
400.0 createRequest
401.0 testRequest
402.0 mdm
403.0 fileUpload
如果在凌晨放出來的狀態代碼自帶.0
但我只想得到400
不適用.0
如何做到這一點。
我已經使用poi
Excel的操作。
問候 安託
你可以使用字符串或正則表達式來格式化輸出。或者嘗試將數字轉換爲整數 – Kuchi 2013-05-09 08:37:13