码迷,mamicode.com
首页 > 其他好文 > 详细

poi读取excel07

时间:2015-07-03 00:07:05      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class test {
    public static void main(String[] args) throws Exception{
        InputStream is = new FileInputStream("C:\\test.xlsx");
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\text.txt")));
        for(int i=0;i<xssfWorkbook.getNumberOfSheets();i++){//处理每个sheet
            XSSFSheet  xs=xssfWorkbook.getSheetAt(i);
            if(xs==null){
                continue;
            }
            for (int rowNum = 33; rowNum <= xs.getLastRowNum(); rowNum++) {
                XSSFRow hssfRow = xs.getRow(rowNum);
                if (hssfRow != null) {
                      XSSFCell number = hssfRow.getCell(2);
                      XSSFCell name = hssfRow.getCell(3);
                      XSSFCell net = hssfRow.getCell(4);
                      XSSFCell pass = hssfRow.getCell(5);
                      if(number==null){
                          System.exit(0);
                      }
                      System.out.println("\n 序号:"+rowNum+";  公司:"+getValue(name)+";  网址:"+getValue(net)+"; \n序列号和密码:"+getValue(pass).replace("\n", ""));
                      writer.write("\r\n序号:"+rowNum+";  公司:"+getValue(name)+";  网址:"+getValue(net)+"; \r\n序列号和密码:"+getValue(pass).replace("\n", ""));
                      System.out.println("------------------------------------------------------------------------------------------------------------------------------");
                      writer.write("\r\n-----------------------------------------------------------------------------------------");
                }
            }

        }
        writer.close();
    }
    private static String getValue(XSSFCell hssfCell) {
                    if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
                     // 返回布尔类型的值
                         return String.valueOf(hssfCell.getBooleanCellValue());
                     } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
                         // 返回数值类型的值
                         return String.valueOf(hssfCell.getNumericCellValue());
                     } else {
                     // 返回字符串类型的值
                         return String.valueOf(hssfCell.getStringCellValue());
                    }
                 }

}

 

poi读取excel07

标签:

原文地址:http://www.cnblogs.com/sunyt/p/4617342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!