数据导出到excel--java

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/** 
 * @author zZz 
 * @date 2017年2月14日 下午4:32:37 
 * @version 1.0 
 */

public class ExportExcel {

	/** 
	 * 导出excel 文件  带标题 
	 * @param fileName  
	 * @param titleList 
	 * @param list 
	 * @param response 
	 * @throws Exception 
	 */  
	public static void exportWithHeadExcel(String fileName, String[] titleList,  
			List<?> list, HttpServletResponse response) throws Exception {  
		SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");  
		String todayStr = df.format(new Date());  
		Date now = new Date();  
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");  
		String today = dateformat.format(now);  
		OutputStream os = null;  
		try {
			os = response.getOutputStream();
			response.reset();//清空输出流
			String localFileName = fileName; 
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题  
			fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题  
			response.setContentType("application/vnd.ms-excel;");  
			response.setHeader("Content-disposition", "attachment; filename=\""+ fileName + "_" + todayStr + ".xls\"");  
			// 开始写入excel  
			// 加标题  
			// 标题字体  
			jxl.write.WritableFont wfc = new jxl.write.WritableFont(  
					WritableFont.COURIER, 18, WritableFont.NO_BOLD, false);  
			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(  
					wfc);  
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE);  
			wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);  
			// 字段字体  
			jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(  
					WritableFont.COURIER, 10, WritableFont.NO_BOLD, false);  
			jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(  
					wfc1);  
			wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);  
			wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);  
			// 结果字体  
			jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();  
			wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);  
			wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);  
			WritableWorkbook wbook = Workbook.createWorkbook(os);  
			// 写sheet名称  
			WritableSheet wsheet = wbook.createSheet(localFileName, 0);  
			int i = 2;  
			for (int m = 0; m < titleList.length; m++) {  
				wsheet.setColumnView(m, 30);  
			}  
			// 加入字段名  
			for (int n = 0; n < titleList.length; n++) {  
				wsheet.addCell(new jxl.write.Label(n, 3, titleList[n], wcfFC1));  
			}  
			// 加入标题  
			wsheet.mergeCells(0, 0, i - 1, 0);  
			wsheet.addCell(new Label(0, 0, localFileName, wcfFC));  
			// 加入打印时间  
			wsheet.addCell(new Label(i - 2, 1, "打印日期:"));  
			wsheet.addCell(new Label(i - 1, 1, today));  
			// 写入流中  
			int row = 0;  
			for (int r = 0; r < list.size(); r++) {  
				Object[] obj = (Object[]) list.get(r);  
				for (int x = 0; x < titleList.length; x++) { 
					if(obj[x]!=null&&obj[x].toString().matches("^[0-9]+\\.{1}([0-9]{2})?$")){
				        jxl.write.NumberFormat nf = new jxl.write.NumberFormat("0.00"); 
				        jxl.write.WritableCellFormat wcf2 = new jxl.write.WritableCellFormat(nf); 
						wsheet.addCell(new jxl.write.Number(x, row + 4,Double.valueOf(obj[x].toString()), wcf2));
					}else
						wsheet.addCell(new jxl.write.Label(x, row + 4,obj[x] == null ? " " : obj[x].toString(), wcfFC1));  
				}  
				row++;  
				if (row % 60000 == 0) {  
					row = 0;  
					// 写sheet名称  
					wsheet = wbook.createSheet(localFileName, 0);  
					i = 2;  
					for (int m = 0; m < titleList.length; m++) {  
						wsheet.setColumnView(m, 30);  
					}  
					// 加入字段名  
					for (int n = 0; n < titleList.length; n++) {  
						wsheet.addCell(new jxl.write.Label(n, 3, titleList[n],wcfFC1));  
					}  
					// 加入标题  
					wsheet.mergeCells(0, 0, i - 1, 0);  
					wsheet.addCell(new Label(0, 0, localFileName, wcfFC));  
					// 加入打印时间  
					wsheet.addCell(new Label(i - 2, 1, "打印日期:"));  
					wsheet.addCell(new Label(i - 1, 1, today));  
				}  
			}  
			wbook.write();
			wbook.close();
		} catch (Exception e) {  
			e.printStackTrace();
			throw e;
		} finally {  
			if (os == null) {  
				// Log.info("os is null");  
			} else {  
				try {
					os.close();  
					os = null;  
				} catch (IOException e) {  
					e.printStackTrace();  
				}  
			}  
		}  
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值