Created
June 26, 2018 06:38
-
-
Save lonly197/439c4ccf67838fc0c7b68081ad7c617d to your computer and use it in GitHub Desktop.
Excel解析工具类(依赖于poi-ooxml)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.collect.Lists; | |
import org.apache.commons.lang3.StringUtils; | |
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.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import javax.servlet.http.HttpServletRequest; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.text.DecimalFormat; | |
import java.util.List; | |
/** | |
* Created by lonly on 2018/6/26. | |
*/ | |
public class ExcelUtils { | |
private static final String OFFICE_EXCEL_2003_POSTFIX = "xls"; | |
private static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx"; | |
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0"); | |
public static List<Long> readItemIdFromExcel(HttpServletRequest request) throws IOException { | |
String typeNameFix = request.getHeader("X-File-Name"); | |
String type = MapUtils.getFileExt(typeNameFix); | |
List<Long> list = null; | |
if (OFFICE_EXCEL_2003_POSTFIX.equals(type.toLowerCase())) { | |
list = readXls(request.getInputStream()); | |
} else if (OFFICE_EXCEL_2010_POSTFIX.equals(type.toLowerCase())) { | |
list = readXlsx(request.getInputStream()); | |
} | |
return list; | |
} | |
/** | |
* .xls结尾的文件 | |
* | |
* @param is 文件流 | |
* @return | |
* @throws IOException | |
*/ | |
private static List<Long> readXlsx(InputStream is) throws IOException { | |
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); | |
List<Long> list = Lists.newArrayList(); | |
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { | |
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); | |
if (xssfSheet == null) { | |
continue; | |
} | |
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { | |
XSSFRow xssfRow = xssfSheet.getRow(rowNum); | |
if (xssfRow != null) { | |
String result = getValue(xssfRow.getCell(0)); | |
if (StringUtils.isNotEmpty(result)) { | |
list.add(Long.valueOf(result)); | |
} | |
} | |
} | |
} | |
return list; | |
} | |
/** | |
* .xls结尾的文件 | |
* | |
* @param is 文件流 | |
* @return | |
* @throws IOException | |
*/ | |
private static List<Long> readXls(InputStream is) throws IOException { | |
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); | |
List<Long> list = Lists.newArrayList(); | |
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { | |
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); | |
if (hssfSheet == null) { | |
continue; | |
} | |
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { | |
HSSFRow hssfRow = hssfSheet.getRow(rowNum); | |
if (hssfRow != null) { | |
String result = getValue(hssfRow.getCell(0)); | |
if (StringUtils.isEmpty(result)) { | |
list.add(Long.valueOf(result)); | |
} | |
} | |
} | |
} | |
return list; | |
} | |
@SuppressWarnings("static-access") | |
private static String getValue(Cell cell) { | |
if (cell.getCellTypeEnum() == CellType.BOOLEAN) { | |
return String.valueOf(cell.getBooleanCellValue()); | |
} else if (cell.getCellTypeEnum() == CellType.NUMERIC) { | |
return String.valueOf(DECIMAL_FORMAT.format(cell.getNumericCellValue())); | |
} else { | |
return String.valueOf(cell.getStringCellValue()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> | |
<dependency> | |
<groupId>org.apache.poi</groupId> | |
<artifactId>poi-ooxml</artifactId> | |
<version>3.17</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment