Print series -- use jxl to generate EXCEL

Source: Internet
Author: User
Tags file url

Jxl installation:
The main is to put the jxl package under the classes of the WEB-INF (if you download the. jar file, put it under Lib). Don't forget to put the common folder in the jxl package under the WEB-INF.

Use of jxl:
The main function is to read and write Excel files.

Read:
This is an idea for reading. First, use an input stream (inputstream) to get the Excel file, then use the workbook in jxl to get the workbook, and use sheet to get the worksheet from the workbook, use cell to obtain a cell in the worksheet.
Inputstream-> workbook-> Sheet-> cell, the cells in the Excel file are obtained.

Code:

  1. <% @ Page contenttype = "text/html; charset = gb2312" %>
  2. <% @ Page import = "Java. Io. *, jxl. *, jxl. Write. *, jxl. Write. *, jxl. format. *" %>
  3. String Path = "C: // excel.xls"; // Excel File URL
  4. Inputstream is = new fileinputstream (PATH); // write to fileinputstream
  5. Jxl. Workbook WB = Workbook. getworkbook (is); // get the working thin
  6. Jxl. Sheet ST = WB. getsheet (0); // obtain the first worksheet in the workbook.
  7. Cell cell = ST. getcell (); // obtain the first cell of the worksheet, that is, A1.
  8. String content = cell. getcontents (); // getcontents () convert the characters in the cell into strings
  9. WB. Close (); // close the workbook
  10. Is. Close (); // close the input stream
    You can also use the getrows () and getcolumns () Methods of sheet to obtain the number of rows and columns for loop control and output all contents in a sheet.

We can use the getcell (x, y) method of sheet to obtain the coordinates of any cell, x, y, and excel.
For example, A1 corresponds to (0, 0), A2 corresponds to (0, 1), D3 corresponds to (3, 2). The coordinates in Excel start from a, 1, and jxl all start from 0.

Write:
The main content written to excel is the class in the jxl. Write package.
The idea is as follows:
Outputstream <-writableworkbook <-writablesheet <-label
The label indicates the cell location and content written to the sheet.
Code:

  1. <% @ Page contenttype = "text/html; charset = gb2312" %>
  2. <% @ Page import = "Java. Io. *, jxl. *, jxl. Write. *, jxl. Write. *, jxl. format. *" %>
  3. Outputstream OS = new fileoutputstream ("C: // test.xls"); // output Excel File URL
  4. Writableworkbook WWB = Workbook. createworkbook (OS); // create a writeable workbook
  5. Writablesheet Ws = WWB. createsheet ("sheet1", 0); // create a writable Worksheet
  6. Label labelcf = new label (0, 0, "hello"); // create the write location and content
  7. WS. addcell (labelcf); // write the label to the sheet.
  8. // The constructor label (int x, int y, string astring) xy of the label refers to the XY of the read, and astring is the written content.
  9. Writablefont WF = new writablefont (writablefont. Times, 12, writablefont. Bold, false );
  10. // Set the write font
  11. Writablecellformat wcff = new writablecellformat (WF); // set cellformat
  12. Label labelcf = new label (0, 0, "hello"); // create a write location, content, and format

Another constructor of label: Label (int c, int R, string cont, cellformat
(St) You can format the written content and set the font and other attributes.

Now you can write
WWB. Write ();
Close after writing
WWB. Close ();
Close the output stream.
OS. close;

OK, as long as the read and write are combined, it is more convenient to read data from N Excel files and write it into the new Excel table you want.

The following is a complete example of using jxl to generate an Excel file:
The Code is as follows:
Test. jsp

  1. <% @ Taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
  2. <HTML: HTML>
  3. <HTML: button property = "button" onclick = "printall ()">
  4. Download
  5. </Html: button>
  6. </Html: HTML>
  7. <Script language = 'javascript '>
  8. Function printall () {location. href = "<% = request. getcontextpath () %>/download. Do ";}
  9. </SCRIPT>

Downloadaction. Java

 

  1. Import org. Apache. Struts. Action .*;
  2. Import javax. servlet. http .*;
  3. Import java. Io. outputstream;
  4. Import test. whw. Upload. excelbean;
  5. Public class downloadaction extends action {
  6. Public actionforward execute (actionmapping mapping,
  7. Actionform form,
  8. Httpservletrequest request,
  9. Httpservletresponse response)
  10. Throws exception {
  11. Try {
  12. String fname = "test"; // Excel file name
  13. Outputstream OS = response. getoutputstream (); // gets the output stream
  14. Response. Reset (); // clear the output stream
  15. Response. setheader ("content-disposition", "attachment; filename =" + fname + ". xls ");
  16. // Set the output file header
  17. Response. setcontenttype ("application/MSExcel"); // defines the output type.
  18. Excelbean EB = new excelbean ();
  19. EB. expordexcel (OS); // call to generate the Excel file Bean
  20. } Catch (exception e ){
  21. System. Out. println (E );
  22. }
  23. Return Mapping. findforward ("display ");
  24. }
  25. }

Excelbean. Java

  1. Package test. whw. Upload;
  2. Import java. Io .*;
  3. Import jxl .*;
  4. Import jxl. Write .*;
  5. Import jxl. format .*;
  6. Import java. util .*;
  7. Import java. AWT. color;
  8. Public class excelbean {
  9. Public excelbean (){}
  10. Public String expordexcel (outputstream OS) throws exception {
  11. Jxl. Write. writableworkbook wbook = Workbook. createworkbook (OS); // create an Excel file
  12. String tmptitle = "test file"; // Title
  13. Jxl. Write. writablesheet wsheet = wbook. createsheet ("first page", 0); // Sheet Name
  14. // Set the Excel title
  15. Jxl. Write. writablefont wfont = new jxl. Write. writablefont (
  16. Writablefont. Arial, 16,
  17. Writablefont. Bold, false, jxl. format. underlinestyle. no_underline,
  18. Jxl. format. colour. Black );
  19. Jxl. Write. writablecellformat wcffc = new jxl. Write. writablecellformat (
  20. Wfont );
  21. Jxl. Write. Label wlabel1;
  22. Wlabel1 = new jxl. Write. Label (5, 0, tmptitle, wcffc );
  23. Wsheet. addcell (wlabel1 );
  24. Wfont = new jxl. Write. writablefont (
  25. Writablefont. Arial, 14,
  26. Writablefont. Bold, false, jxl. format. underlinestyle. no_underline,
  27. Jxl. format. colour. Black );
  28. Wcffc = new jxl. Write. writablecellformat (
  29. Wfont );
  30. Jxl. Write. Label wlabel;
  31. Wlabel = new jxl. Write. Label (0, 0, "write content ");
  32. Wsheet. addcell (wlabel );//
  33. // Merge the cells mergecells (rows, columns, rows, and columns );
  34. Wsheet. mergecells (3, 1, 5, 1 );
  35. Wbook. Write (); // write a file
  36. Wbook. Close ();
  37. OS. Close ();
  38. Return "success ";
  39. }
  40. }

Struts-config.xml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.1 //" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
  3. <Struts-config>
  4. <Action-mappings>
  5. <Action type = "test. whw. Upload. downloadaction" Path = "/download">
  6. <Forward name = "display" Path = "/display. jsp"/>
  7. </Action>
  8. </Action-mappings>
  9. </Struts-config>
  10. <! -- Display. jsp is a successful prompt page -->

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.