poi - EXECL的导出(jar包的版本为3.14),支持Execl全版本,后缀xls,后缀xlsx

所需poi  jar包的地址:[添加链接描述](https://download.csdn.net/download/qq_29451823/12074663)


package com.zytx.cxf.ws.kaoping;

import java.io.*;
import java.util.ArrayList;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest {

    public static void main(String[] args) throws Exception {
        String fileName = "C:\\Users\\Administrator\\Desktop\\zytx.xlsx";
        readExcel(fileName);
    }

    public static ArrayList<String> readExcel(String fileName) {
        ArrayList<String> list = new ArrayList<String>();
        Workbook workbook = null;
        Row row = null;
        //获取Excel文档
        workbook = getWorkbook(fileName);
        //获取Excel文档的第一个sheet页
        Sheet sheet = workbook.getSheetAt(0);
        //获取文档中已保存数据的行数
        int rowNum = sheet.getPhysicalNumberOfRows();
        //获取第一行
        row = sheet.getRow(0);
        //获取当前行已保存数据的最大列数
        int colnum = row.getPhysicalNumberOfCells();
        for (int i = 0; i < rowNum; i++) {
            row = sheet.getRow(i);
            if (null != row) {
                for (int j = 0; j < colnum; j++) {
                    Cell cell = row.getCell(j);

                    String str = (String) getValueFromCell(cell);
                    list.add(str);
                }
            }
        }
        return list;
    }

    private static Workbook getWorkbook(String fileName) {//根据后缀获取Excel表格
        Workbook workbook = null;
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        InputStream in = null;
        try {
            in = new FileInputStream(fileName);
            if ("xls".equals(suffix)) {
                //  workbook = new HSSFWorkbook(in);
            } else if ("xlsx".equals(suffix)) {
                workbook = new XSSFWorkbook(in);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    private static Object getValueFromCell(Cell cell) {//获取单元格的值
        Object value = null;
        if (null == cell) {
            return "";
        }
        //判断cell类型
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC: {
                value = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA: {
                //判断cell是否为日期格式
                if (DateUtil.isCellDateFormatted(cell)) {
                    //转换为日期格式YYYY-mm-dd
                    value = cell.getDateCellValue();
                } else {
                    //数字
                    value = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING: {
                value = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                value = "";
        }
        return value;
    }

    public static void xieru(String str) {
        //创建excel文件
        File NewxlsFile = new File("C:\\Users\\Administrator\\Desktop\\kaopingresult.xlsx");
        // 创建一个工作簿
        XSSFWorkbook Newworkbook = new XSSFWorkbook();
        // 创建一个工作表
        XSSFSheet Newsheet = Newworkbook.createSheet("sheet1");
        // 将数据填入新的表格中
        for (int row = 0; row < 500; ++row) {
            //创建行
            XSSFRow Newrows = Newsheet.createRow(row);
            for (int col = 0; col < 1; ++col) {
                //按列写入
                Newrows.createCell(col).setCellValue("col" + col);
            }
            //将excel写入
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(NewxlsFile);
                Newworkbook.write(fileOutputStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(JavaSE)