jxl 생성 excel

4297 단어 F#Excel
/**
 * 
 */
package com.automic.monitor.dailyWork.toExcel.util;

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

import jxl.Workbook;
import jxl.write.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.automic.monitor.dailyWork.toExcel.pvo.FileVO;

/**
 * @author hxb
 *
 */
public class ExcelUtil {

	private WritableWorkbook book;
	private WritableSheet sheet;
	
	/**
	 * @return ExcelUtil 
	 */
	public static ExcelUtil getInstance(){
		return new ExcelUtil();
	}
	/**
	 * @param savePath
	 * @return savePath 
	 */
	public ArrayList<FileVO> getFiles(String savePath){
		ArrayList<FileVO> vos = new ArrayList<FileVO>(0);
		File f = new File(savePath);
		File[] fs = f.listFiles();
		for(int i = 0;fs != null && i < fs.length;i++){
			File temp = fs[i];
			FileVO vo = new FileVO();
			vo.setName(temp.getName());
			vo.setPath(savePath + vo.getName());
			
			vos.add(vo);
		}
		return vos;
	}
	/**
	 * @param savePath
	 * @param fileName
	 * @return ,true: 、false: 
	 */
	public boolean isExist(String savePath,String fileName){
		File f = new File(savePath);
		File[] fs = f.listFiles();
		for(int i = 0;fs != null && i < fs.length;i++){
			File ftemp = fs[i];
			String nameTemp = ftemp.getName();
			if(fileName != null && fileName.equals(nameTemp)){
				return true;
			}
		}		
		return false;
	}
	
	/**
	 * @param dir
	 *  excel sheet
	 * @throws IOException
	 * @return :false: 、true:  
	 */
	public boolean createBookSheet(String saveFile,String sheetName,int sheetNo) throws IOException{
		if(this.book == null){
			this.book = Workbook.createWorkbook(new File(saveFile));
		}
		if(this.sheet == null || this.book.getSheet(sheetNo) == null){
			this.sheet = this.book.createSheet(sheetName, sheetNo);
			return true;
		}
		return false;
	}
	/**
	 * @param header
	 * @throws WriteException 
	 * @return excel 
	 */
	@SuppressWarnings("deprecation")
	public int buildHeader(ArrayList<String> header) throws WriteException{
		if(header == null || header.size() < 1){
			return 0;
		}
		if(this.book == null || this.sheet == null){
			return 0;
		}
		for(int i = 0;i < header.size();i++){
			String content = header.get(i);
			WritableCell cell =  new Label(i,0,content);
			WritableCellFormat format = new WritableCellFormat();
			format.setBackground(Colour.GRAY_25);
			cell.setCellFormat(format);
			this.sheet.addCell(cell);
		}
		return 0;
	}
	/**
	 * @param oobj
	 *  excel 
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	public boolean buildContent(ArrayList<ArrayList<Object>> oobj) throws RowsExceededException, WriteException{
		if(this.book == null || this.sheet == null){
			return false;
		}
		for(int i = 0;oobj != null && i < oobj.size();i++){// 
			int row = i + 1;
			ArrayList<Object> obj = oobj.get(i);
			for(int k = 0;obj != null && k < obj.size();k++){// 
				Object o = obj.get(k);
				WritableCell cell =  new Label(k,row,(o == null ? "" : o.toString()));
				this.sheet.addCell(cell);
			}
		}
		
		return true;
	}
	/**
	 *  
	 * @throws IOException 
	 * @throws WriteException 
	 */
	public void closed() throws IOException, WriteException{
		if(this.book != null){
			this.book.write();
			this.book.close();
		}
	}
}

좋은 웹페이지 즐겨찾기