···
/**
- 项目名称:m2m
- 文件名称: ExcelUtil.java
package com.syfoxconn.admin.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
/**
- EXCEL 操作工具类。
- @author Administrator
*/
public class ExcelUtil {
private static final Logger logger = Logger.getLogger(ExcelUtil.class);
public static String[] getSheetNames(String fullFileName) {
if (StringUtils.isBlank(fullFileName)) {
logger.error("未指定文件名称。");
return null;
}
if (!fullFileName.endsWith(".xls") && !fullFileName.endsWith(".xlsx") &&
!fullFileName.endsWith(".xlsm")) {
logger.error("【" + fullFileName + "】不是 excel 文件。");
return null;
}
File file = new File(fullFileName);
if (!file.exists()) {
logger.error("文件【" + fullFileName + "】不存在。");
return null;
}
HSSFWorkbook hssfWb = null;
XSSFWorkbook xssfWb = null;
FileInputStream fis = null;
int num;
String[] result;
try {
fis = new FileInputStream(file);
if (fullFileName.endsWith(".xls")) {
hssfWb = new HSSFWorkbook(fis);
num = hssfWb.getNumberOfSheets();
result = new String[num];
for (int i = 0; i < num; i++) {
result[i] = hssfWb.getSheetName(i);
}
hssfWb.close();
hssfWb = null;
} else {
xssfWb = new XSSFWorkbook(fis);
num = xssfWb.getNumberOfNames();
result = new String[num];
for (int i = 0; i < num; i++) {
result[i] = xssfWb.getSheetName(i);
}
xssfWb.close();
xssfWb = null;
}
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.error("读取文件【" + fullFileName + "】时发生了异常", e);
return null;
}
return result;
}
public static String[][] readFromExcel(String fullFileName) {
return readFromExcel(fullFileName, 0);
}
public static String[][] readFromExcel(String fullFileName, ExcelReadConfig ioConfig) {
return readFromExcel(fullFileName, 0, ioConfig);
}
public static String[][] readFromExcel(String fullFileName, String sheetName) {
return readFromExcel(fullFileName, sheetName, null);
}
public static String[][] readFromExcel(String fullFileName, String sheetName, ExcelReadConfig ioConfig) {
if (StringUtils.isBlank(fullFileName)) {
logger.error("未指定文件名称。");
return null;
}
if (!fullFileName.endsWith(".xls") && !fullFileName.endsWith(".xlsx") &&
!fullFileName.endsWith(".xlsm")) {
logger.error("【" + fullFileName + "】不是 excel 文件。");
return null;
}
File file = new File(fullFileName);
if (!file.exists()) {
logger.error("文件【" + fullFileName + "】不存在。");
return null;
}
HSSFWorkbook hssfWb = null;
XSSFWorkbook xssfWb = null;
FileInputStream fis = null;
int sheetIndex = -1;
try {
fis = new FileInputStream(file);
if (fullFileName.endsWith(".xls")) {
hssfWb = new HSSFWorkbook(fis);
sheetIndex = hssfWb.getSheetIndex(sheetName);
hssfWb.close();
hssfWb = null;
} else {
xssfWb = new XSSFWorkbook(fis);
sheetIndex = xssfWb.getSheetIndex(sheetName);
xssfWb.close();
xssfWb = null;
}
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.error("读取文件【" + fullFileName + "】时发生了异常", e);
return null;
}
if (sheetIndex < 0) {
logger.error("文件【" + fullFileName + "】中不存在 sheet 【" + sheetName + "】");
return null;
}
return readFromExcel(fullFileName, sheetIndex, ioConfig, false);
}
public static String[][] readFromExcel(String fullFileName, int sheetIndex) {
return readFromExcel(fullFileName, sheetIndex, null);
}
public static String[][] readFromExcel(String fullFileName, int sheetIndex, ExcelReadConfig ioConfig) {
return readFromExcel(fullFileName, sheetIndex, ioConfig, true);
}
private static String[][] readFromExcel(String fullFileName, int sheetIndex, ExcelReadConfig ioConfig, boolean doInvalidFileCheck) {
File file = new File(fullFileName);
if (doInvalidFileCheck) {
if (StringUtils.isBlank(fullFileName)) {
logger.error("未指定文件名称。");
return null;
}
if (!fullFileName.endsWith(".xls") && !fullFileName.endsWith(".xlsx") &&
!fullFileName.endsWith(".xlsm")) {
logger.error("【" + fullFileName + "】不是 excel 文件。");
return null;
}
if (!file.exists()) {
logger.error("文件【" + fullFileName + "】不存在。");
return null;
}
}
HSSFWorkbook hssfWb = null;
XSSFWorkbook xssfWb = null;
Sheet sheet = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
if (fullFileName.endsWith(".xls")) {
hssfWb = new HSSFWorkbook(fis);
sheet = hssfWb.getSheetAt(sheetIndex);
} else {
xssfWb = new XSSFWorkbook(fis);
sheet = xssfWb.getSheetAt(sheetIndex);
}
if (sheet == null) {
logger.error("索引越界【" + sheetIndex + "】");
}
} catch (Exception e) {
logger.error("读取文件【" + fullFileName + "】时发生了异常", e);
return null;
}
int startRowIdx = 0;
int endRowIdx = -1;
int startColIdx = 0;
int endColIdx = -1;
if (ioConfig != null) {
startRowIdx = ioConfig.getStartRowIndex() < 0 ? 0 : ioConfig.getStartRowIndex();
endRowIdx = ioConfig.getEndRowIndex() < 0 ? -1 : ioConfig.getEndRowIndex();
startColIdx = ioConfig.getStartColumnIndex() < 0 ? 0 : ioConfig.getStartColumnIndex();
endColIdx = ioConfig.getEndColumnIndex() < 0 ? -1 : ioConfig.getEndColumnIndex();
}
if (endRowIdx == -1) {
endRowIdx = sheet.getLastRowNum();
}
int rowCount = endRowIdx - startRowIdx + 1;
if (rowCount <= 0) {
return null;
}
int colCount = endColIdx - startColIdx + 1;
if (endColIdx != -1 && colCount <= 0) {
return null;
}
String[][] result = new String[rowCount][];
String[] rowData;
Row row = null;
Cell cell = null;
int colCountPerRow = 0;
int realEndColIdx = 0;
int realStartColIdx = 0;
for (int i = startRowIdx; i <= endRowIdx; i++) {
row = sheet.getRow(i);
if (row == null) {
result[i - startRowIdx] = new String[0];
continue;
}
// getLastCellNum():取得最后一列的序号,该值为索引值 + 1。
colCountPerRow = row.getLastCellNum();
if (colCountPerRow <= 0) {
result[i - startRowIdx] = new String[0];
continue;
}
if (colCountPerRow < startColIdx + 1) {
result[i - startRowIdx] = new String[0];
continue;
}
realStartColIdx = startColIdx;
if (colCountPerRow < endColIdx + 1) {
realEndColIdx = colCountPerRow;
} else {
realEndColIdx = colCountPerRow - 1;
}
rowData = new String[realEndColIdx - realStartColIdx + 1];
result[i - startRowIdx] = rowData;
for (int j = realStartColIdx; j <= realEndColIdx; j++) {
cell = row.getCell(j);
if (cell == null) {
rowData[j] = null;
} else {
int type = cell.getCellType();
switch (type) {
case Cell.CELL_TYPE_BLANK :
rowData[j] = "";
break;
case Cell.CELL_TYPE_BOOLEAN :
rowData[j] = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR :
rowData[j] = "";
break;
case Cell.CELL_TYPE_FORMULA :
try {
rowData[j] = String.valueOf(cell.getNumericCellValue());
} catch (Exception e) {
rowData[j] = cell.getRichStringCellValue().getString();
}
break;
case Cell.CELL_TYPE_NUMERIC :
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
rowData[j] = DateFormatUtils.format(date, "yyyy/MM/dd");
} else {
// 所有的浮点数都精确到4 位小数
DecimalFormat df = new DecimalFormat("#.####");
rowData[j] = df.format(cell.getNumericCellValue());
// 如果是整数(以 4 个 0 结尾),则取整
if (rowData[j].endsWith("0000")) {
rowData[j] = rowData[j].substring(0, rowData[j].indexOf('.'));
}
}
break;
case Cell.CELL_TYPE_STRING :
rowData[j] = cell.getStringCellValue();
break;
default :
rowData[j] = "";
break;
}
}
}
}
try {
if (hssfWb != null) {
hssfWb.close();
}
if (xssfWb != null) {
xssfWb.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.error("文件【" + fullFileName + "】关闭时发生了异常。");
}
return result;
}
public static String writeToExcel(List
}
···