POI读取Excel带格式数据

package com.hs.tiger.application;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

public class ParseValidator {
public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream(
"E:\\JEE\\Workspaces\\MyEclipse 8.6\\Mirror\\src\\com\\hs\\tiger\\application\\Numeric.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
HSSFSheet sheet = wb.getSheetAt(0);
try
{
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Cell c = sheet.getRow(i).getCell(0);

//System.out.println(c.getCellType());

//c.setCellType(Cell.CELL_TYPE_STRING);
System.out.println(c.getCellStyle().getDataFormatString());
try{
System.out.println(c.getStringCellValue());
}
catch (Exception e) {
try
{
System.out.println(c.getNumericCellValue());
}
catch (Exception ex) {
// TODO: handle exception
}
}

}
}
catch (Exception e) {
// TODO: handle exception
}

/*
* for (Iterator<org.apache.poi.ss.usermodel.Row> rit =
* sheet.rowIterator(); rit.hasNext(); ) { row = rit.next(); for
* (Iterator<Cell> cit = row.cellIterator(); cit.hasNext(); ) { cell =
* cit.next(); System.out.println(cell.getBooleanCellValue()); } }
*/

}
}


使用poi读取excel内容的问题:
例如excel中cell中显示值为20,100,612.00,实际值为20100612,
使用getNumbericCellValue()读取的的实际值,
是否有函数能读取cell的显示值,求大侠指教!


显示值20,100,612.00只是格式化了而已
你读取之后再用JAVA格式化一次就行了
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
df.applyPattern("###,###.00");
System.out.println(df.format(new Integer("20100612")));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的POI库来读取Excel日期格式数据。具体步骤如下: 1. 使用POI库中的Workbook类打开Excel文件。 2. 确定要读取的工作表。 3. 遍历工作表的每一行,读取日期格式单元格。 4. 使用Java中的日期格式化函数将日期数据转换为标准日期格式。 以下是示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ReadExcelDate { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("your_file.xls")); //Create Workbook instance holding reference to .xlsx file Workbook workbook = WorkbookFactory.create(file); //Get first/desired sheet from the workbook Sheet sheet = workbook.getSheetAt(0); //Iterate through each rows one by one for (Row row : sheet) { //Iterate through each cell one by one for (Cell cell : row) { //Check the cell type and format accordingly switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.print(cell.getDateCellValue() + "\t"); } else { System.out.print(cell.getNumericCellValue() + "\t"); } break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; } } System.out.println(""); } file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 该代码将遍历Excel中的每一个单元格,检查其类型并格式化日期数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值