POI3.9读写Excel

使用POI3.9对excel进行读写

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;

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;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;

public class Copy_2_of_TestExcel {
	public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
		String filePath = "C:\\A.xls";
		HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(filePath));
		HSSFSheet sheet = book.getSheetAt(0);
		FormulaEvaluator evaluator = book.getCreationHelper().createFormulaEvaluator();
		
		sheet.getRow(15).getCell(3).setCellValue("bbb");
		
		for(int i=12;i<31;i++){
			HSSFRow row1 = sheet.getRow(i);
			HSSFCell cell1 = row1.getCell(3);
			if (Cell.CELL_TYPE_FORMULA == cell1.getCellType()){
				try{
					System.out.println((i+1) + "行:" + cell1.toString() + ","  + evaluator.evaluate(cell1).getStringValue());
				}catch (Exception e) {
					System.out.println((i+1) + "行:" + cell1.toString() + "," + cell1.toString() + ","  + evaluator.evaluate(cell1).getNumberValue());
				}
			}else if(cell1.toString() != "" && cell1.toString() != null){
				System.out.println(i + ":" + cell1.toString());
			}
		}
	}
}

 所需jar包见附件。

你可能感兴趣的:(Excel,POI)