I believe that everyone will often encounter the situation that needs to generate an Excel report on the Web page. I just encountered in the recent work, the following is an example of using the JXL package (an open source package). The // Servlet generates the routine that provides the downloaded Excel document, where JXL package is used, of course, you can also try with Apache's POI package. public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ ** ...... ** / OutputStream os = response.getOutputStream (); jxl.write.WritableWorkbook wwb = null;
Response.setHeader ("Content-Disposition", "Attachment; FileName =" FNAME ".xls");
Response.setContentType ("Application / MSExcel");
WWB = Workbook.createworkbook (OS); / ** ... ** / wwb.write ();
Os.flush (); wwb.close (); os.close ();} appendix: JXL instructions
JXL's homepage http://hoth.stsci.edu/public/java1.2/jexcel/tutorial.html jxl Summary (below Http://forum.javaeye.com/viewtopic.php?t=4157)
When writing data in the XLS file, you need to pay attention to the first to create a new XLS file OutputStream OS = New FileoutputStream ("c: //excel2.xls");
Establish a work file JXL.WRITE.WRITABLEWORKBOOK WWB = Workbook.createworkBook (New File);
If this file already exists, then we can join a Sheet in this file to separate the previous data; jxl.write.writablesheet WS = wwb.createsheet ("Test Sheet 1", 0); in front of the Createsheet method The parameter is the Sheet name, the next is the Sheet number to operate.
Next, you can write data in this file.
Pay attention to the data when writing data
(1) Added font style jxl.write.writablefont wf = new jxl.write.writablefont (WritableFont.bold, true); WritableFont.bold, true); WritableFont () method parameter description: This method is a container, you can put it In the first few: Times is the font size, he writes 18 second: bold is the judge whether it is a slope, select True, the third: Arial fourth: underlinestyle.no_underline Underline Fifth: JXL.FORMAT.COLOUR.RED font color is red
Jxl.write.writablecellformat wcff = new jxl.write.writablecellFormat (WF); jxl.write.label labelc = new jxl.write.label (0, 0, "this is a label cell", ws.addcell ( Labelc); three parameters in the label () method are the number of columns, the second is the number of lines, the fourth representative of the content to be written is optional, which is the style inside this label. Then write the content into the Sheet by writing the SHEET.
(2) Add Number object with formatting jxl.write.Numberformat nf = new jxl.write.Numberformat ("#. ##");
(3) Add Number object (3.1) to display the format of Number object data
JXL.WRITE.NUMBERFORMAT NF = New jxl.write.Numberformat ("#. ##"); jxl.write.writablecellformat wcfn = new jxl.write.writablecellFormat (NF);
Jxl.write.number labelnf = new jxl.write.Number (1, 1, 3.1415926, ws.addcell (labelnf); Number () method parameter description: The third representation of the input position is input two content
(4) Add Boolean object jxl.write.Boolean labelb = new jxl.write.Boolean (0, 2, false); ws.addcell (labelb);
(5) Add a DateTime object jxl.write.datetime labeldt = new jxl.write.datetime (0, 3, new java.util.date ()); ws.addcell (labeldt); parameter of the DateTime () method front two A third time indicating the input location, the current time of the input
(6) Add a DateFormat object with Formatting this display of all information about the current time, including the annual hourly second jxl.write.dateformat DF = New jxl.write.dateFormat ("DD MM YYYY HH: MM: SS") JXL.WRITE.WRITABECELLFORMAT WCFDF = New JXL.WRITE.WRITABECECELLFORMAT (DF); jxl.write.datetime labeldtf = new jxl.write.datetime (1, 3, new java.util.date (), wcfdf); WS. AddCell (labeldtf);
(7) Add object with font color formatting Jxl.write.writablefont wfc = new jxl.write.writablefont (WritableFont.Arial, 10, Writablefont.no_bold, false, underlinestyle.no_underline, jxl.format.colour.red; JXL.WRITE.WRITABECECELLFORMAT WCFFC = New JXL.WRITE.WRITABECECELLFORMAT (WFC);
Import = "jxl.formaVat. * jxl.write.writablefont wfc = new jxl.write.writablefont (WritableFont.arial, 20, Writablefont.Bold, False, underlinestyle.no_underline, jxl.format.colour.green; (8) Set cell style
JXL.WRITE.WRITABECELLFORMAT WCFFC = New JXL.WRITE.WRITABECECELLFORMAT (WFC); wcffc.setback in (jxl.format.colour.red); // Setting the cell's color is red WCFFC = New JXL.WRITE.LABEL (6, 0, "I Love China", WCFFC);