Java POI 工具类

9 篇文章 0 订阅
2 篇文章 0 订阅

Java POI 工具类

org.apache.poi 工具类

package replace.your.package;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

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

public class PoiUtils {

	/**
	 * 获取工作簿
	 * 
	 * @param excelFilePath Excel文件路径
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(String excelFilePath) throws EncryptedDocumentException, IOException {
		return getWorkbook(new File(excelFilePath));
	}

	/**
	 * 获取工作簿
	 * 
	 * @param excelFile Excel文件
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(File excelFile) throws EncryptedDocumentException, IOException {
		return WorkbookFactory.create(excelFile);
	}

	/**
	 * 获取工作簿
	 * 
	 * @param excelFileInStream Excel文件输入流
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(InputStream excelFileInStream) throws EncryptedDocumentException, IOException {
		return WorkbookFactory.create(excelFileInStream);
	}

	/**
	 * 获取工作簿
	 * 
	 * @param excelFile Excel文件
	 * @param password  文件密码
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(File excelFile, String password) throws EncryptedDocumentException, IOException {
		return getWorkbook(excelFile, password, false);
	}

	/**
	 * 获取工作簿
	 * 
	 * @param excelFileInStream Excel文件输入流
	 * @param password          文件密码
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(InputStream excelFileInStream, String password)
			throws EncryptedDocumentException, IOException {
		return WorkbookFactory.create(excelFileInStream, password);
	}

	/**
	 * 获取工作簿
	 * 
	 * @param excelFile Excel文件
	 * @param password  文件密码
	 * @param readonly  是否只读
	 * @return 工作簿
	 * @throws EncryptedDocumentException 加密文档异常
	 * @throws IOException                IO异常
	 */
	public static Workbook getWorkbook(File excelFile, String password, boolean readonly)
			throws EncryptedDocumentException, IOException {
		// 检查文件是否存在
		if (!excelFile.exists()) { // 文件不存在
			return null;
		}
		// 返回工作簿
		return WorkbookFactory.create(excelFile, password, readonly);
	}

	/**
	 * 获取单元格字符串值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 字符串值
	 */
	public static String getCellValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格字符串值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 字符串值
	 */
	public static String getCellValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.toString();
	}

	/**
	 * 获取单元格字符串值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 字符串值
	 */
	public static String getCellStringValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellStringValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格字符串值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 字符串值
	 */
	public static String getCellStringValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.getStringCellValue();
	}

	/**
	 * 获取单元格富文本值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 富文本值
	 */
	public static RichTextString getCellRichTextValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellRichTextValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格富文本值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 富文本值
	 */
	public static RichTextString getCellRichTextValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.getRichStringCellValue();
	}

	/**
	 * 获取单元格数字值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 数字值
	 */
	public static Double getCellNumericValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellNumericValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格数字值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 数字值
	 */
	public static Double getCellNumericValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.getNumericCellValue();
	}

	/**
	 * 获取单元格日期值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 日期值
	 */
	public static Date getCellDateValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellDateValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格日期值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 日期值
	 */
	public static Date getCellDateValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.getDateCellValue();
	}

	/**
	 * 获取单元格布尔值
	 * 
	 * @param sheet       sheet
	 * @param rowIndex    行index
	 * @param columnIndex 列index
	 * @return 布尔值
	 */
	public static Boolean getCellBooleanValue(Sheet sheet, int rowIndex, int columnIndex) {
		if (sheet == null || rowIndex < 0 || columnIndex < 0) {
			return null;
		}
		return getCellBooleanValue(sheet.getRow(rowIndex), columnIndex);
	}

	/**
	 * 获取单元格布尔值
	 * 
	 * @param row         行:Row对象
	 * @param columnIndex 列index
	 * @return 布尔值
	 */
	public static Boolean getCellBooleanValue(Row row, int columnIndex) {
		if (row == null || columnIndex < 0) {
			return null;
		}
		Cell cell = row.getCell(columnIndex);
		if (cell == null) {
			return null;
		}
		return cell.getBooleanCellValue();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值