Commit f6ae26bc authored by Quxl's avatar Quxl

x

parent 43a90d05
......@@ -14,12 +14,12 @@ import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
/**
......@@ -78,8 +78,7 @@ public class ExcelUtil {
* @param widths 列宽度{24, 12, 15, 15}
*/
public static void excel(OutputStream os, Object[][] objs, Integer[] widths) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
try(HSSFWorkbook wb = new HSSFWorkbook()) {
HSSFCellStyle bodyStyle = wb.createCellStyle();
bodyStyle.setWrapText(true);
HSSFSheet sheet = wb.createSheet("sheet1");
......@@ -194,8 +193,7 @@ public class ExcelUtil {
* @throws IOException
*/
public static String[][] excel(InputStream is) {
try {
HSSFWorkbook wb = new HSSFWorkbook(is);
try (HSSFWorkbook wb = new HSSFWorkbook(is)) {
HSSFSheet sheet = wb.getSheetAt(0);
String[][] datas = new String[sheet.getLastRowNum()+1][];
for(int i = 0; i < datas.length; i++) {
......@@ -220,33 +218,31 @@ public class ExcelUtil {
* @return
*/
private static String stringVal(Cell cell) {
try {
int cellType = cell.getCellType();
if(cellType == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
return DateUtil.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
if(cell != null) {
CellType cellType = cell.getCellType();
if(cellType == CellType.NUMERIC) {
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
return DateUtil.format(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(cell.getNumericCellValue()));
} else {
return String.valueOf(FMT_INT.format(cell.getNumericCellValue()));
}
} else if(cellType == HSSFCell.CELL_TYPE_STRING) {
return String.valueOf(cell.getRichStringCellValue());
} else if(cellType == HSSFCell.CELL_TYPE_FORMULA) {
} else if(cellType == CellType.STRING) {
return cell.getStringCellValue();
} else if(cellType == CellType.FORMULA) {
String value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {
value = String.valueOf(cell.getRichStringCellValue());
}
return value;
} else if(cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
} else if(cellType == CellType.BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if(cellType == HSSFCell.CELL_TYPE_BLANK) {
return "";
} else if(cellType == HSSFCell.CELL_TYPE_ERROR) {
} else if(cellType == CellType.BLANK || cellType == CellType.ERROR) {
return "";
} else {
return String.valueOf(cell.getRichStringCellValue());
return cell.getStringCellValue();
}
} catch (Exception e) {
return null;
} else {
return "";
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment