zoukankan      html  css  js  c++  java
  • 解析Excel_Poi

    public class PoiExpExcel{

      public static void main(String[] args){

        String[] title = {"id","name","sex"};//定义一个表头

        //创建Excel工作簿

        HSSFWorkbook workbook = new HSSFWorkbook();

        //创建一个工作表sheet

        HSSFSheet sheet  =workbook.createSheet();

        //创建第一行

        HSSRow row = sheet.createRow(0);

        HssFCell cell = null;

        //插入第一行数据id,name,sex

        for(int i = 0; i < title.length; i++){

          cell = row.createCell(i);

          cell.setCellValue(title[i]);

        }

        //追加数据

        for(int i = 1; i < 10; i++){

          HSSFRow nextrow = sheet.createRow(i);

          HSSFCell cell2 = nextrow.createCell(0);

          cell2.setCellValue("a" + 1);

          cell2 = nextrow.createCell(1);

          cell2.setCellValue("user" + i);

          cell2 = nextrow.createCell(2);

          cell2.setCellValue("男");

        }

        // 创建一个文件

        File file = new  File("e:/poi_test");

        file.createNewFile();//捕获异常

        //讲Excel内容存盘

        FileOutputStream stream = FileUtils.opentOutputStream(file);

        workbook.write(stream);

        workbook.close();

      }

    }

    poi 解析Excel

    public class PoiReadExcel{

      public static void main(String[] args){

        //需要解析的Excel文件

        File file = new File("e:/poi_test");

        HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.opentInputSteam(file));//捕获异常

        //获取第一个工作表

        HSSFSheet sheet = workbook.getSheet("sheet0");//传的是sheet表的名称

        // 另一种方式获取sheet

        //读取默认的第一个工作表sheet

        // HSSFSheet sheet  = workbook.getSheetAt(0);

        //读取工作表中的数据

        int firstRowNum = 0;

        //获取sheet中的最后一行行号

        int lastRowNum = sheet.getLastRowNum();

        for(int i = firstRowNum; i <= lastRowNum; i++){

          HSSFRow row = sheet.getRow(0);

          //获取当前最后单元格列号

          int lastCellNum = row.getLastCellNum();

          for(int j = 0; j < lastCellNum; j++){

            HSSFCell cell = row.getCell(j);

            String value = cell.getStringCellValue();

            System.out.print(value + " ");

          }

        System.out.println();

        }  

      }

    }

  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/0914lx/p/6755701.html
Copyright © 2011-2022 走看看