Java implements import and export of Excel tables

Source: Internet
Author: User

Note that this approach requires the introduction of the POI jar package

Build a Student class first
public class Student {

private String name;
private int age;
private int grade;

}

Add get set and its parameter and parameterless construction method;

Prepare the test data set to create an Excel table

public class Exceloperate {

public static void Main (string[] args) {
Create an Excel table
Createexcel (Getstudent ());

       //Read Excel form
        list< student> list = Readexcel ();
        System.out.println (list.tostring ());
   }

/**
* Initialize Data
*
* @return Data
*/
private static list<student> getstudent () {
list<student> list = new arraylist<student> ();
Student student1 = new Student ("Xiaoming", 8, "second Grade");
Student Student2 = new Student ("Small aperture", 9, "third grade");
Student Student3 = new Student ("Floret", 10, "grade Four");
List.add (STUDENT1);
List.add (Student2);
List.add (STUDENT3);
return list;
}

/**
* Create Excel
*
* @param list
* Data
*/
private static void Createexcel (List<student> List) {
Create an Excel file
Hssfworkbook workbook = new Hssfworkbook ();
Create a worksheet
Hssfsheet sheet = workbook.createsheet ("Student form One");
Add header row
Hssfrow hssfrow = sheet.createrow (0);
Set cell formatting to center
Hssfcellstyle CellStyle = Workbook.createcellstyle ();
Cellstyle.setalignment (Hssfcellstyle.align_center);

Add header Content
Hssfcell Headcell = Hssfrow.createcell (0);
Headcell.setcellvalue ("name");
Headcell.setcellstyle (CellStyle);

Headcell = Hssfrow.createcell (1);
Headcell.setcellvalue ("Age");
Headcell.setcellstyle (CellStyle);

Headcell = Hssfrow.createcell (2);
Headcell.setcellvalue ("grade");
Headcell.setcellstyle (CellStyle);

Add Data content
for (int i = 0; i < list.size (); i++) {
Hssfrow = Sheet.createrow ((int) i + 1);
Student Student = List.get (i);

Create a cell and set a value
Hssfcell cell = Hssfrow.createcell (0);
Cell.setcellvalue (Student.getname ());
Cell.setcellstyle (CellStyle);

Cell = Hssfrow.createcell (1);
Cell.setcellvalue (Student.getage ());
Cell.setcellstyle (CellStyle);

            cell = Hssfrow.createcell (2);
            Cell.setcellvalue (Student.getgrade ());
            Cell.setcellstyle (CellStyle);
       }

       //Save Excel File
        try {
            OutputStream outputstream = new FileOutputStream ("D:/students.xls");
            Workbook.write (outputstream);
            outputstream.close ();
       } catch (Exception e) {
             E.printstacktrace ();
       }
   }

   /**
     * Read Excel
     *
      * @return Data collection
     */
    private static list<student> Readexcel () {
        list<student> List = new Arraylist<student> ( );
        Hssfworkbook workbook = null;

try {
Reading Excel files
InputStream InputStream = new FileInputStream ("D:/students.xls");
workbook = new Hssfworkbook (InputStream);
Inputstream.close ();
} catch (Exception e) {
E.printstacktrace ();
}

Looping worksheets
for (int numsheet = 0; Numsheet < workbook.getnumberofsheets (); numsheet++) {
Hssfsheet Hssfsheet = Workbook.getsheetat (Numsheet);
if (Hssfsheet = = null) {
Continue
}
Loop Line
for (int rowNum = 1; rowNum <= hssfsheet.getlastrownum (); rownum++) {
Hssfrow Hssfrow = Hssfsheet.getrow (RowNum);
if (Hssfrow = = null) {
Continue
}

Save the contents of a cell in a collection
Student Student = new Student ();

                Hssfcell cell = Hssfrow.getcell (0);
                if (cell = = null) {
                     continue;
               }
                Student.setname ( Cell.getstringcellvalue ());

                cell = Hssfrow.getcell (1);
                if (cell = = null) {
                     continue;
               }
                Student.setage (( int) cell.getnumericcellvalue ());

Cell = Hssfrow.getcell (2);
if (cell = = null) {
Continue
}
Student.setgrade (Cell.getstringcellvalue ());

List.add (student);
}
}
return list;
}

}

Java implements import and export of Excel tables

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.