zoukankan      html  css  js  c++  java
  • 用Java解析Excel文件

    用到了Apache的→POI插件←,可点击链接从官网下载,目前已更新至4.0版本

    1.将下载好的放入lib(class_path)目录下,

    2.编写代码

    package com.fyf.test;
    
    import java.io.FileInputStream;
    
    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;
    
    public class PoiRead {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                FileInputStream fStream = new FileInputStream("D:/test.xls");
                Workbook wb = new HSSFWorkbook(fStream);
                Sheet sheet = wb.getSheetAt(0);
                //因为Sheet接口继承了 java.lang.Iterable接口所以,遍历表中的行可以一用foreach很方便    
                for (Row row : sheet) {
              //跳过空行
    if (row==null) { continue; } System.out.print("row:"+row.getRowNum()+" ");
              //同理行中的单元格也可以用foreach遍历
    for (Cell cell : row) { if (cell==null) { continue; }
                //对cell进行判断后输出 System.out.print(
    "|"+getStringCell(cell)+" |"); } System.out.println(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static String getStringCell(Cell cell) { String result = ""; int cellType = cell.getCellType(); switch (cellType) { case Cell.CELL_TYPE_BOOLEAN: result = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC:
           //这里将数组作为日期返回 result
    = String.valueOf(cell.getDateCellValue()); break; case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; default: break; } return result; } }
  • 相关阅读:
    java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
    CentOS7 firewalld 使用
    服务器设置主机名以及服务器间ssh连接
    httpclient处理返回数据
    httpclient如何获取请求参数
    httpclient请求转发实战
    Java自带的md5、sha和base64加密怎么用
    mongodb分页Spring-data-mongodb
    has been loaded by xml or sqlprovider
    052(十)
  • 原文地址:https://www.cnblogs.com/annofyf/p/9669946.html
Copyright © 2011-2022 走看看