日期:2014-05-16  浏览次数:20868 次

操作Excel工具类(基于Apache的POI类库)

上一篇介绍的是基于jxl.jar的操作Excel工具类,但是jxl.jar的licence限制了它不能用于商业项目,所以本篇介绍Apache的POI类库。

?

上篇文章在这里

操作Excel工具类(基于jxl.jar)

?

功能简介:

1、向Excel文档插入数据,可以是多行可以是多列,保留原单元格格式不变

2、向Excel文档插入一个新行,并且使用与上一行完全相同的格式

3、等等

?

需要的第三方JAR包:

poi-3.8-20120326.jar

poi-examples-3.8-20120326.jar

poi-excelant-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

poi-ooxml-schemas-3.8-20120326.jar

poi-scratchpad-3.8-20120326.jar

stax-api-1.0.1.jar

?

完整的工具类的代码如下:

感谢gegewuqin9和在世界的中心呼喚愛在回复中建议,对读取单元格值的地方做了修改。

?

?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

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;

/**
 * Excel工具类
 * 
 * <pre>
 * 基于Apache的POI类库
 * </pre>
 * 
 * @author 陈峰
 */
public class POIExcelMakerUtil {

	private File excelFile;

	private InputStream fileInStream;

	private Workbook workBook;

	public POIExcelMakerUtil(File file) throws Exception {
		this.excelFile = file;
		this.fileInStream = new FileInputStream(this.excelFile);
		this.workBook = WorkbookFactory.create(this.fileInStream);
	}

	/**
	 * 写入一组值
	 * 
	 * @param sheetNum
	 *            写入的sheet的编号
	 * @param fillRow
	 *            是写入行还是写入列
	 * @param startRowNum
	 *            开始行号
	 * @param startColumnNum
	 *            开始列号
	 * @param contents
	 *            写入的内容数组
	 * @throws Exception
	 */
	public void writeArrayToExcel(int sheetNum, boolean fillRow,
			int startRowNum, int startColumnNum, Object[] contents)
			throws Exception {
		Sheet sheet = this.workBook.getSheetAt(sheetNum);
		writeArrayToExcel(sheet, fillRow, startRowNum, startColumnNum, contents);
	}

	/**
	 * 写入一组值
	 * 
	 * @param sheetNum
	 *            写入的sheet的名称
	 * @param fillRow
	 *            是写入行还是写入列
	 * @param startRowNum
	 *            开始行号
	 * @param startColumnNum
	 *            开始列号
	 * @param contents
	 *            写入的内容数组
	 * @throws Exception
	 */
	public void writeArrayToExcel(String sheetName, boolean fillRow,
			int startRowNum, int startColumnNum, Object[] contents)
			throws Exception {
		Sheet sheet = this.workBook.getSheet(sheetName);
		writeArrayToExcel(sheet, fillRow, startRowNum, startColumnNum, contents);
	}

	private void writeArrayToExcel(Sheet sheet, boolean fillRow,
			int startRowNum, int startColumnNum, Object[] contents)
			throws Exception {
		for (int i = 0, length = contents.length; i < length; i++) {
			int rowNum;
			int columnNum;
			// 以行为单位写入
			if (fillRow) {
				rowNum = startRowNum;
				columnNum = startColumnNum + i;
			}
			//  以列为单位写入
			else {
				rowNum = startRowNum + i;
				columnNum = startColumnNum;
			}
			this.writeToCell(sheet, rowNum, columnNum,
					convertString(contents[i]));
		}
	}

	/**
	 * 向一个单元格写入值
	 * 
	 * @param sheetNum
	 *            sheet的编号
	 * @param rowNum
	 *            行号
	 * @param columnNum
	 *            列号
	 * @param value
	 *            写入的值
	 * @throws Exception
	 */
	public void writeToExcel(int sheetNum, int rowNum, int columnNum,
			Object value) throws Exception {
		Sheet sheet = this.workBook.getSheetAt(sheetNum);
		this.writeToCell(sheet, rowNum, columnNum, value);
	}

	/**
	 * 向一个单元格写入值
	 * 
	 * @param sheetName
	 *            sheet的名称
	 * @param columnRowNum
	 *            单元格的位置
	 * @param value
	 *            写入的值
	 * @throws Exception
	 *