excel 生成与解析工具类

public class CvsUtils {

	/**
	 * function 生成账务明细报表
	 * 
	 * @param titles
	 * @param path
	 * @param accountDetailList
	 */
	public static boolean createAccountDetailExcel(String[] titles,
			String path, List<AccountDetail> accountDetailList) {

		boolean flag = true;

		try {

			WritableWorkbook wbook = Workbook
					.createWorkbook(new FileOutputStream(path)); // 建立excel文件

			WritableSheet wsheet = wbook.createSheet(Constants.CVS_SHEET_NAME,
					0); // 工作表名称

			// 设置Excel字体
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
					WritableFont.BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			WritableCellFormat titleFormat = new WritableCellFormat(wfont);

			// 设置Excel表头
			for (int i = 0; i < titles.length; i++) {
				Label excelTitle = new Label(i, 0, titles[i], titleFormat);
				wsheet.addCell(excelTitle);
			}
			int c = 1; // 用于循环时Excel的行号
			Iterator it = accountDetailList.iterator();
			while (it.hasNext()) {
				AccountDetail accountDetail = (AccountDetail) it.next();
				Label content1 = new Label(0, c, accountDetail.getPaymentId());
				Label content2 = new Label(1, c, accountDetail.getPayTime()
						.toString());
				Label content3 = new Label(2, c, accountDetail
						.getTransactionType().toString());
				Label content4 = new Label(3, c, accountDetail.getPayer());
				Label content5 = new Label(4, c, accountDetail.getIncome()
						.toString());
				Label content6 = new Label(5, c, accountDetail.getExpenses()
						.toString());
				Label content7 = new Label(6, c, accountDetail.getPayment()
						.toString());

				wsheet.addCell(content1);
				wsheet.addCell(content2);
				wsheet.addCell(content3);
				wsheet.addCell(content4);
				wsheet.addCell(content5);
				wsheet.addCell(content6);
				wsheet.addCell(content7);
				c++;
			}
			wbook.write(); // 写入文件
			wbook.close();

		} catch (Exception e) {
			flag = false;
			System.out.println(e + "生成报表错误");
			e.printStackTrace();

		}

		return flag;

	}

	/**
	 * 导出为Excel
	 * 
	 * @param cdosCategoryKeyWords
	 * @return
	 * @throws Exception
	 */
	public static boolean exportExcel(HttpServletResponse response, String[] titles, List<AccountDetail> accountDetailList) {
		boolean flag = true;
		OutputStream os = null;
		try {
			os = response.getOutputStream();

			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String curDatetime = sdf.format(cal.getTime());
			response.reset();
			response.setHeader("Content-disposition",
					"attachment; filename=CategoryKeyWords" + curDatetime
							+ ".xls");
			response.setContentType("application/msexcel");

			WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件

			WritableSheet wsheet = wbook.createSheet(Constants.CVS_SHEET_NAME,
					0); // 工作表名称

			// 设置Excel字体
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
					WritableFont.BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			WritableCellFormat titleFormat = new WritableCellFormat(wfont);

			// 设置Excel表头
			for (int i = 0; i < titles.length; i++) {
				Label excelTitle = new Label(i, 0, titles[i], titleFormat);
				wsheet.addCell(excelTitle);
			}

			// 设置Excel表头
			for (int i = 0; i < titles.length; i++) {
				Label excelTitle = new Label(i, 0, titles[i], titleFormat);
				wsheet.addCell(excelTitle);
			}
			int c = 1; // 用于循环时Excel的行号
			Iterator<AccountDetail> it = accountDetailList.iterator();
			while (it.hasNext()) {
				AccountDetail accountDetail = (AccountDetail) it.next();
				Label content1 = new Label(0, c, accountDetail.getPaymentId());
				Label content2 = new Label(1, c, accountDetail.getPayTime()
						.toString());
				Label content3 = new Label(2, c, accountDetail
						.getTransactionType().toString());
				Label content4 = new Label(3, c, accountDetail.getPayer());
				Label content5 = new Label(4, c, accountDetail.getIncome()
						.toString());
				Label content6 = new Label(5, c, accountDetail.getExpenses()
						.toString());
				Label content7 = new Label(6, c, accountDetail.getPayment()
						.toString());

				wsheet.addCell(content1);
				wsheet.addCell(content2);
				wsheet.addCell(content3);
				wsheet.addCell(content4);
				wsheet.addCell(content5);
				wsheet.addCell(content6);
				wsheet.addCell(content7);
				c++;
			}
			wbook.write(); // 写入文件
			wbook.close();
			os.close();
		} catch (Exception e) {
			 e.printStackTrace();
			 flag=false;
			 
		}

		return flag;

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值