Java Poi determines whether excel is xlsx or xls
rely
<dependency> <groupId></groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency>
Code
package ; import ; import ; import ; import ; import ; import ; import ; import ; public class ExcelRateOneTest { public static void main(String[] args) { File file = new File("D:\\The Young Cadre Talent Base List.xls"); try { Workbook workbook = null; try { workbook = new XSSFWorkbook((())); } catch (Exception e) { ("This excel file is not type xlsx"+()); } try { if (null == workbook) { workbook = new HSSFWorkbook((())); } } catch (Exception e) { ("This file is not an excel file"+()); } if (workbook != null) { Sheet sheet = (0); Row row = (0); Cell cell = (0); ("Cell content:" + getCellValue(cell)); } }catch (Exception e){ (()); } } /** * Get the cell content according to the cell type and convert the content to a string type * @param cell * @return */ public static String getCellValue(Cell cell) { if (cell == null) { return ""; } switch (()) { case STRING: return (); case NUMERIC: return () + ""; case BOOLEAN: return () + ""; case FORMULA: return () + ""; default: return ""; } } }
Knowledge Supplement
I have encountered the problem of handling excel. I have been searching online for a long time and feel that their code is too messy and complicated. This is my simplified version of excel processing code, which is simple and violent.
First of all, why use poi? jxl can only handle excel before version 03, that is, ending with xlss, and cannot handle xlsx. Poi is compatible with two formats. When poi parses files in the two formats, the only difference is that when xls is HSSF; when xlsx is XSSF.
First is a small way to deal with Cell objects:
//This method processes the cell, passes in the cell object, returns the content in the cell, and the String type.public static String getCellFormatValue(Cell cell) { String cellValue = ""; if (cell != null) { // Determine cell type switch (()) { case Cell.CELL_TYPE_NUMERIC: { cellValue = (()); break; } case Cell.CELL_TYPE_STRING: { cellValue = ().getString(); break; } default: cellValue = ""; } } return cellValue; }
Two codes for handling excel:
When xls:
InputStream inputStream = new FileInputStream("Absolute path.xls"); HSSFWorkbook workbook = new HSSFWorkbook(inputStream); HSSFSheet s = (0); for (int j = 0; j < (); j++) {//Get the total number of rows Row row = (j); // Take out the i-th line getRow(index) Get the (j) line for (int k = 0; k < (); k++) { // getPhysicalNumberOfCells() Gets the total number of columns in the current row String value1 = getCellFormatValue((k));//Take out the value of the j-th column k-th column (value1); } } ();
When xlsx:
InputStream inputStream = new FileInputStream("Absolute path.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); XSSFSheet s = (0); for (int j = 0; j < (); j++) {//Get the total number of rows Row row = (j); // Take out the i-th line getRow(index) Get the (j) line for (int k = 0; k < (); k++) { // getPhysicalNumberOfCells() Gets the total number of columns in the current row String value1 = getCellFormatValue((k));//Take out the value of the j-th column k-th column (value1); } } ();
The above is the detailed content of java poi to determine whether excel is xlsx or xls type. For more information about java to determine whether excel is xlsx or xls, please follow my other related articles!