【POI】学习计划 第二篇

来源:互联网 发布:淘宝随便逛逛 编辑:程序博客网 时间:2024/06/05 06:07
之前学习了操作excel的一个单元格,现在我们来学习一下如何操作整个EXCEL文件,实现原理很简单,只需要循环输出就可以了,话不多说,直接上代码,如有不懂,请点左边的QQ联系。
package com.xg.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;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 PoiTest2 {/* * 读取excel内的全部内容 */public static void read(InputStream inputStream) throws IOException{//初始化整个ExcelHSSFWorkbook workbook=new HSSFWorkbook(inputStream);//循环workbook内的所有sheetfor(int sheetIndex=0;sheetIndex<workbook.getNumberOfSheets();sheetIndex++){HSSFSheet sheet=workbook.getSheetAt(sheetIndex);System.err.println("sheet序号"+sheetIndex+"sheet名称:"+sheet.getSheetName());//循环该sheet表中的每一行for(int rowIndex=0;rowIndex<sheet.getLastRowNum();rowIndex++){HSSFRow row=sheet.getRow(rowIndex);if (row==null) {continue;}//循环该行的每一个单元格的数据for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {HSSFCell cell=row.getCell(cellIndex);if(cell!=null){cell.setCellType(cell.CELL_TYPE_STRING);System.err.println("第"+rowIndex+"行      第"+cellIndex+"列    内容为:"+cell.getRichStringCellValue());}}}System.out.println("-----------------");}}public static void main(String[] args) {InputStream is=null;try {is=new FileInputStream(new File("c:/xg.xls"));read(is);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if (is!=null) {is.close();}} catch (Exception e2) {}}}}



原创粉丝点击