六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 38|回复: 0

poi读写excel

[复制链接]

升级  0%

12

主题

12

主题

12

主题

秀才

Rank: 2

积分
50
 楼主| 发表于 2013-1-15 02:16:09 | 显示全部楼层 |阅读模式
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;



import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellRangeAddress;

/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API
* users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
*/
public final class HSSFReadWrite {

/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/

public static void main(String[] args) throws IOException
{



try {  
           InputStream input = new FileInputStream("d:\\poi.xls");  
           POIFSFileSystem fs = new POIFSFileSystem(input);  
           HSSFWorkbook wb = new HSSFWorkbook(fs);  
           HSSFSheet sheet = wb.getSheetAt(0);  
           // Iterate over each row in the sheet  
           Iterator rows = sheet.rowIterator();  
           while (rows.hasNext()) {  
            HSSFRow row = (HSSFRow) rows.next();  
           
            // Iterate over each cell in the row and print out the cell"s  
            // content  
            Iterator cells = row.cellIterator();  
            while (cells.hasNext()) {  
             HSSFCell cell = (HSSFCell) cells.next();  
            
             switch (cell.getCellType()) {  
             case HSSFCell.CELL_TYPE_NUMERIC:  
              System.out.print(cell.getNumericCellValue());  
              break;  
             case HSSFCell.CELL_TYPE_STRING:  
              System.out.print(cell.getStringCellValue());  
              break;  
             case HSSFCell.CELL_TYPE_BOOLEAN:  
              System.out.print(cell.getBooleanCellValue());  
              break;  
             case HSSFCell.CELL_TYPE_FORMULA:  
              System.out.print(cell.getCellFormula());  
              break;  
             default:  
              System.out.print("unsuported sell type");  
              break;  
             }  
            
            }  System.out.println();  
           }  
          } catch (IOException ex) {  
           ex.printStackTrace();  
          }  
}  
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表