POI操作Excel详解,HSSF和XSSF两种方式

HSSF方式:

package com.tools.poi.lesson1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

import com.tools.poi.bean.Student;

public class ExcelUtilWithHSSF {
	public static void main(String[] args) {
		try {
			getExcelAsFile("aaa");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
//		try {
//			CreateExcelDemo1();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		
		
	}
	
	/**
	 * 得到Excel,并解析内容
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{
		//1.得到Excel常用对象
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/test.xls"));
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
		//2.得到Excel工作簿对象
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		//3.得到Excel工作表对象
		HSSFSheet sheet = wb.getSheetAt(0);
		//总行数
		int trLength = sheet.getLastRowNum();
		//4.得到Excel工作表的行
		HSSFRow row = sheet.getRow(0);
		//总列数
		int tdLength = row.getLastCellNum();
		//5.得到Excel工作表指定行的单元格
		HSSFCell cell = row.getCell((short)1);
		//6.得到单元格样式
		CellStyle cellStyle = cell.getCellStyle();
		for(int i=0;i

XSSF方式:

package com.tools.poi.lesson1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.WorkbookUtil;

import com.tools.poi.bean.Student;

public class ExcelUtilWithXSSF {
	public static void main(String[] args) {
		try {
			getExcelAsFile("d:/FTP/系统报表.xls");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}
		
		
//		try {
//			CreateExcelDemo1();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		
		
	}
	
	/**
	 * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws InvalidFormatException 
	 */
	public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{
//		//1.得到Excel常用对象
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
//		//2.得到Excel工作簿对象
//		HSSFWorkbook wb = new HSSFWorkbook(fs);

		
		
		InputStream ins = null;   
        Workbook wb = null;   
            ins=new FileInputStream(new File(file));   
            //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);   
            wb = WorkbookFactory.create(ins);   
            ins.close();   
		
		
		//3.得到Excel工作表对象
		Sheet sheet = wb.getSheetAt(0);
		//总行数
		int trLength = sheet.getLastRowNum();
		//4.得到Excel工作表的行
		Row row = sheet.getRow(0);
		//总列数
		int tdLength = row.getLastCellNum();
		//5.得到Excel工作表指定行的单元格
		Cell cell = row.getCell((short)1);
		//6.得到单元格样式
		CellStyle cellStyle = cell.getCellStyle();

		for(int i=5;i

注意:

修改Excel中的某个内容:

cell1.setCellValue("1000");

保存修改后的Excel文件:

OutputStream out = new FileOutputStream(file);
wb.write(out);

轉自【http://blog.csdn.net/he90227/article/details/37563497】

你可能感兴趣的:(Java体系)