jxl生成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();
		}
	}
}

 

你可能感兴趣的:(F#,Excel)