读取excel文件中的信息实例1
来源:互联网 发布:windows gcc搭建 编辑:程序博客网 时间:2024/06/05 20:24
package excel;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import org.apache.commons.lang3.StringUtils;import org.apache.poi.hssf.usermodel.HSSFCell;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.DateUtil;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelUtils { /** * suffix of excel 2003 */ public static final String OFFICE_EXCEL_V2003_SUFFIX = "xls"; /** * suffix of excel 2007 */ public static final String OFFICE_EXCEL_V2007_SUFFIX = "xlsx"; /** * suffix of excel 2010 */ public static final String OFFICE_EXCEL_V2010_SUFFIX = "xlsx"; public static final String EMPTY = ""; public static final String DOT = "."; public static final String LIB_PATH = "lib"; public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + DOT + OFFICE_EXCEL_V2003_SUFFIX; public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + DOT + OFFICE_EXCEL_V2007_SUFFIX; public static final String NOT_EXCEL_FILE = " is Not a Excel file!"; public static final String PROCESSING = "Processing..."; public static void main(String[] args) throws IOException { try { List<Student> list = readExcel("E:\\test\\excel\\student.xlsx"); System.out.println(list); } catch (Exception e) { e.printStackTrace(); } } /** * Check which version of The Excel file is. Throw exception if Excel file path is illegal. * * @param path the Excel file * @return a list that contains Students from Excel. * @throws IOException */ public static List<Student> readExcel(String path) throws IOException, IllegalArgumentException { if (StringUtils.isBlank(path)) { throw new IllegalArgumentException(path + " excel file path is either null or empty"); } else { String suffiex = getSuffiex(path); if(StringUtils.isBlank(suffiex)){ throw new IllegalArgumentException(path + " suffiex is either null or empty"); } if (OFFICE_EXCEL_V2003_SUFFIX.equals(suffiex)) { return readXls(path); } else if (OFFICE_EXCEL_V2007_SUFFIX.equals(suffiex)) { return readXlsx(path); } else if (OFFICE_EXCEL_V2010_SUFFIX.equals(suffiex)) { return readXlsx(path); } else { throw new IllegalArgumentException(path + NOT_EXCEL_FILE); } } } /** * Read the Excel 2017 or 2010 * @param path the path of the excel file * @return * @throws IOException */ public static List<Student> readXlsx(String path) throws IOException { System.out.println(PROCESSING + path); InputStream is = new FileInputStream(path); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); Student student = null; List<Student> list = new ArrayList<Student>(); // Read the Sheet for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // Read the Row for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) { student = new Student(); XSSFCell no = xssfRow.getCell(0); XSSFCell name = xssfRow.getCell(1); XSSFCell age = xssfRow.getCell(2); XSSFCell score = xssfRow.getCell(3); student.setNo(getStringVal(no)); student.setName(getStringVal(name)); student.setAge(getStringVal(age)); student.setScore(Float.valueOf(getStringVal(score))); list.add(student); } } } return list; } /** * Read the Excel 2003 * @param path the path of the Excel * @return * @throws IOException */ public static List<Student> readXls(String path) throws IOException { System.out.println(PROCESSING + path); InputStream is = new FileInputStream(path); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); Student student = null; List<Student> list = new ArrayList<Student>(); // Read the Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // Read the Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { student = new Student(); HSSFCell no = hssfRow.getCell(0); HSSFCell name = hssfRow.getCell(1); HSSFCell age = hssfRow.getCell(2); HSSFCell score = hssfRow.getCell(3); student.setNo(getStringVal(no)); student.setName(getStringVal(name)); student.setAge(getStringVal(age)); student.setScore(Float.valueOf(getStringVal(score))); list.add(student); } } } return list; } @SuppressWarnings("static-access") private static String getValue(XSSFCell xssfCell) { if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) { return String.valueOf(xssfCell.getBooleanCellValue()); } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) { return String.valueOf(xssfCell.getNumericCellValue()); } else { return String.valueOf(xssfCell.getStringCellValue()); } } @SuppressWarnings("static-access") private static String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { return String.valueOf(hssfCell.getNumericCellValue()); } else { return String.valueOf(hssfCell.getStringCellValue()); } } public static String getSuffiex(String path) { if(StringUtils.isBlank(path)){ return EMPTY; } int index = path.lastIndexOf(DOT); if (index == -1) { return EMPTY; } return path.substring(index + 1, path.length()); } public static String getStringVal(HSSFCell cell){ switch(cell.getCellType()){ case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue()?"TRUE":"FALSE"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cell.getDateCellValue()); } cell.setCellType(Cell.CELL_TYPE_STRING);; case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return EMPTY; } } public static String getStringVal(XSSFCell cell){ switch(cell.getCellType()){ case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue()?"TRUE":"FALSE"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cell.getDateCellValue()); } cell.setCellType(Cell.CELL_TYPE_STRING); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return EMPTY; } }}class Student { /** * id */ private Integer id; /** * 学号 */ private String no; /** * 姓名 */ private String name; /** * 学院 */ private String age; /** * 成绩 */ private float score; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; }}
阅读全文
0 0
- 读取excel文件中的信息实例1
- 读取excel文件信息实例2
- [ahk]读取excel文件实例
- 读取Excel文件中的数据
- php读取excel文件的数据信息
- Workbook解析excel文件 读取信息
- 如何读取EXCEL文件中的图片?
- jxl读取excel文件中的时间类型
- vb.net 读取EXCEL文件中的数据
- Java读取Excel文件中的数据
- 读取Excel中的文件加入到数据库
- MATLAB如何读取excel文件中的数据?
- MATLAB读取Excel,Txt文件中的数据
- java读取excel文件中的数据
- 读取properties文件中的配置信息
- android读取properties文件中的信息
- java读取EXCEL文件1
- Excel学习1_Java读取文件中的内容写入excel中
- 两个目录互相备份,相同文件取最新
- HDU 3255 Farming
- javascript中的原型继承
- eclipse环境下如何使用断点调试?
- Java实现字符串的反转
- 读取excel文件中的信息实例1
- Xcode NSLog输出带时间、文件名、行、方法名
- Bigdata Development Web_Study_05(JSP中九大内置对象)
- linux 服务器部署 lnamp 四、mysql
- unity中如何实现画面滚动
- javaScript学习笔记
- 【Lua学习笔记】Lua中协程的使用
- innerHTML基础知识
- hdu 6085