• 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();
            }
        }
    
    }
  • 相关阅读:
    hdu 1084(水题)
    hdu 1872(稳定排序)
    NOI 2008 志愿者招募 / bzoj 1061 (最小费用最大流)
    hdu 1019(LCM)
    hdu 1876(dp)
    Codeforces Round #171 (Div. 2)
    poj 3422(最小费用最大流)
    poj 3264(简单线段树)
    Codeforces Round #156 (Div. 2)
    Codeforces Round #169 (Div. 2)
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5705818.html
Copyright © 2020-2023  润新知