java+maven+excel(獲取excel內容 用於導入數據庫)

**

廢話不多說 直接上代碼

**

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
 * Created by Lenovo on 2018/1/21.
 */
public class ReadExcel {
	/** 
	 * 讀取Excel測試,兼容 Excel 2003/2007/2010 
	 */  
	public static void  main(String args[])  {  
	    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
	    try {  
	        //同時支持Excel 2003、2007  
	        //Excel文件所在位置
	        File excelFile = new File("D://aa.xlsx"); //創建文件對象  
	        FileInputStream is = new FileInputStream(excelFile); //文件流  
	        Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的  
	        int sheetCount = workbook.getNumberOfSheets();  //Sheet的數量  
	        //遍歷每個Sheet  
	        for (int s = 0; s < sheetCount; s++) {  
	            Sheet sheet = workbook.getSheetAt(s);  
	            int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數  
	            //遍歷每一行  
	            for (int r = 0; r < rowCount; r++) {  
	                Row row = sheet.getRow(r);  
	                int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數  
	                //遍歷每一個單元格  
	                
	             
	                for (int c = 0; c < cellCount; c++) {  
	                    Cell cell = row.getCell(c);  
	                    int cellType = cell.getCellType();  
	                    String cellValue = null;
	                    //在讀取單元格內容前,設置所有單元格中內容都是字符串類型
	                    cell.setCellType(Cell.CELL_TYPE_STRING);
	                    //按照字符串類型讀取單元格內數據
	                    cellValue = cell.getStringCellValue();
	                    /*在這裏可以對每個單元格中的值進行二次操作轉化*/
	                    System.out.print(cellValue + "  "); 
	                }  
	                System.out.println(); 
	            }  
	        }  
	  
	    }  
	    catch (Exception e) {  
	        e.printStackTrace();  
	    }  
	  
	}
}

Pom

<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml-schemas</artifactId>
		    <version>3.17</version>
		</dependency>
		 <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-scratchpad</artifactId>
		    <version>3.17</version>
		</dependency>
		  <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.17</version>
		      </dependency>  
		  <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-examples</artifactId>
		    <version>3.17</version>
		      </dependency>  
		  <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-excelant</artifactId>
		    <version>3.17</version>
		      </dependency>  
		  <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.17</version>
		      </dependency>  
		  <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
		<dependency>
		    <groupId>org.apache.xmlbeans</groupId>
		    <artifactId>xmlbeans</artifactId>
		    <version>2.6.0</version>
		</dependency>

運行結果展示
在這裏插入圖片描述
根據自己所需 創建對應對象 存放屬性字段,調用sql參入數據庫即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章