zoukankan      html  css  js  c++  java
  • Poi低版本生成excel和解析

    package com.jmz.java;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class Poi {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String title[] = {"id","name","sex"};
            //生成表
            HSSFWorkbook workbook = new HSSFWorkbook();
            //sheet页
            HSSFSheet sheet = workbook.createSheet();
            //创建第1行
            HSSFRow row1 = sheet.createRow(0);
            //循环加入cell
            HSSFCell cell = null;
            //添加第一行内容
            for (int i = 0; i < title.length; i++) {
                cell = row1.createCell(i);
                cell.setCellValue(title[i]);
            }
            //追加数据
            for (int i = 1; i < 11; i++) {
                //循环创建行,从第2行开始
                HSSFRow row2 = sheet.createRow(i);
                //创建第一个单元
                HSSFCell cell1 = row2.createCell(0);
                //设置值
                cell1.setCellValue(i+"");
                //第二个
                cell1 = row2.createCell(1);
                cell1.setCellValue("jim"+i);
                //第三个
                cell1 = row2.createCell(2);
                cell1.setCellValue("男");
            }
            //创建文件
             File file = new File("d:\poi.xls");
             try {
                 //创建文件
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                //把内容写入文件
                workbook.write(fos);
                //关流
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    解析:

    package com.jmz.java;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class PreaseExcel {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                //获取workbook
                HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(new File("d:\poi.xls")));
                //创建sheet页
                HSSFSheet sheet = workbook.getSheetAt(0);
                int firstRow = 0;
            //获取最后一行行数
                int lastRow = sheet.getLastRowNum();
                for (int i = firstRow; i < lastRow; i++) {
                    //获取当前行数
                    HSSFRow row = sheet.getRow(i);
                    //获取当前行数的最后一个cell数
                    int lastCellNum = row.getLastCellNum();
                    //遍历此行的cell数
                    for (int j = 0; j < lastCellNum; j++) {
                        //获取这个cell
                        HSSFCell cell = row.getCell(j);
                        String value = cell.getStringCellValue();
                        System.out.print(value+"  ");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    mongo的常用操作——增删改查
    配置我的sublime
    mongo概念
    mongo命令
    mongo安装与配置
    node搭建http基本套路
    模块的导出入
    vue数据绑定原理
    webpack打包速度优化
    工作中的优化之数字键盘优化
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5705818.html
Copyright © 2011-2022 走看看