• Java读取Excel文件(包括xls和xlsx)的样例程序


    样例程序如下所示,其中:

    • parseXls()函数依赖于jxl,只能读取xls格式文件;
    • parseExcel()函数依赖于apache poi,能够读取xls和xlsx两种格式的文件。
      jxl的依赖:
    		<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
    		<dependency>
    			<groupId>net.sourceforge.jexcelapi</groupId>
    			<artifactId>jxl</artifactId>
    			<version>2.6.12</version>
    		</dependency>
    

    apache poi的依赖:

    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.10-FINAL</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml</artifactId>
    			<version>3.10-FINAL</version>
    		</dependency>
    

    示例代码:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import jxl.Sheet;
    import jxl.Workbook;
    
    public class ExcelParser {
    
    	public List<List<String>> parseXls(File file) {
    		try {
    			Workbook workbook = Workbook.getWorkbook(file);
    			Sheet sheet = workbook.getSheet(0);
    			List<List<String>> list = new ArrayList<List<String>>();
    			for (int i = 0; i < sheet.getRows(); i++) {
    				List<String> rowList = new ArrayList<String>();
    				for (int j = 0; j < sheet.getColumns(); j++) {
    					rowList.add(sheet.getCell(j, i).getContents());
    				}
    				list.add(rowList);
    			}
    			// test
    			for (List<String> rowList : list) {
    				for (String s : rowList)
    					System.out.print(s + ",");
    				System.out.println();
    			}
    
    			return list;
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    	public List<List<String>> parseExcel(File file) {
    		try {
    			InputStream fis = new FileInputStream(file);
    			String fileName = file.getName();
    			org.apache.poi.ss.usermodel.Workbook workbook = null;
    			if (fileName.toLowerCase().endsWith("xlsx")) {
    				workbook = new XSSFWorkbook(fis);
    			} else if (fileName.toLowerCase().endsWith("xls")) {
    				workbook = new HSSFWorkbook(fis);
    			}
    			org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
    			List<List<String>> list = new ArrayList<List<String>>();
    			Iterator<Row> rowIterator = sheet.iterator();
    			while (rowIterator.hasNext()) {
    				Row row = rowIterator.next();
    				List<String> rowList = new ArrayList<String>();
    				Iterator<Cell> cellIterator = row.cellIterator();
    				while (cellIterator.hasNext()) {
    					Cell cell = cellIterator.next();
    					switch(cell.getCellType()) {
    					case Cell.CELL_TYPE_NUMERIC:
    						rowList.add("" + cell.getNumericCellValue());
    						break;
    					case Cell.CELL_TYPE_STRING:
    					default: 
    						rowList.add(cell.getStringCellValue());
    						break;
    					}
    				}
    				list.add(rowList);
    			}
    			// test
    			for (List<String> rowList : list) {
    				for (String s : rowList)
    					System.out.print(s + ",");
    				System.out.println();
    			}
    			return list;
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    
    	}
    
    	public static void main(String[] args) {
    		new ExcelParser().parseExcel(new File("C:\Users\zifeiy\Documents\zifeiy.xlsx"));
    	}
    
    }
    
    

    参考链接:https://www.journaldev.com/2562/apache-poi-tutorial

  • 相关阅读:
    PHP防盗链的基本思想&&防盗链的设置方法
    PHP程序员遇到职业问题时,是离职?还是坚持?
    如何学习Linux性能优化?
    PHP到底有多牛?你所知道的网站都在用它
    Linux 与 Unix 到底有什么不同?区别在哪?
    用PHP打造一个高性能好用的网站
    七夕-心形表白-简单css代码
    css雪碧图-css精灵图
    jquery-时间轴滑动
    js-进度条-动画
  • 原文地址:https://www.cnblogs.com/zifeiy/p/9592662.html
Copyright © 2020-2023  润新知