JAVA-based Excel read/write-poi, javaexcel-poi

Source: Internet
Author: User

JAVA-based Excel read/write-poi, javaexcel-poi

The previous article introduced xls. jar to generate an Excel file. This article introduces how to use poi to read and write Excel files. The content is very simple and the code annotations are clear.

1. generate an Excel file:

Import java. io. file; import java. io. fileOutputStream; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; public class poi_write {public static void main (String [] args) {final String [] str = {"id", "name", "sex "}; // create an Excel worksheet HSSFWorkbook workbook = new HSSFWorkb Ook (); // create an Excel worksheet HSSFSheet = workbook. createSheet (); // create the first HSSFRow row = sheet. createRow (0); HSSFCell cell = null; // Add the header for (int I = 0; I <str. length; I ++) {cell = row. createCell (I); cell. setCellValue (str [I]);} // append data for (int I = 1; I <= 10; I ++) {HSSFRow rows = sheet. createRow (I); HSSFCell cell_id = rows. createCell (0); cell_id.setCellValue (I + ""); HSSFCell cell_name = rows. create Cell (1); cell_name.setCellValue ("name:" + I); HSSFCell cell_sex = rows. createCell (2); cell_sex.setCellValue ("male");} // File storage path file = new File ("E:/cnblogs/poi_text.xls"); try {if (! File. exists () {file. createNewFile ();} FileOutputStream fileOut = new FileOutputStream (file); // create a file output stream object workbook. write (fileOut); fileOut. close (); // close the output stream object} catch (Exception e) {e. printStackTrace ();} System. out. println ("Excel file generated ");}}

2. Read Excel files:

Import java. io. file; import java. io. IOException; import org. apache. commons. io. fileUtils; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; public class poi_reading {public static void main (String [] args) {final File file = new File ("E:/cnblogs/poi_text.xls"); if (file. exists () {try {HSSFWorkbook workbook = new HSSFWorkbook (FileUtils. openInputStream (file); // workbook. getSheet ("Sheet0"); get the specified worksheet // HSSFSheet sheet = workbook. getSheet ("Sheet0"); // workbook. getSheetAt (0); the first worksheet HSSFSheet = workbook is obtained by default. getSheetAt (0); int firstRowNum = 0; // obtain the row number int lastRowNum = sheet in the last row of the current sheet. getLastRowNum (); for (int I = firstRowNum; I <= lastRowNum; I ++) {HSSFRow row = sheet. getRow (I); // get the last cell column number of the current row int lastCellNum = row. getLastCellNum (); for (int j = 0; j <lastCellNum; j ++) {HSSFCell cell = row. getCell (j); String value = cell. getStringCellValue (); System. out. print (value + "");} System. out. println ("") ;}} catch (IOException e) {e. printStackTrace () ;}} else {System. out. println ("the read file does not exist ");}}}

All right, the basic methods have been introduced for you, and you can find the best results on your own.

Do you find a problem? The end of lifecycle. How can we generate a later version of Excel? We can't just modify the suffix of the File. here we need to use XSSFWorkbook to create a workbook, and then remove the beginning of the remaining HSSF. The Excel file at the end of the sheet.

In case of any errors, please correct them. Thank you.

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.