Servlet 生成excel 并下载JXL方式

生成Excel并下载 JXL方式(去除html标签)

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		OutputStream os = response.getOutputStream();// 取得输出流
		response.reset();// 清空输出流

		//不能用用中文设置 filename,会出错
		response.setHeader("Content-disposition", "attachment; filename=stu.xls");// 设定输出文件头
		response.setContentType("application/msexcel");// 定义输出类型

		List<SdProduct> list = new ArrayList<SdProduct>();// 这个是从数据库中取得要导出的数据
		SdProduct sdp = new SdProduct();
		sdp.setSdTitle("<div>供应瑞丽女装服装代理,原单,外贸日韩女裤打底裤<div>");
		sdp.setSdConName("周清香女士");
		sdp.setSdMobile("123456789");
		sdp.setSdTel("123456789");
		sdp.setSdComment("<div><div><palign='left'><imgalt='' src='http://www.gzlaike.com/word/laike01.jpg'style='VISIBILITY:visible'/></p><palign='left'><spanstyle='FONT-SIZE:xx-large'><spanstyle='COLOR:#ff0000'>秋冬季节不管是做内衣打底,还是外穿,与靴鞋是最佳搭配,莱可制衣的精心做工,精梳全棉密根面料,非常舒适贴身,弹性也很好,拉伸无&ldquo;漏网&rdquo;原批发价24元,现特价16元,市场上10元打底无法与之相提并论,请勿议价!!</span></span></p><div>【编号】T314</div><div>【质地】纯棉,有弹力</div><div>【颜色】黑色,紫色,灰色,蓝色,咖啡</div><div>【标牌】全新包装带吊牌领标水洗标</div><div>【尺码】均码</div><div>【重量】0.15KG</div><div>【尺寸参照】腰围:52-88CM全长:89CM手工平铺测量有弹力");
		list.add(sdp);
		try {
			// 使用WritableCellFormat
			// 的setWrap(true)可以设成自动换行,然后再用WritableSheet的setRowView设置行的高度,setColumnView设置列的宽度
			//WritableWorkbook wbook = Workbook.createWorkbook(new File(request.getRealPath(path)+ "\\" + fileName)); // 建立excel文件
			WritableWorkbook wbook = null;
			wbook = Workbook.createWorkbook(os);
			WritableSheet wsheet = wbook.createSheet("工作表名称", 0); // 工作表名称
			// 设置Excel字体
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 14,
					WritableFont.BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.RED);
			WritableFont font = new WritableFont(WritableFont.ARIAL, 12,
					WritableFont.BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			WritableCellFormat nameFormat = new WritableCellFormat(wfont);
			WritableCellFormat titleFormat = new WritableCellFormat(font);
			String[] title = { "标 题", "联系人", "座 机", "手 机", "简 介" };
			// 设置Excel标题头
			wsheet.mergeCells(0, 0, 5, 0); // 合并单元格
			Label excelTitle1 = new Label(0, 0, "XXXXX:关键词—服装 的供应信息", nameFormat);
			wsheet.addCell(excelTitle1);

			// 设置Excel表头
			for (int i = 0; i < title.length; i++) {
				Label excelTitle = new Label(i, 1, title[i], titleFormat);
				wsheet.addCell(excelTitle);
			}
			int c = 2; // 用于循环时Excel的行号
			Iterator it = list.iterator();
			while (it.hasNext()) {
				SdProduct sd = (SdProduct) it.next();
				Label content1 = new Label(0, c, sd.getSdTitle().replaceAll(
						"<[^>]*>", ""));// 去掉HTML的所有标签
				Label content2 = new Label(1, c, sd.getSdConName());
				Label content3 = new Label(2, c, sd.getSdMobile());
				Label content4 = new Label(3, c, sd.getSdTel());
				String comment = sd.getSdComment().replaceAll("<[^>]*>", "");
				Label content5 = new Label(4, c, comment.replaceAll("&nbsp;",
						""));
				wsheet.setColumnView(0, 40);
				wsheet.setColumnView(1, 12);
				wsheet.setColumnView(2, 13);
				wsheet.setColumnView(3, 14);
				wsheet.setColumnView(4, 50);
				wsheet.addCell(content1);
				wsheet.addCell(content2);
				wsheet.addCell(content3);
				wsheet.addCell(content4);
				wsheet.addCell(content5);
				c++;
			}

			wbook.write(); // 写入文件
			wbook.close();
		} catch (Exception e) {
			e.printStackTrace();

		}

	}

 

你可能感兴趣的:(C++,c,servlet,Excel,C#)