JSP dynamic output of Excel and Chinese garbled solution

xiaoxiao2021-03-06  24

I recently saw a Java to manipulate the Open Source with Java, I tried it on WebLogic, I feel very good, here I recommend it. First go to http://www.andykhan.com/jexcelapi/index.html to download the latest JEXCELAPI, put JXL.jar in your classpath. Write a JavaBean, use Jexcelapi to dynamically generate an Excel document, I write a simplest, schematic. Complicated you may want to query the database what. //Test.java///package com.jagie.test; import java.io. *; Import jxl. *; Import jxl.write. *; Import jxl.format. *; Import java.util. *; Import java.util. *; Import java.util. *; Import java.awt.Color; public class Test {public static void writeExcel (OutputStream os) throws Exception {jxl.write.WritableWorkbook wwb = Workbook.createWorkbook (os); jxl.write.WritableSheet ws = wwb.createSheet ( "TestSheet1", 0); jxl.write.label labelc = new jxl.write.label (0, 0, "I love China"); ws.addcell (labelc); jxl.write.writablefont wfc = new jxl.write.writableFont (WritableFont .ARIAL, 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN); jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat (wfc); wcfFC.setBackground (jxl.format.Colour .Red; labelc = new jxl.write.label (6, 0, "China loves me", wcffc); ws.addcell (labelc); // Write EXEL worksheet Wwb.write (); // Close Excel Work thin object wwb.close ();} // It is best to write a such main method to test whether your Class is written. Public static void main (string [] args) throws exception {file f = new file ("kk.xls"); f.createNewFile (); WriteExcel (New fileoutputstream (f));}} Write a JSP to use Test This JavaBean outputs an Excel document. //test_excel.jsp// <% @ page import = "com.jagie.test.test"%> <% response.reset (); respternse.setContentType ("Application / VND.MS-Excel"); test. WriteExcel (response.getoutputstream ());%> This is very big, you use IE to access Test_excel.jsp to open dynamically generated Excel documents in IE. Nothing is no.

Maybe someone will ask: response.reset (); can not do this, my suggestion must be written unless you can guarantee that there is no other thing in response's buffer.

Some people may ask: I started at the beginning of JSP and <% @ Page ContentType = "Application / VND.MS-Excel; Charset = GBK"%> This sentence removes Response.SetContentType ("Application / VND.MS-Excel" ); Don't you? Answer this question is very simple, just check the JAVA code generated after compiling JSP, if you change to this, my Welogic7 compiles TEST_EXCEL.JSP, generated Java files, generating code: public void _jspService (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException {// declare and set well-known variables: javax.servlet.ServletConfig config = getServletConfig (); javax.servlet.ServletContext application = config.getServletContext (); javax.servlet.jsp.tagext.Tag _activeTag = null; // variables for Tag extension protocol Object page = this; javax.servlet.jsp.JspWriter out ; javax.servlet.jsp.PageContext pageContext = javax.servlet.jsp.JspFactory.getDefaultFactory () getPageContext (this, request, response, null, true, 8192, true);. response.setHeader ( "Content-Type", " Application / VND.ms-Excel; Charset = GBK "); OUT = PageContext.get (); JSpwriter _ORIGINALOUT = OUT; Javax.Servlet.http.httpsession session = request.getSession (TRUE); try {// error Page Try Block response.setContentType ("Application / VND.MS-Excel; Charset = GBK"); Out.print ("/ r / n / r / n / R / N / R / N"); out.print ("/ r / n"); // [/ Test_excel.jsp; line: 6] response.reset () ; // [/ Test_Excel.jsp; Line: 7] //Response.setContentType ("Application/VND.MS-EXCEL "); // [/test_excel.jsp; line: 8] Test.writeExcel (response.getoutputstream ))); // [/ Test_Excel.jsp; line: 9]} catch (throwable __ee) {while (out! = null && out! = _ORIGINALOUT) OUT =

pageContext.popBody (); ((weblogic.servlet.jsp.PageContextImpl) pageContext) .handlePageException ((Throwable) __ ee);} // before final close brace ...} Obviously, shielding response.setContentType ( "application / vnd .ms-excel "); after Test.WriteExcel (response.getoutPutStream ()); before, response.reset (); there is no correct type of Response ContentType, of course, the output is garbled.

转载请注明原文地址:https://www.9cbs.com/read-65098.html

New Post(0)