Commit f6ae26bc authored by Quxl's avatar Quxl

x

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