• java excel 导入


    /**
    * 导入和导出Excel文件类
    * 支持2003(xls)和2007(xlsx)版本的Excel文件
    *
    * @author yxm
    */
    public class OperationExcelForPOI {

    public static void main(String[] args) {
    // 文件所在路径
    String execelFile = "D:\Download\公司员工履历表5.xls" ;
    //String execelFile = "C:/Book2003.xls" ;
    // 导入Excel
    new OperationExcelForPOI().impExcel(execelFile) ;
    // 导出Excel
    // String expFilePath = "C:/testBook.xls" ;
    // new OperationExcelForPOI().expExcel(expFilePath);
    }

    /**
    * 导入Excel
    * @param execelFile
    */
    public void impExcel(String execelFile){
    try {
    // 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区)
    Workbook book = null;
    try {
    // Excel 2007获取方法
    book = new XSSFWorkbook(new FileInputStream(execelFile));
    } catch (Exception ex) {
    // Excel 2003获取方法
    book = new HSSFWorkbook(new FileInputStream(execelFile));
    }

    // 读取表格的第一个sheet页
    Sheet sheet = book.getSheetAt(0);
    // 定义 row、cell
    Row row;
    String cell= "";
    // 总共有多少行,从0开始
    int totalRows = sheet.getLastRowNum() ;
    // 循环输出表格中的内容,首先循环取出行,再根据行循环取出列
    for (int i = 1; i <= totalRows; i++) {

      

    if(row == null){
    // continue ;
    // }
    // 总共有多少列,从0开始
    int totalCells = row.getLastCellNum() ;
    for (int j = row.getFirstCellNum(); j < totalCells; j++) {
    // 处理空列
    if(row.getCell(j) == null || ){
    continue ;
    }
    // 通过 row.getCell(j).toString() 获取单元格内容
    cell = row.getCell(j).toString();
    if("".equals(cell))continue;
    System.out.print(cell +" ");
    }
    System.out.println("");
    //插入SQL

    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    //action 

    String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    //D:apache-tomcat-6.0.18webappsstruts2_uploadimages
    System.out.println("realpath: "+realpath);
    if (image != null) {
    File savefile = new File(new File(realpath), imageFileName);
    if (!savefile.getParentFile().exists())
    savefile.getParentFile().mkdirs();
    FileUtils.copyFile(image, savefile);
    ActionContext.getContext().put("message", "文件上传成功");
    }

  • 相关阅读:
    [C/C++] 指针数组和数组指针
    [计算机网络] DNS劫持和DNS污染
    [计算机网络-数据链路层] CSMA、CSMA/CA、CSMA/CD详解
    [BinaryTree] 二叉树常考知识点
    NODE-windows 下安装nodejs及其配置环境
    MATLAB/Excel-如何将Excel数据导入MATLAB中
    Excel-怎样实现行列转置
    一篇文章学懂Shell脚本
    SQL-MySQL使用教程-对MySQL的初步尝试
    资源贴-在线编译环境推荐
  • 原文地址:https://www.cnblogs.com/aiwoqu/p/4142234.html
Copyright © 2020-2023  润新知