<%@ page language="java"
	import="java.util.*,java.io.*,java.sql.*,java.net.*,javax.servlet.*,javax.servlet.http.*,java.text.SimpleDateFormat,java.util.Date"
	pageEncoding="gb2312"%>
<%
if (request.getParameter("action") != null) {
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
	String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
			+ this.getServletContext().getRealPath("demodata/") + "\\demo.mdb";
	Connection conn = DriverManager.getConnection(strUrl);
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("select Max(ID) from excel");
	int newID = 1;
	if (rs.next()) {
		String id = rs.getString(1);
		if (id != null && id.length() > 0) {
			newID = Integer.parseInt(id) + 1;
		}
	}
	rs.close();

	String fileName = "bbcc" + newID + ".xls";
	String FileSubject = "请输入文档主题";
	String getFile = (String) request.getParameter("FileSubject");
	if (getFile != null && getFile.length() > 0)
		FileSubject = new String(getFile.getBytes("iso-8859-1"));
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	// new Date()为获取当前系统时间
	String strsql = "Insert into excel(ID,FileName,Subject,SubmitTime) values("
			+ newID + ",'" + fileName + "','" + FileSubject + "','" + df.format(new Date()) + "')";
	stmt.executeUpdate(strsql);
	stmt.close();
	conn.close();

	//拷贝文件
	String oldPath = getServletContext().getRealPath("doc/" + request.getParameter("TemplateName"));
	String newPath = getServletContext().getRealPath("doc/" + fileName);
	try {
		int bytesum = 0;
		int byteread = 0;
		File oldfile = new File(oldPath);
		if (oldfile.exists()) { //文件存在时 
			InputStream inStream = new FileInputStream(oldPath); //读入原文件 
			FileOutputStream fs = new FileOutputStream(newPath);
			byte[] buffer = new byte[1024];
			int length;
			while ((byteread = inStream.read(buffer)) != -1) {
				bytesum += byteread; //字节数 文件大小 
				fs.write(buffer, 0, byteread);
			}
			inStream.close();
		}
	} catch (Exception e) {
		System.out.println("复制单个文件操作出错");
		e.printStackTrace();
	}

	response.sendRedirect("excel.jsp?id=" + newID);
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="css/style.css" type="text/css"></link>
		<!--[if lte IE 6]>
<script type="text/javascript" src="js/belatedPNG.js"></script>
<script type="text/javascript">
  DD_belatedPNG.fix('.png_bg,.png_bg a:hover,img,li');
</script>
<![endif]-->
		<title>在线演示示例</title>
	</head>
	<body>
		<script type="text/javascript">
	function onColor(td) {
		td.style.backgroundColor = '#D7FFEE';
	}
	function offColor(td) {
		td.style.backgroundColor = '';
	}
	function openHtml(filename) {
		window.open("doc/" + filename);
	}
</script>
		<!--header-->
		<div class="zz-headBox br-5 clearfix">
			<div class="zz-head mc">
				<!--logo-->
				<div class="logo fl">
					<a href="#"><img src="images/logo.png" alt="" /> </a>
				</div>
				<!--logo end-->
				<ul class="head-rightUl fr">
					<li>
						<a href="http://www.zhuozhengsoft.com" target="_blank">卓正网站</a>
					</li>
					<li>
						<a href="http://www.zhuozhengsoft.com/poask/index.asp" target="_blank">客户问吧</a>
					</li>
					<li class="bor-0">
						<a href="http://www.zhuozhengsoft.com/contact-us.html" target="_blank">联系我们</a>
					</li>
				</ul>
			</div>
		</div>
		<!--header end-->


		<!--content-->
		<div class="zz-content mc clearfix pd-28">
			<div class="demo mc">
				<h2 class="fs-16">
					PageOffice组件 在Excel在线编辑中的应用
				</h2>
				<h3 class="fs-12">
					演示说明:
				</h3>

				<p>
					提示1：正确运行本演示前，您首先要确定您的机器上已安装Microsoft Office。
				</p>
				<p>
					提示2：如果您是第一次使用PageOffice，在打开文件的时候会提示安装控件，请点允许安装。
				</p>
			</div>
			<div class="demo mc">
				<h3 class="fs-12">
					新建文件
				</h3>
				<form name="form1" id="form1" action="index.jsp?action=copy" method="post">
					<table class="text" cellSpacing="0" cellPadding="0" border="0">
						<tr>
							<td style="font-size: 9pt" align="left">
								<select name="TemplateName">
									<option value='redhead00.xls'>
										------不使用模板------
									</option>
									<option value='redhead01.xls' selected>
										公司销售情况月报模板
									</option>
								</select>
							</td>
							<td align="center">
								<input name="FileSubject" type="text" class="boder"
									style="width: 180px;" value="请输入文档主题" />
							</td>
							<td width="221">
								&nbsp;
								<input type="image" name="ImageButton1" id="ImageButton1"
									src="images/newword.gif" alt="" border="0" />
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="zz-talbeBox mc">
				<h2 class="fs-12">
					文档列表
				</h2>
				<table class="zz-talbe">
					<thead>
						<tr>
							<th width="7%">
								类型
							</th>
							<th width="32%">
								文档名称
							</th>
							<th width="19%">
								创建日期
							</th>
							<th width="30%">
								操作模式
							</th>
							<th width="8%">
								&nbsp;
							</th>
						</tr>
					</thead>
					<tbody>
						<%
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
							String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
											+ this.getServletContext().getRealPath("demodata/") + "\\demo.mdb";
							Connection conn = DriverManager.getConnection(strUrl);
							Statement stmt = conn.createStatement();
							ResultSet rs = stmt.executeQuery("select * from  excel order by ID DESC ");

							while (rs.next()) {
								String str0 = rs.getString(1);
								if (str0 != null && str0.length() > 0) {
						%>
						<tr onmouseover='onColor(this);' onmouseout='offColor(this);'>
							<td>
								<img src='images/office-2.jpg' />
							</td>
							<td><%=rs.getString(3)%></td>
							<%
								//显示时间
										String str4 = rs.getString(4);
										if (str4 != null && str4.length() > 0) {
							%>
							<td><%=new SimpleDateFormat("yyyy/MM/dd")
								.format(new SimpleDateFormat("yyyy-MM-dd")
										.parse(str4))%></td>
							<%
								} else {
							%>
							<td>
								&nbsp;
							</td>
							<%
								}		
							%>
							<td>
								<a href='excel.jsp?id=<%=str0 %>'>在线编辑</a> <a href='excel2.jsp?id=<%=str0 %>'>只读打开</a>
							</td>
							<%
								}
							%>
							<td>
								&nbsp;
							</td>
						</tr>
						<%
							}
							rs.close();
							stmt.close();
							conn.close();
						%>
					</tbody>
				</table>
			</div>
		</div>
		<!--content end-->
		<!--footer-->
		<div class="login-footer clearfix">
			Copyright copy 2013 北京卓正志远软件有限公司
		</div>
		<!--footer end-->
	</body>
</html>
