jsp生成html后转excel
jsp页面<%@ page contentType="application/msexcel;charset=utf-8"%><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><!-- 以上这行设定本网页为excel格式的网页 --><html><head><meta http-equiv="Content-Type" content="application/msexcel; charset=utf-8"><title>Excel档案呈现方式</title></head><body><table border="1" id="the-table" width="100%" ><thead><tr><th width="45px">应收帐款日期</th><th width="100px">客户名称</th><th width="100px">销售合同号</th><th width="100px">借方本币</th><th width="100px">贷方本币</th></tr></thead><tbody><c:forEach var="record" items="${pageinfo.records}" varStatus="i"><tr><td><c:out value="${record}" /></td><td><c:out value="${record}" /></td><td><c:out value="${record}" /></td><td><c:out value="${record}" /></td><td><c:out value="${record}" /></td></tr></c:forEach></tbody></table></body></html>
java代码 需要用到java2word
publicvoid htmlToExcel(String execlFile, String txtFile) {ActiveXComponent app = null;try {app = new ActiveXComponent("excel.Application");app.setProperty("Visible", new Variant(false)); // 设置execl不可见app.setProperty("DisplayAlerts", new Variant(false)); Object docs = app.getProperty("Workbooks").toDispatch();//打开文档Object doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { execlFile, new Variant(false), new Variant(true) }, new int).toDispatch(); // 打开execl文件//另存为excel 改变new Variant(1)数值可以改变另存文件的格式Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { txtFile, new Variant(1) }, new int); // 另存为txt文件//Dispatch.call(doc, "Close", new Variant(true));} catch (Exception e) {e.printStackTrace();} finally {if (app != null) {//关闭应用app.invoke("Quit", new Variant);}//释放线程ComThread.Release();}}publicvoid outputExcel(DispatchAction ac, HttpServletRequest request, HttpServletResponse response, String jspPath, String fileName) throws Exception {//以下生成文件ServletContext sc = ac.getServlet().getServletContext();File f = File.createTempFile("temp008", ".html");String pName = f.getAbsolutePath(); // 生成html的完整路径RequestDispatcher rd = sc.getRequestDispatcher(jspPath);final ByteArrayOutputStream os = new ByteArrayOutputStream();final ServletOutputStream stream = new ServletOutputStream() {public void write(byte[] data, int offset, int length) {os.write(data, offset, length);}public void write(int b) throws IOException {os.write(b);}};final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));HttpServletResponse rep = new HttpServletResponseWrapper(response) {public ServletOutputStream getOutputStream() {return stream;}public PrintWriter getWriter() {return pw;}};rd.include(request, rep);pw.flush();FileOutputStream fos = new FileOutputStream(pName); // 把jsp输出的内容写到d:\test.htm OutputStreamWriter ff=new OutputStreamWriter(fos, "UTF-8");//InputStreamReader in=new InputStreamReader(os);//os.writeTo(fos);byte[] zyf=os.toByteArray();String thetext=new String(zyf,"GB2312");ff.write(thetext);ff.close();os.close();String fileName2=CommonUtil.getStText("st.tempFile.path") + fileName + CommonUtil.getTodayYMD() + RandomStringUtils.randomNumeric(10) + ".xls";///以下输出File f4 = new File(ac.getServlet().getServletContext().getRealPath(fileName2));String gg = f4.getAbsolutePath();htmlToExcel(pName, gg);//response.sendRedirect(request.getContextPath()+"/servlet/FileDownload?filepath="+URLEncoder.encode(fileName2, "UTF-8"));//downLoad(gg, response, false);}publicvoid downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {File f = new File(filePath);if (!f.exists()) {response.sendError(404, "File not found!");return;}BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));byte[] buf = new byte;int len = 0;response.reset(); // 非常重要if (isOnLine) { // 在线打开方式URL u = new URL("file:///" + filePath);response.setContentType(u.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(f.getName(), "UTF-8"));// 文件名应该编码成UTF-8} else { // 纯下载方式response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(f.getName(), "UTF-8"));}OutputStream out=null;try {out = response.getOutputStream();while ((len = br.read(buf)) > 0) {out.write(buf, 0, len);}} catch (Exception e) {//e.printStackTrace();}finally{try {br.close();out.close();} catch (Exception e) {}}}
页:
[1]