Java poi 读取excel数据 字符串型,数值型,日期型

1,前提

这里我已经将excel的文件信息传入数据表中,表格内容以字节流数据存储

datacontent字段就是我存储的数据内容,

fileNameOrg则是文件信息
在这里插入图片描述

2,思路

1,获取表格文件信息
2,读取文件数据内容
3,判断文件类型是否为表格
4,读取第一个工作页sheet
5, 读取标题栏
6,读取内容


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;


  public HashMap<String, Object> importExcel(@RequestParam HashMap<String, String> param) {
  
        try {
        //获取表格文件名
            InputStream is = null;
            String filename = fileEntity.getFileNameOrg();
            
            //获取后缀名称
            try {
                is =new ByteArrayInputStream(fileEntity.getDatacontent());
                //判断是否是表格文件,是,获取工作簿
                Workbook wb = null;
                if (filename.contains("xlsx")) {
                    wb = new XSSFWorkbook(is);
                } else if (filename.contains("xlsx")) {
                    wb = new HSSFWorkbook(is);
                } else {
                    HashMap<String, Object> resultMap = new HashMap<>();
                    resultMap.put("success", false);
                    resultMap.put("msg", "数据导入失败!");
                    return resultMap;
                }

                //读取第一个工作页sheet
                Sheet sheet = wb.getSheetAt(0);
                //列头
                Row hender = sheet.getRow(0);

              //标题行获取,根据索引一一对应
                HashMap<Integer, String> fieldnamemap = new HashMap<>();
                for (Cell cell : hender) {
                    fieldnamemap.put(cell.getColumnIndex(), cell.getStringCellValue());
                }
   
                //表格行数据
                for (Row row : sheet) {
                    if(StringUtil.isEmptyOrLength0(row)){
                        break;
                    }
                    //非标题行数据读取
                    if (row.getRowNum()!=0){
                        for (Cell cell : row) {
                        //当前单元格的标题
                            String name = fieldnamemap.get(cell.getColumnIndex());
                            //字符串型处理
							String value1=cell.getStringCellValue();
							//数值型处理
							double value2=cell.getNumericCellValue();
							//时间型处理
 							int intnum=Integer.parseInt(cell.toString().split("\\.")[0]) ;
                            double demnum=Double.parseDouble("0."+cell.toString().split("\\.")[1]);
                            Date dateTime =getTime(getDate(intnum), demnum);
                            String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateTime);
                            //日期型处理
							int intnum=Integer.parseInt(cell.toString().split("\\.")[0]) ;
                            Date dateTime =getTime(getDate(intnum), 0);
                            String date= new SimpleDateFormat("yyyy-MM-dd").format(dateTime);
                            // 略。。。。。。。。。。
 


	          } catch (Exception e) {
	               logger.error("系统错误!" + e.getMessage(), e);
	               HashMap<String, Object> resultMap = new HashMap<>();
	               resultMap.put("success", false);
	               resultMap.put("msg", "数据导入失败!");
	               return resultMap;
	           } finally {
	               try {
	                   if (is != null) is.close();
	               } catch (IOException e) {
	                   e.printStackTrace();
	               }
	           }
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("success", true);
            resultMap.put("msg", "数据导入成功!");
            return resultMap;
        } catch (Exception ex) {
            ex.printStackTrace();
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("success", false);
            resultMap.put("msg", "系统错误,请稍候再试." + ex.getMessage().replace("\r", "").replace("\n", ""));
            return resultMap;
         }
    }


//时间和日期处理的方法
    public Date getDate(int days) {
        Calendar c = Calendar.getInstance();
        c.set(1900, 0, 1);
        c.add(Calendar.DATE, days - 2);
        return c.getTime();
    }

    public Date getTime(Date date, double ditNumber) {
        Calendar c = Calendar.getInstance();
        int mills = (int) (Math.round(ditNumber * 24 * 3600));
        int hour = mills / 3600;
        int minute = (mills - hour * 3600) / 60;
        int second = mills - hour * 3600 - minute * 60;
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
        return c.getTime();
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaPOI库来读取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、付费专栏及课程。

余额充值