package com.test.opeZip;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.collect.Maps;
public class ExcelUtils {
// @Value("${file_base_path}")
// private static String fileBasePath;//文件的基础路径
// private static String fileBasePath = System.getProperty("user.dir") + File.separator + "excel" + File.separator;;//文件的基础路径
public static final String OFFICE_EXCEL_XLS = "xls";
public static final String OFFICE_EXCEL_XLSX = "xlsx";
/**
* 读取指定Sheet也的内容
* @param filepath filepath 文件全路径
* @param sheetNo sheet序号,从0开始,如果读取全文sheetNo设置null
*/
public static String readExcel(String filepath, Integer sheetNo)
throws EncryptedDocumentException, InvalidFormatException, IOException {
StringBuilder sb = new StringBuilder();
Workbook workbook = getWorkbook(filepath);
if (workbook != null) {
if (sheetNo == null) {
//获取Excel页数
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = workbook.getSheetAt(i);
//判断是否为空页
if (sheet == null) {
continue;
}
sb.append(readExcelSheet(sheet));
}
} else {
Sheet sheet = workbook.getSheetAt(sheetNo);
if (sheet != null) {
sb.append(readExcelSheet(sheet));
}
}
}
return sb.toString();
}
/**
* 根据文件路径获取Workbook对象
* @param filepath 文件全路径
*/
public static Workbook getWorkbook(String filepath)
throws EncryptedDocumentException, InvalidFormatException, IOException {
InputStream is = null;
Workbook wb = null;
if (StringUtils.isBlank(filepath)) {
throw new IllegalArgumentException("文件路径不能为空");
} else {
//获取文件名后缀
String suffiex = getSuffiex(filepath);
if (StringUtils.isBlank(suffiex)) {
throw new IllegalArgumentException("文件后缀不能为空");
}
if (OFFICE_EXCEL_XLS.equals(suffiex) || OFFICE_EXCEL_XLSX.equals(suffiex)) {
try {
is = new FileInputStream(filepath);
wb = WorkbookFactory.create(is);
} finally {
if (is != null) {
is.close();
}
if (wb != null) {
读写Excel万能模板(POI)
最新推荐文章于 2023-08-08 17:57:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)