Java Operations Excel table

Source: Internet
Author: User

The interaction between Java encoding and Excel is very much used in actual development, and today it is simple to use JXL to realize their interaction. Nonsense not much to say, direct code!

First, you have to import the JXL rack package:

1. Writing data to Excel

  1. Package com.easyteam.yc01;
  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 Operateexcel {
  8. Public void Getexcel () throws exception{
  9. File File = new file ("D:" +file.separator+"A.xls"); Create a file
  10. if (!file.exists ()) {
  11. File.createnewfile ();
  12. }
  13. Writableworkbook workbook=workbook.createworkbook (file); //Create a workbook
  14. Writablesheet Sheet=workbook.createsheet ("Sheet1",0); Create sheet
  15. String[] title={"id","name","Age"}; Defining header Elements
  16. Populating data into Excel
  17. for (int i=0;i<title.length;i++) {
  18. Label lable1=New Label (I,0,title[i]); Table Header
  19. Label lable2=new label (0,i+1,i+""); ID
  20. Label lable3=new label (1,i+1,"Zhang San" +i); Name
  21. Label lable4=new label (2,i+1,"+i"); Age
  22. Sheet.addcell (LABLE1);
  23. Sheet.addcell (Lable2);
  24. Sheet.addcell (LABLE3);
  25. Sheet.addcell (LABLE4);
  26. }
  27. Workbook.write (); //Write Data
  28. Workbook.close (); //Off
  29. }
  30. }

Test:

    1. Package com.easyteam.yc01;
    2. Public class Test {
    3. Public static void Main (string[] args) throws Exception {
    4. Operateexcel op=New Operateexcel ();
    5. Op.getexcel ();
    6. }
    7. }

Test results:



2. Read the data in Excel

  1. Package com.easyteam.yc01;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import JXL. Cell;
  5. Import JXL. Sheet;
  6. Import JXL. Workbook;
  7. Import jxl.read.biff.BiffException;
  8. Import Jxl.write.WritableWorkbook;
  9. Public class Getexceldata {
  10. public void GetData () throws exception{
  11. File File = new file ("D:" +file.separator+"A.xls");
  12. Workbook workbook=workbook.getworkbook (file); //Get a working thin
  13. Sheet Sheet =workbook.getsheet (0); Get sheet
  14. int rows = Sheet.getrows (); //number of rows
  15. int columns = Sheet.getcolumns (); //Number of columns
  16. For (int i=0;i<rows;i++) {
  17. For (int j=0;j<columns;j++) {
  18. Cell cell= Sheet.getcell (J, I); //Get cell
  19. String str=cell.getcontents (); //Get cell contents
  20. System.out.print (str+"\ t");
  21. }
  22. System.out.println ();
  23. }
  24. }
  25. }

Test:

    1. Package com.easyteam.yc01;
    2. Public class Test1 {
    3. Public static void Main (string[] args) throws Exception {
    4. Getexceldata ged=New Getexceldata ();
    5. Ged.getdata ();
    6. }
    7. }

Test results:

Java Operations Excel table

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.