struts2- Excel 업로드 및 해석

2276 단어 struts
package com.bjpn.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
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.struts2.ServletActionContext;

/**
 *  excel ,  excel 
 * 
 * @author zhaoj
 * 
 */
public class ParseExcelAction {

	private File excel;//  name 
	private String excelFileName;//  name +FileName

	public File getExcel() {
		return excel;
	}

	public void setExcel(File excel) {
		this.excel = excel;
	}

	public String getExcelFileName() {
		return excelFileName;
	}

	public void setExcelFileName(String excelFileName) {
		this.excelFileName = excelFileName;
	}

	public String execute() throws IOException {

		//  
		String desPath = ServletActionContext.getServletContext().getRealPath(
				"/imags");
		File destFile = new File(desPath, excelFileName);
		FileUtils.copyFile(excel, destFile);

		//  excel
		//  excel 
		HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(destFile));
		//  
		HSSFSheet sheet = book.getSheetAt(0);
		//  
		for (int i = 0; i <= sheet.getLastRowNum(); i++) {
			//  
			HSSFRow row = sheet.getRow(i);
			for (int j = 0; j < row.getLastCellNum(); j++) {
				//  
				HSSFCell cell = row.getCell(j);
				System.out.print(cell + "\t");
			}
			System.out.println();
		}

		return "success";
	}
}









	$(function(){
	
		$("#submitBtn").click(function(){
			var fileName = $("#excel").val();
			var suffix = (fileName.substr(fileName.lastIndexOf("."))).toUpperCase();
			
			// 
			if(".XLS" != suffix && ".XLSL" == suffix){
				alert(" .XLS .XLSL");
				return;
			}
			
			$("#myForm").submit();
		
		});
	
	
	
	});





	

좋은 웹페이지 즐겨찾기