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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值