java jxl 해석 excel

5270 단어
자바 JXL 해석 엑셀
package me.tspace.exceldeal;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelDeal {
	
	/**
	 *             
	 * @param s    
	 * @param val       
	 * @return
	 */
	public String toToken(String s, String val) {
		if (s == null || s.trim().equals("")) {
			return s;
		}
		if (val == null || val.equals("")) {
			return s;
		}
		StringBuffer stringBuffer = new StringBuffer();
		String[] result = s.split(val);
		 for (int x=0; x<result.length; x++){
			 stringBuffer.append(" ").append(result[x]);
		 }
		return stringBuffer.toString();
		
	}
	
	public String excelCharaterDeal(String str){
		String[] val = {"-","_","/"};//      
		for(String i:val){
			str=this.toToken(str, i);
		}
		return str;
	}
	

	/**  Excel     
	 * @param file        
	 * @return
	 */
	public  String readExcel(File file){
		StringBuffer stringBuffer = new StringBuffer();
		Workbook workBook = null;
		try {
			//  Workbook(   )  
			workBook=Workbook.getWorkbook(file);
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(workBook==null)
			return null;
		
		//   Workbook    ,        Sheet(   )   
		Sheet[] sheet = workBook.getSheets();
		
		if(sheet!=null&&sheet.length>0){
			//          
			for(int i=0;i<sheet.length;i++){
				//          
				int rowNum = sheet[i].getRows();
				for(int j=0;j<rowNum;j++){
					//           
					Cell[] cells = sheet[i].getRow(j);
					if(cells!=null&&cells.length>0){
						//          
						for(int k=0;k<cells.length;k++){
							//         
							String cellValue = cells[k].getContents();
							//      
							cellValue=this.excelCharaterDeal(cellValue);
							stringBuffer.append(cellValue+"\t");
						}
					}
					stringBuffer.append("\r
"); } stringBuffer.append("\r
"); } } // , workBook.close(); return stringBuffer.toString(); } /** Excel * @param fileName Excel */ public static void writeExcel(String fileName){ WritableWorkbook wwb = null; try { // Workbook (Workbook) wwb = Workbook.createWorkbook(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } if(wwb!=null){ // //Workbook createSheet , , WritableSheet ws = wwb.createSheet("sheet1", 0); // for(int i=0;i<10;i++){ for(int j=0;j<5;j++){ // , Excel , , Label labelC = new Label(j, i, " "+(i+1)+" , "+(j+1)+" "); try { // ws.addCell(labelC); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } try { // wwb.write(); // , wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } /** * @param file * @param keyWord * @return */ public static boolean searchKeyWord(File file,String keyWord){ boolean res = false; Workbook wb = null; try { // Workbook( ) wb=Workbook.getWorkbook(file); } catch (BiffException e) { return res; } catch (IOException e) { return res; } if(wb==null) return res; // Workbook , Sheet( ) Sheet[] sheet = wb.getSheets(); boolean breakSheet = false; if(sheet!=null&&sheet.length>0){ // for(int i=0;i<sheet.length;i++){ if(breakSheet) break; // int rowNum = sheet[i].getRows(); boolean breakRow = false; for(int j=0;j<rowNum;j++){ if(breakRow) break; // Cell[] cells = sheet[i].getRow(j); if(cells!=null&&cells.length>0){ boolean breakCell = false; // for(int k=0;k<cells.length;k++){ if(breakCell) break; // String cellValue = cells[k].getContents(); if(cellValue==null) continue; if(cellValue.contains(keyWord)){ res = true; breakCell = true; breakRow = true; breakSheet = true; } } } } } } // , wb.close(); return res; } /** Excel * @param dataSheet * @param col * @param row * @param width * @param height * @param imgFile */ public static void insertImg(WritableSheet dataSheet, int col, int row, int width, int height, File imgFile){ WritableImage img = new WritableImage(col, row, width, height, imgFile); dataSheet.addImage(img); } }

좋은 웹페이지 즐겨찾기