码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现对Excel文件的读取、操作

时间:2019-05-30 10:47:31      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:class   catch   util   rac   文件的   number   get   model   valueof   

1.项目所需jar包,poi-3.9-20121203.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar

2.案例参考

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Excel01 {

    public static void main(String[] args) throws IOException,ParseException{
        System.out.println(">>>>>>>>>>>>>读取Excel");
        Workbook wb = null; 
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
        String cellData = null;
        String filePath = "D:\\Zexcel\\E.xlsx";
        String columns[] = {"a","b","c","d"};
        wb=readExcel(filePath);
        if (wb != null) {
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            System.out.println("最大行数"+rownum);
            //获取第一行
            row = sheet.getRow(1);
            //获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
            System.out.println("最大列数"+colnum);
            for (int i = 1; i < rownum; i++) {
                Map<String,String> map = new LinkedHashMap<String,String>();
                row = sheet.getRow(i);
                if (row!=null) {
                    for (int j = 0; j < colnum; j++) {
                        cellData = (String)getCellFormatValue(row.getCell(j));
                        map.put(columns[j], cellData);
                    }
                } else {
                    break;
                }
                list.add(map);
            }
        }
        //遍历解析出来的list
        FileWriter idAndPolict = new FileWriter("D:\\Zexcel\\n.txt");
        String temp = ",";
        String tempEnd = " from A;";
        //select a,b,c,d from A;
        for (Map<String,String> map : list) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("select ").append(map.get("a")).append(temp).append(map.get("b")).append(temp).append(map.get("c")).append(temp).append(map.get("d")).append(tempEnd);
            //写入ds文件
            idAndPolict.write(stringBuffer.toString().toString()+"\r\n");
            idAndPolict.flush();
        }
    }

    //读取Excel
    public static Workbook readExcel(String filePath){
        Workbook wb = null;
        if (filePath==null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (".xls".equals(extString)) {
                return wb = new HSSFWorkbook(is);
            } else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
    
    //Excel值转化
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if (cell!=null) {
            //判断cell类型
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                    Date theDate = cell.getDateCellValue();
                    SimpleDateFormat dff = new SimpleDateFormat("yyyy/MM/dd");
                    cellValue = dff.format(theDate);
                } else {
                    DecimalFormat df = new DecimalFormat("0");
                    cellValue = df.format(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //判断cell是否为日期格式
                if (DateUtil.isCellDateFormatted(cell)) {
                    //转换为日期格式yyyy-mm-dd
                    cellValue = cell.getDateCellValue();
                } else {
                    //数字
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
            
        }else{
            cellValue = "";
        }
        return cellValue;
        
    }
    
    
}

 

Java实现对Excel文件的读取、操作

标签:class   catch   util   rac   文件的   number   get   model   valueof   

原文地址:https://www.cnblogs.com/jiangaihu/p/10947987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!