Apache POI Read and create Excel----01 (Simple operation)

Source: Internet
Author: User

public class Excelcreatandread {


/**
* Create an Excel document using Apache POI
* */
public static void Createxl () {
/**excel the location of the file to be stored, assuming that it is under the D disk */
String outputfile= "D:\\test.xlsx";
try {
Create a new Excel workbook
Xssfworkbook Workbook =new Xssfworkbook ();

Create a worksheet in the Excel workbook, which is called the default value
Hssfsheet Sheet=workbook.createsheet ();
Build a worksheet in an Excel workbook named "Test"
Xssfsheet sheet=workbook.createsheet ("test");

Create first row
Xssfrow row =sheet.createrow (0);
Row.createcell (0). Setcellvalue ("Student Information");

Merge cells
Sheet.addmergedregion (New Cellrangeaddress (0,0,0,1));

Create a row at index 0 (first row)
Xssfrow row1=sheet.createrow (0);

Create a column at index 0 (first column)
Xssfcell Cell=row1.createcell (0);//The horizontal line is representative of this method is obsolete, is not recommended to use;

Defines the cell as a string type (excel-format cell-number-text; Do not set default to "General", can also be set to other, specific settings refer to related documents)
Cell.setcelltype (hssfcell.cell_type_string);

Enter some content in the cell
Cell.setcellvalue ("name");
Row1.createcell (1). Setcellvalue ("Age");

Create the second line:
Xssfrow Row2=sheet.createrow (1);
Row2.createcell (0). Setcellvalue ("Zhang San");
Row2.createcell (1). Setcellvalue (18);

Create the third line:
Xssfrow Row3=sheet.createrow (2);
Row3.createcell (0). Setcellvalue ("John Doe");
Row3.createcell (1). Setcellvalue (20);


Create a new output file stream
FileOutputStream fout=new FileOutputStream (outputFile);

Save the corresponding Excel workbook
Workbook.write (Fout);

Flush ();
Java when using a stream, there will be a buffer, according to a method it considers more efficient to send data: The data to be sent to the buffer, the buffer is full and then once the past, rather than separate once to send.
Flush () means that the data in the buffer is forced to be sent out without waiting for the buffer to be full.
So if there is no flush () when using the stream, there are many situations where the other side of the stream cannot read the data, especially if the data is very small.
Fout.flush ();

End of operation, close file
Fout.close ();
SYSTEM.OUT.PRINTLN ("File generation");

} catch (Exception e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Already Running Xlcreate ():" +e);
}

}
/** reading Excel Content
* @throws FileNotFoundException * *
public static void Readexcel () throws filenotfoundexception{
FileInputStream file=new FileInputStream ("d:\\test.xlsx");

try {
Xssfworkbook Workbook =new xssfworkbook (file);
Xssfsheet sheet=workbook.getsheet ("test");
Xssfrow row =sheet.getrow (0);
Xssfcell Cell=row.getcell (0);
System.out.println (Cell.getstringcellvalue ());


} catch (Exception e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Already Running Readexcel ():" +e);
}


Put the imported content in the collection
List temp =new ArrayList ();
try {
Xssfworkbook Workbook =new Xssfworkbook (New FileInputStream ("d:\\test.xlsx"));
Xssfsheet sheet=workbook.getsheet ("test");
for (Row R:sheet) {
if (R.getrownum () <1) {
Continue
}
String Name=r.getcell (0). Getstringcellvalue ();
int age= (int) R.getcell (1). Getnumericcellvalue ();
Temp.add (name);
Temp.add (age);
}

File.close ();

Traverse List Temp
Iterator it = Temp.iterator ();
while (It.hasnext ()) {
System.out.print (It.next () + "");
}
System.out.println ();
SYSTEM.OUT.PRINTLN ("read Complete");

} catch (Exception e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Already running Readexcel" +e);
}

}

public static void Main (string[] args) throws FileNotFoundException {
CREATEXL ();//Create an Excel document

Readexcel ();//Read Excel content

}

}

Apache POI Read and create Excel----01 (Simple operation)

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.