poi之WEB生成xls

	String str = request.getParameter("str");
		str = URLDecoder.decode(str, "utf-8");
		String[] arr = str.split(",");
		try {
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet("第一个xls");
			
			
			HSSFCellStyle cellStyle = wb.createCellStyle();     //字体用的风格
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //左右居中    
			cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //上下居中 
			cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 
			cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框     
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框     
			cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //上边框 
			cellStyle.setWrapText(true);
			HSSFFont cellFont5 = wb.createFont();
			cellFont5.setFontHeightInPoints((short) 10); //字号
			cellFont5.setFontName("宋体");      //设置为宋体
			cellStyle.setFont(cellFont5);
			
			
			
			HSSFRow rowi = sheet.createRow(0);   //第一行
			rowi.setHeight((short)800);
			HSSFCell celli = rowi.createCell((short) 0);
	        celli.setEncoding(HSSFCell.ENCODING_UTF_16);
	        HSSFCell cellmy = null;
	        for (int i = 0; i < 4; i++) {
				cellmy = rowi.createCell((short) i);
				cellmy.setCellStyle(cellStyle);
			}
	        celli.setCellValue("生成Excel文件");
	        celli.setCellStyle(cellStyle);
	        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short)4));
			
			
			HSSFRow row = sheet.createRow(1); // 第一行标题

			String[] titles = { "姓名", "年龄", "QQ", "Email" };
			HSSFCell cell = null;
			for (short i = 0; i < titles.length; i++) {
				cell = row.createCell(i);
				cell.setCellValue(titles[i]);
			}

			row = sheet.createRow(2); // 第二行
			for (short i = 0; i < arr.length; i++) {
				cell = row.createCell(i);
				cell.setCellValue(arr[i]);
			}

			SimpleDateFormat timeformat = new SimpleDateFormat("yyyyMMddHHmmss");
			java.util.Date date = Calendar.getInstance().getTime();
			String createdate = timeformat.format(date).toString();

			int location = (int) (Math.random() * 100); // 100中取出一个

			String fileDirectory = request.getRealPath("/") + "/xls";
			FileOutputStream fileOut = new FileOutputStream(fileDirectory
					+ File.separator + createdate + "" + location + ".xls");
			wb.write(fileOut);
			fileOut.flush();
			fileOut.close();
			String fileName = fileDirectory + File.separator + createdate + ""
					+ location + ".xls";
			// System.out.println("文件名" + fileName);

			// 新建一个SmartUpload对象

			SmartUpload su = new SmartUpload();

			su.initialize(this.getServletConfig(), request, response);

			su.downloadFile(fileName, "application/vnd.ms-excel");// 用浏览器打开

		} catch (Exception e) {
			e.printStackTrace();

		}

<%@ page language="java" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>


		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<meta http-equiv="content-type" content="text/html; charset=gbk">
		<script type="text/javascript">
		
	function  generateExcel(e){

		// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}


		var str="";
		var ele=null;
		var tr=null;
		if(document.all){
			ele=event.srcElement;
			tr=ele.parentElement.parentElement;
		}else{
			ele=e.target;
			tr=ele.parentNode.parentNode;
		}



		 
		var len=tr.childNodes.length;
		//alert(len);
		for(var i=0;i<len-1;i++){
			var child=tr.childNodes[i];
			if(document.all){
			str+=child.innerText.Trim()+",";
			}else{
			    if(child.tagName){
				    if(child.textContent!=null){
						str+=child.textContent.Trim()+",";
					}
					//alert(child.textContent);
				}
			}
		}
		//alert(str);
		str=str.substr(0,str.length-1);
		 
		//alert(str);
		
		str=encodeURI(str);
		str=encodeURI(str);
		
		document.location="makeExcel?str="+str;
	}
		</script>
	</head>

	<body>
		<table border="1" cellspacing="0" cellpadding="6">
			<tr>
				<td>
					姓名
				</td>
				<td>
					年龄
				</td>
				<td>
					QQ
				</td>
				<td>
					Email
				</td>
				<td>
					操作
				</td>
			</tr>
			<tr>
				<td>
					张三
				</td>
				<td>
					18
				</td>
				<td>
					359951165
				</td>
				<td>
					[email protected]
				</td>
				<td>
					<input type="button" value="生成excel文件"
						onClick="generateExcel(event)"></input>
				</td>
			</tr>
			<tr>
				<td>
					李四
				</td>
				<td>
					21
				</td>
				<td>
					2221311
				</td>
				<td>
					[email protected]
				</td>
				<td>
					<input type="button" value="生成excel文件"
						onClick="generateExcel(event)"></input>
				</td>
			</tr>
			<tr>
				<td>
					李四光
				</td>
				<td>
					100
				</td>
				<td>
					1234567
				</td>
				<td>
					[email protected]
				</td>
				<td>
					<input type="button" value="生成excel文件"
						onClick="generateExcel(event)"></input>
				</td>
			</tr>
			<tr>
				<td>
					小飞
				</td>
				<td>
					32
				</td>
				<td>
					2322223
				</td>
				<td>
					[email protected]
				</td>
				<td>
					<input type="button" value="生成excel文件"
						onClick="generateExcel(event)"></input>
				</td>
			</tr>
			<tr>
				<td>
					叶无道
				</td>
				<td>
					20
				</td>
				<td>
					7654321
				</td>
				<td>
					[email protected]
				</td>
				<td>
					<input type="button" value="生成excel文件"
						onClick="generateExcel(event)"></input>
				</td>
			</tr>

		</table>
	</body>
</html>

你可能感兴趣的:(jsp,Web,qq,Excel,idea)