---
title: "POI解析excel2007和生成excel2007 "
author: "xiaohuafyle"
date: "2012-06-26T15:45:32Z"
tags: []
categories: ["编程语言"]
slug: "poi-excel2007-excel2007"
draft: false
lastmod: "2012-06-26T18:11:38Z"
id: 1568179
url: "https://xiaohuafyle.iteye.com/blog/1568179"
---

最近几天在整理客户的数据，数据量比较大，有几十万条，用到了excel。用户提供的数据有很多重复的，这个我们要挑拣出来，我用代码写挑拣重复的数据，原来用jxl解析excel，发现excel不支持office2007，并且excel2003最大的行数只能是65535，这样就不能运行一次代码解决问题了。查了下资料，apache的POI可以解析office2007。下面两个简单例子望能提供给网友参考。

1.读取excel2007
Java代码
        //office2007工作区   
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("D:/temp/test_poi.xlsx"));   
        //获得该工作区的第一个sheet   
        XSSFSheet sheet = wb.getSheetAt(0);   
        //总共有多少行,从0开始   
        int totalRows = sheet.getLastRowNum();   
        for (int i = 0; i <= totalRows; i++) {   
            //取得该行   
            XSSFRow row = sheet.getRow(i);   
            //注释的代码，是为了防止excel文件有空行   
//          if(row == null) {   
//              continue;   
//          }   
            System.out.println(row.getCell(1).toString());   
        }  

2.生成excel2007
Java代码
//输出流   
OutputStream os = new FileOutputStream("D:/temp/create.xlsx");    
//工作区   
XSSFWorkbook wb = new XSSFWorkbook();   
//创建第一个sheet   
XSSFSheet sheet= wb.createSheet("test");   
//生成第一行   
XSSFRow row = sheet.createRow(0);   
//给这一行的第一列赋值   
row.createCell(0).setCellValue("column1");   
//给这一行的第一列赋值   
row.createCell(1).setCellValue("column2");   
//写文件   
wb.write(os);   
//关闭输出流   
os.close();