Jxlを使用してExcelファイルの内容を読み込む

4259 ワード

package com.xhgjky.ssh.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class JxlReadExcel {
	/**
	 * JXL  Excel
	 * @return 
	 * */
	public static List<String> main(String path) {
		// TODO Auto-generated method stub
		List<String> cx = new ArrayList<String>();
		List<String> flag = new ArrayList<String>();
		String s = null;
		int h;
		int l;
		try {
			//  workbook
//			Workbook workbook = Workbook.getWorkbook(new File("C:/Users/Administrator/Desktop/poi_test.xls"));
			Workbook workbook = Workbook.getWorkbook(new File(path));
			//     sheet
			Sheet sheet = workbook.getSheet(0);
			//    
			boolean found = false; 
			for (int i = 1; i < sheet.getRows() && !found; i++) {
				for (int j = 0; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(j, i);
					//         ,        
					if(i > 1){
						h = i + 1;
						l = j + 1;
						if(j == 0){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;
								break;
							}else {
								boolean f = (cell.getContents().trim()).matches("^[A-Za-z0-9]+$");
								if(f == false){
									s = " "+h+" 、 "+l+"     ,        !";
									found = true;
									break;
								}
							}
						}else if(j == 1){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}
						}else if(j == 2){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}else {
								boolean f = (cell.getContents().trim()).matches("^[A-Za-z0-9]+$");
								if(f == false){
									s = " "+h+" 、 "+l+"     ,        !";
									found = true;  
									break;
								}
							}
						}else if(j == 3){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}else {
								if(!("    ").equals(cell.getContents().trim())&&!("    ").equals(cell.getContents().trim())){
									s = " "+h+" 、 "+l+"     ,        !";
									found = true;  
									break;
								}
							}
						}
						else if(j == 4){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}
						}
						else if(j == 5){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}
						}else if(j == 6){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}
						}else if(j == 7){
							if(cell.getContents().trim() == ""||"".equals(cell.getContents().trim())){
								s = " "+h+" 、 "+l+"     ,        !";
								found = true;  
								break;
							}else {
								if(!(cell.getContents().trim()).matches("\\d*")){
									s = " "+h+" 、 "+l+"     ,        !";
									found = true;  
									break;
								}
							}
						}
					}
//					System.out.print(cell.getContents()+"  ");
					cx.add(cell.getContents());
				}
//				System.out.println();
			}
			workbook.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(s == null){
			return cx;
		}else {
			flag.add("err");
			flag.add(s);
			return flag;
		}
	}

}