java 導入excle

 

說一下大體的思路:

 

1.首先通過頁面的上傳文件,選擇excle;

2.通過ajax 傳到後臺(有回調函數);

3.後臺處理,返回數據;

因爲我想將數據先檢查數據,是否滿足我們的插入條件,執行回調函數,假如其中的數據有問題,就將在頁面彈框顯示,有問題的原因(後面我是輸出在控制檯,還沒有傳到前臺),。我只是將數據打印在控制檯,也還沒有寫入數據庫,所以其中寫入數據庫部分,要將輸出語句改爲插入到數據庫的方法,這個後面會完成

 

一。先上前臺和ajax

 


 

導入數據

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

二。後臺

package com.ciitc.hsi.controller;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ciitc.hsi.bean.BaseResponseBean;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	//這個是響應實體
	BaseResponseBean brb=new BaseResponseBean();

	

	//對取得的列中的數據類型進行判斷
	public static String  getCellValue(HSSFCell cell) {  
        String cellValue = "";  
        DecimalFormat df = new DecimalFormat("#");  
      	  switch (cell.getCellType()) {  
            case HSSFCell.CELL_TYPE_STRING:  
                cellValue = cell.getRichStringCellValue().getString().trim();  
                break;  
            case HSSFCell.CELL_TYPE_NUMERIC:  
                cellValue = df.format(cell.getNumericCellValue()).toString();  
                break;  
            case HSSFCell.CELL_TYPE_BOOLEAN:  
                cellValue = String.valueOf(cell.getBooleanCellValue()).trim();  
                break;  
            case HSSFCell.CELL_TYPE_FORMULA:  
                cellValue = cell.getCellFormula();  
                break;  
            default:  
            	 cellValue = "";
            }  
        return cellValue.toString();  
    } 
	
	
	
	
	
	/*方法二:在不滿足條件的字段,給出錯誤提示*/
	@RequestMapping(value="doCheck",method=RequestMethod.POST)
	public @ResponseBody  String doCheck(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("uploadexcle") MultipartFile file) {
		try {
			//獲取excle的輸入流
			InputStream inputStream=file.getInputStream();
			POIFSFileSystem fs=new POIFSFileSystem(inputStream);
			HSSFWorkbook workbook=new HSSFWorkbook(fs);
			
			//設置開始行爲
			int  currentPosition=0;
			//設置當前位置
			int currentSheet=0;
			//設置sheet數
			int numOfSheet=workbook.getNumberOfSheets();
			
			//獲取第一個sheet 
			HSSFSheet sheet=workbook.getSheetAt(currentSheet);
			//getLastRowNum  是列
			//判斷當前行是否到達sheet的結尾
			System.out.println("currentPosition值:"+currentPosition);
			System.out.println("sheet.getLastRowNum()值:"+sheet.getLastRowNum());
			
			int i=0;;
			while(currentPosition<=sheet.getLastRowNum()) {
				//獲取當前行數
				int row=currentPosition;
				currentPosition++;
				//讀取當前行的數據
				String line=getLine2(sheet,row);
				System.out.println( line);
				i++;
			}
			
			currentPosition=0;
			//判斷是否還有sheet 
			while(currentSheet!=numOfSheet-1) {
				//得到下一張sheet
				sheet=workbook.getSheetAt(currentSheet+1);
				//獲取當前行數
				int row=currentPosition;
				//當前行數是否到達文件末尾
				if(currentPosition==sheet.getLastRowNum()) {
					//當前sheet只想下一張sheet
					currentSheet++;
					//重新設定當前行數爲0;
					currentPosition=0;
					String line= getLine2(sheet,row);
					System.out.println( line);
					continue;
				}else{
					currentPosition++;
					//讀取當前行的數據
					String line= getLine2(sheet,row);
					System.out.println( line);
				}
			}
			
			if(i!=0) {
				brb.setCode("200");
				brb.setMsg("執行成功");
			}else {
			brb.setCode("100");
			brb.setMsg("未執行數據檢查");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return JSONObject.toJSONString(brb);
	}
	
	
	//函數getLine返回sheet的一行數據
	public  static String getLine2(HSSFSheet sheet,int row) {
		//創建字符緩衝區
		StringBuffer buffer=new StringBuffer();
		StringBuffer error=new StringBuffer();
		//根據行數取䮻sheet的一行數據
		HSSFRow rowline=sheet.getRow(row);
		//獲得當前行的列數
		//int filedColumns=rowline.getLastCellNum();
		HSSFCell cell;
		
		//此處 i<列數,可以寫成i

 

 

 

 

 

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