Java Excel and PDF files

Source: Internet
Author: User

Java Excel and PDF files

The following are some of the things used in development. Some codes are not completely pasted, but the code for operating Excel is pasted:

Jxl is a Java Excel tool written by Chinese people. In the Open Source world, there are two sets of influential APIs available: poi and jexcelapi. The function is weaker than that of poi. However, jexcelapi provides excellent support for Chinese characters. The API is pure Java and does not depend on Windows systems. Even if it runs in Linux, it can process Excel files correctly. In addition, this API has limited support for graphics and charts and only recognizes PNG formats.

Use:

Build Environment

Unpack the downloaded file, get jxl. jar, and put it into classpath. The installation is complete.

Basic operations

1. Create a file

To generate an Excel file named “test.xls, the first worksheet is named
The general effect of "Page 1" is as follows:

Code
  1. Package test;
  2. // Generate an Excel class
  3. Import java. Io. file;
  4. Import jxl. workbook;
  5. Import jxl. Write. label;
  6. Import jxl. Write. writablesheet;
  7. Import jxl. Write. writableworkbook;
  8. Public class createexcel {
  9. Public static void main (string ARGs []) {
  10. Try {
  11. // Open the file
  12. Writableworkbook book = Workbook. createworkbook (new file ("test.xls "));
  13. // Generate a worksheet named "first page". The parameter 0 indicates that this is the first page.
  14. Writablesheet sheet = book. createsheet ("first page", 0 );
  15. // In the constructor of the label object, the cell position is the first row (0, 0) in the first column)
  16. // And the cell content is test
  17. Label Label = new label (0, 0, "test ");
  18. // Add the defined cells to the worksheet
  19. Sheet. addcell (Label );
  20. /**//*
  21. * To generate a cell that saves numbers, you must use the full package path of number. Otherwise, the location of the cell is the second column, the first row, and the value is 789.123.
  22. */
  23. Jxl. Write. Number = new jxl. Write. Number (1, 0,555.12541 );
  24. Sheet. addcell (number );
  25. // Write data and close the file
  26. Book. Write ();
  27. Book. Close ();
  28. } Catch (exception e ){
  29. System. Out. println (E );
  30. }
  31. }

 

After compilation, an Excel file is generated.

3. Reading files

Take the Excel file we just created as an example to perform a simple read operation. The program code is as follows:

Code
  1. Package test;
  2. // Read the Excel class
  3. Import java. Io. file;
  4. Import jxl. cell;
  5. Import jxl. sheet;
  6. Import jxl. workbook;
  7. Public class readexcel {
  8. Public static void main (string ARGs []) {
  9. Try {
  10. Workbook book = Workbook. getworkbook (new file ("test.xls "));
  11. // Obtain the first worksheet object
  12. Sheet sheet = book. getsheet (0 );
  13. // Obtain the cell in the first row of the first column
  14. Cell cell1 = sheet. getcell (0, 0 );
  15. String result = cell1.getcontents ();
  16. System. Out. println (result );
  17. Book. Close ();
  18. } Catch (exception e ){
  19. System. Out. println (E );
  20. }
  21. }

 

Program Execution result: Test

4. modify files
You can use jexcelapi to modify an existing Excel file. When modifying an Excel file, you can open the file in different ways,
Other operations are the same as creating an Excel file. The following example adds a worksheet to the generated Excel file:

Code
  1. Package test;
  2. Import java. Io. file;
  3. Import jxl. workbook;
  4. Import jxl. Write. label;
  5. Import jxl. Write. writablesheet;
  6. Import jxl. Write. writableworkbook;
  7. Public class updateexcel {
  8. Public static void main (string ARGs []) {
  9. Try {
  10. // Obtain the file in Excel
  11. Workbook WB = Workbook. getworkbook (new file ("test.xls "));
  12. // Open a copy of the file and specify the data to be written back to the original file
  13. Writableworkbook book = Workbook. createworkbook (new file ("test.xls "),
  14. WB );
  15. // Add a worksheet
  16. Writablesheet sheet = book. createsheet ("Page 2", 1 );
  17. Sheet. addcell (new label (0, 0, "test data on the second page "));
  18. Book. Write ();
  19. Book. Close ();
  20. } Catch (exception e ){
  21. System. Out. println (E );
  22. }
  23. }

 

Other operations

I. data formatting

Excel does not involve complex data types. It can be used to process strings, numbers, and dates.

1. String formatting

Character string formatting involves the font, width, font size, and other elements. These functions mainly include writablefont and
Writablecellformat class. Suppose we use the following statement to generate a cell containing strings,
To facilitate the description, we added the number for each line of command:

Code
  1. Writablefont font1 =
  2. New writablefont (writablefont. Times, 16, writablefont. Bold); ①
  3. Writablecellformat format1 = new writablecellformat (font1); ②
  4. Label Label = new label (0, 0, "data 4 test", format1) ③
  5. ① Specifies the string format: The font is times, the font size is 16, and the string format is bold. Writablefont has a rich set
  6. Constructor is used in different cases. The Java-Doc of jexcelapi has a detailed list, which is not listed here.
  7. ② The writablecellformat class is used in the Code. This class is very important. It can be used to specify various types of cells.
  8. Attribute. More descriptions are available in the subsequent cell formatting.
  9. ③ The constructor of the label class is used to specify the format that the string is given.
  10. In the writablecellformat class, another important method is to specify the Data Alignment mode. For example
  11. For the above instance, you can specify:
  12. // Specify the horizontal alignment to center
  13. Format1.setalignment (jxl. format. Alignment. centre );
  14. // Center the vertical alignment
  15. Format1.setverticalalignment (jxl. format. verticalalignment. centre );


Ii. Cell operations

 

An important part of Excel is Cell operations, such as Row Height, column width, and cell merging. Fortunately, jexcelapi
Provides these support. These operations are relatively simple. The following describes only related APIs.

1. Merge Cells

Code
  1. Writablesheet. mergecells (int m, int N, int P, int Q );
  2. // The function is to merge all cells from (m, n) to (p, q), for example:
  3. Writablesheet sheet = book. createsheet ("first page", 0 );
  4. // Merge all cells in the first row to the sixth row of the first column
  5. Sheet. mergecells (0, 0, 5, 0 );


Merging can be either horizontal or vertical. The merged cells cannot be merged again. Otherwise, an exception is triggered.

 

2. Row Height and column width

Code
  1. Writablesheet. setrowview (int I, int height );
  2. // Specifies the height of row I + 1, for example:
  3. // Set the height of the first row to 200
  4. Sheet. setrowview (0,200 );
  5. Writablesheet. setcolumnview (int I, int width );
  6. // Specifies the width of column I + 1, for example:
  7. // Set the width of the first column to 30
  8. Sheet. setcolumnview (0, 30 );


Jexcelapi also has some other functions, such as inserting images. Here we will not introduce them one by one. You can explore them yourself.

 

If you want to read an Excel file, you need to know the number of rows and columns:

Code
  1. Workbook book = Workbook. getworkbook (new file ("test 1.xls "));
  2. // Obtain the first worksheet object
  3. Sheet sheet = book. getsheet (0 );
  4. // Obtain the cell in the first row of the first column
  5. Int columnum = sheet. getcolumns (); // obtain the number of Columns
  6. Int rownum = sheet. getrows (); // get the number of rows
  7. System. Out. println (columnum );
  8. System. Out. println (rownum );
  9. For (INT I = 0; I <rownum; I ++) // read and write cyclically
  10. {
  11. For (Int J = 0; j <columnum; j ++ ){
  12. Cell cell1 = sheet. getcell (J, I );
  13. String result = cell1.getcontents ();
  14. System. Out. Print (result );
  15. System. Out. Print ("/t ");
  16. }
  17. System. Out. println ();
  18. }
  19. Book. Close ();

 

Last Updated:

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.