使用poi读写Excel------demo

package com.js.ai.modules.pointwall.interfac;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class TestExcel {
	/**
	 * 
	 * @Title: testReadExcel
	 * @Description: 读取Excel
	
	 * @return: void
	 */
public void testReadExcel(){
	try {
		// 读取Excel  
		Workbook wb = new XSSFWorkbook(new FileInputStream("D:\\javatest\\2.xlsx"));
		// 获取sheet(篇)数目  
		for (int t = 0; t < wb.getNumberOfSheets(); t++) {
			Sheet sheet = wb.getSheetAt(t);
			Row row = null;
			int lastRowNum = sheet.getLastRowNum();
			// 循环读取
			for (int i = 0; i <= lastRowNum; i++) {
				row = sheet.getRow(i);
				if (row != null) {
					// 获取每一列的值  
					for (int j = 0; j < row.getLastCellNum(); j++) {
						Cell cell = row.getCell(j);
						String value = getCellValue(cell);
						if (!value.equals("")) {
							System.out.print(value + "|");
						}
					}
					System.out.println();
				}
			}
		} 
	} catch (Exception e) {
		e.printStackTrace();
	}
}
/**
 * 
 * @Title: getCellValue
 * @Description: 读取单元格的值
 * @param cell
 * @return

 * @return: String
 */
private String getCellValue(Cell cell){
	Object result="";
	switch(cell.getCellType()){
	case Cell.CELL_TYPE_STRING:
	result=cell.getStringCellValue();
	break;
	case Cell.CELL_TYPE_NUMERIC:
	result=cell.getNumericCellValue();
	break;
	case Cell.CELL_TYPE_BOOLEAN:
	result=cell.getBooleanCellValue();
	break;
	case Cell.CELL_TYPE_FORMULA:
	result=cell.getCellFormula();
	break;
	case Cell.CELL_TYPE_ERROR:
	result=cell.getErrorCellValue();
	break;
	case Cell.CELL_TYPE_BLANK:
	break;
	default:
	break;
	}
	return result.toString();
}
/**
 * 
 * @Title: testWriteExcel
 * @Description: 写入Excel文件

 * @return: void
 */
public void testWriteExcel() {  
	String excelPath = "D:\\javatest\\2.xlsx"; 
	Workbook workbook=null;
	try {
		workbook = new XSSFWorkbook();
	} catch (Exception e) {
		System.out.println("创建Excel失败: ");  
		e.printStackTrace();
	}
	if(workbook!=null){
		Sheet sheet=workbook.createSheet("测试数据");
		Row row0=sheet.createRow(0);
		for(int i=0;i<12;i++){
			Cell cell=row0.createCell(i, Cell.CELL_TYPE_STRING);
			cell.setCellValue("列标题");
			sheet.autoSizeColumn(i);//自动调整宽度 
		}
		for (int rowNum = 1; rowNum < 16; rowNum++) {  
		      Row row = sheet.createRow(rowNum);  
		      for (int i = 0; i < 12; i++) {  
		        Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);  
		        cell.setCellValue("单元格" + String.valueOf(rowNum + 1)  
		            + String.valueOf(i + 1));  
		      }  
		    }
		try {  
		      FileOutputStream outputStream = new FileOutputStream(excelPath);  
		      workbook.write(outputStream);  
		      outputStream.flush();  
		      outputStream.close();  
		    } catch (Exception e) {  
		      System.out .println("写入Excel失败: ");  
		      e.printStackTrace();  
		    }  
	}
}
public static void main(String[] args) {
	TestExcel testExcel=new TestExcel();
	//testExcel.testReadExcel();
	testExcel.testWriteExcel();
	
}
}

  Workbook是一个接口,他有2个实现:HSSFWorkbook和XSSFWorkbook。前者是用来读取97-03版的Excel,扩展名为xls,后者是读取07及以后的版本,扩展名为xlsx。读入到workbook中,然后循环所有的sheet,在sheet循环所有的有效行和有效列。其中sheet.getLastRowNum()获得最后一行的索引值(从0开始),而sheet.getPhysicalNumberOfRows()则是获取的最后一行的行号(从1开始)。这里要注意的是循环列不是在sheet中循环,而是在row中循环。

posted @ 2017-05-11 20:25  ATJAVA  阅读(336)  评论(0编辑  收藏  举报