Java:创建和下载excel文件

public class ExcelUtil {

    // 标签代号信息  
	private String[] fTags = null;  

    // 对象属性信息  
	private String[] fNames = null; 

    // 标签缓存  
	private Hashtable allTags = null; 

	public ExcelUtil() {

	}

	public ExcelUtil(String[] fTags, String[] fNames, Hashtable allTags) {
		this.fTags = fTags;
		this.fNames = fNames;
		this.allTags = allTags;
	}

	/**
	 * 生成Excel文件
	 *
	 * @param path
	 * @param dataList
	 * @return
	 */
	public String createExcel(String path, List dataList) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		// deleteAllFile(file);  
		file = new File(file, System.currentTimeMillis() + ".xls");
		WritableWorkbook book = null;
		try {
			book = Workbook.createWorkbook(file);
			WritableSheet sheet = book.createSheet(file.getName(), 0);
            // 设置Excel标题信息  
			this.setHeader(sheet);
            // 设置Excel内容主体信息  
			this.setBody(sheet, dataList); 
			book.write();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				book.close();
			} catch (WriteException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file.getAbsolutePath();
	}

	/**
	 * 清空file下的所有子文件
	 *
	 * @param file
	 */
	public void deleteAllFile(File file) {
		try {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				files[i].delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 设置Excel标题信息
	 *
	 * @param sheet
	 * @throws WriteException
	 */
	public void setHeader(WritableSheet sheet) throws WriteException {
		String[] header = new String[fTags.length];
		for (int i = 0; i < fTags.length; i++) {
			String fTagsName = (String) allTags.get("F_" + fTags[i].toUpperCase());
			header[i] = fTagsName != null ? fTagsName : fTags[i];
		}
		this.setHeader(sheet, header);
	}

	/**
	 * 设置Excel标题信息
	 *
	 * @param sheet
	 * @param column
	 * @throws WriteException
	 */
	public void setHeader(WritableSheet sheet, String[] column) throws WriteException {
		WritableCellFormat headerFormat = new WritableCellFormat();
        // 水平居中对齐  
		headerFormat.setAlignment(Alignment.CENTRE);  
        // 竖直方向居中对齐  
		headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE); 
		headerFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
		for (int i = 0; i < column.length; i++) {
			Label label = new Label(i, 0, column[i], headerFormat);
			sheet.addCell(label);
			sheet.setColumnView(i, 20);
			sheet.setRowView(0, 500);
		}
	}

	/**
	 * 设置Excel内容主体信息
	 *
	 * @param sheet
	 * @param rowList
	 * @throws Exception
	 */
	public void setBody(WritableSheet sheet, List rowList) throws Exception {
		this.setBody(sheet, rowList, fNames);
	}

	/**
	 * 设置Excel内容主体信息
	 *
	 * @param sheet
	 * @param rowList
	 * @param column
	 * @throws Exception
	 */
	public void setBody(WritableSheet sheet, List rowList, String[] column) throws Exception {
		WritableCellFormat bodyFormat = new WritableCellFormat();
        // 水平居中对齐  
		bodyFormat.setAlignment(Alignment.CENTRE);
        // 竖直方向居中对齐  
		bodyFormat.setVerticalAlignment(VerticalAlignment.CENTRE); 
		bodyFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
		Object obj = null;
		Label label = null;
		for (int i = 0; i < rowList.size(); i++) {
			obj = rowList.get(i);
			for (int j = 0; j < column.length; j++) {
				if (obj instanceof Map) {
					label = new Label(j, i + 1, String.valueOf(((Map) obj).get(column[j].toLowerCase())), bodyFormat);
				} else {
					label = new Label(j, i + 1, BeanUtils.getProperty(obj, column[j]), bodyFormat);
				}
				sheet.addCell(label);
				sheet.setRowView(i + 1, 350);
			}
		}
	}


	/**
	 * 文件下载
	 *
	 * @param response
	 * @param filePath 文件路径
	 * @param fileName 文件名称
	 */
	public void download(HttpServletResponse response, String filePath, String fileName) throws IOException {
		FileInputStream fis = null;
		OutputStream os = null;
		try {
			fis = new FileInputStream(filePath);
            // 取得输出流  
			os = response.getOutputStream();
            // 清空输出流  
			response.reset();
            // 设定输出文件头  
			response.setHeader("Content-disposition", "attachment; filename=" + fileName);
			response.setContentType("application/x-download");
			byte[] mybyte = new byte[8192];
			int len = 0;
			while ((len = fis.read(mybyte)) != -1) {
				os.write(mybyte, 0, len);
			}
			os.close();
		} catch (IOException e) {
			throw e;
		}
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值