我刚刚让我的代码正常工作,但 Excel 工作表中的数字在 java 控制台中没有以正确的方式显示。例如,年龄应该是 20,但结果是 20.0。有什么建议吗?
此外,由于某种原因,我的记录器出现错误。
log4j:WARN No appenders could be found for logger (package.JamaisDisplayMenu).
log4j:WARN Please initialize the log4j system properly.
我在另一个项目中以完全相同的方式设置了它,并且工作正常。对此还有什么想法吗?
这是我的代码...
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class JamaisDisplayMenu {
private final static Logger logger = Logger.getLogger(JamaisDisplayMenu.class); //starts logger
public static void main( String [] args ) {
try {
InputStream input = new BufferedInputStream(
new FileInputStream("c:/Users/dude/Desktop/Data2.xls"));
logger.error("File parsed");
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
HSSFCell cell = (HSSFCell) cells.next();
if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
System.out.print( cell.getNumericCellValue()+" " );
else
if(HSSFCell.CELL_TYPE_STRING==cell.getCellType())
System.out.print( cell.getStringCellValue()+" " );
else
if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType())
System.out.print( cell.getBooleanCellValue()+" " );
else
if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
System.out.print( "BLANK " );
else
System.out.print("Unknown cell type");
logger.error("File parsed");
}
}
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
最佳答案
您使用的方法返回一个 double 值,您可能需要该值的下限(年龄),或四舍五入的值(如果您想要某物的价格,可能是最接近的美元)
替换:
System.out.print( cell.getNumericCellValue()+" " );
与:
System.out.print( new Double(Math.floor(cell.getNumericCellValue())).intValue() +"");
年龄
或
System.out.print( new Double(cell.getNumericCellValue()+0.5).intValue() +"");
用于四舍五入
关于java - 如何在java excel导入输出中将1000.0更改为1000?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943888/