Poi outputs the Excel display content, and poi outputs the excel content

Source: Internet
Author: User

Poi outputs the Excel display content, and poi outputs the excel content

In the business system, how many times has access to Excel parsing. Choosing Apache POI on the java development platform is a wise choice. POI provides an excellent API to read or write data to Microsoft Office Excel.

At present, the imported data is processed twice. In our development mode, the content in Excel is directly imported into a data table corresponding to the database as is, and then processed twice. What is the original, that is, what we see in excle, what is the imported data, and how to implement it?

First, consider that exce is saved as csv, and then can be parsed as csv. After excel is saved as csv

It seems that this is okay, but it is also troublesome to convert the csv File Uploaded by the user through the background (PS: Actually, I don't know how to convert it, some of you may want to share it with me.) Are you sure you want to use POI? So let's take a look at some information about how POI processes Excel.

It is learned that the HSSF In the POI class is used to process the 03 format, and the XSSF is used to process the 07 and above formats. If you don't talk much about it, first look at the DataFormatter, an important role output as is,

Refer to the official documents. The red box clearly shows the actual text in the real Excel file.

We also need to judge the formula type here,

1     DataFormatter df = new DataFormatter();2     if (cell.getCellType() == cell.CELL_TYPE_FORMULA) {3         FormulaEvaluator formulaEval = wb.getCreationHelper().createFormulaEvaluator();4         value = df.formatCellValue(cell, formulaEval);5     } else {6         value = df.formatCellValue(cell);7     }

So with such a code, the value displayed in the cell is achieved by a big engineer. Ps: parse the complete code of Excle

1 private ArrayList <String []> GetExcel (String filename) 2 throws FileNotFoundException, IOException, Exception {3 File f = new File (filename); 4 ArrayList data = new ArrayList (); 5 if (f. exists () {6 InputStream FCM = new FileInputStream (f); 7 Workbook wb = WorkbookFactory. create (FS); 8 Sheet fst = wb. getSheetAt (0); 9 int rowcount = fst. getLastRowNum (); 10 Row headrow = fst. getRow (0); 11 int colco Unt = headrow. getLastCellNum (); 12 for (int ri = 0; ri <= rowcount; ri ++) {13 System. out. println (""); 14 System. out. println ("Number" + ri + "Row data"); 15 String [] colValues = new String [colcount]; 16 row Row = fst. getRow (ri); 17 for (int I = 0; I <colcount; I ++) {18 Cell cell = row. getCell (I); 19 String value = ""; 20 if (cell! = Null) {21 DataFormatter df = new DataFormatter (); 22 if (cell. getCellType () = cell. CELL_TYPE_FORMULA) {23 FormulaEvaluator formulaEval = wb. getCreationHelper (). createFormulaEvaluator (); 24 value = df. formatCellValue (cell, formulaEval); 25} else {26 value = df. formatCellValue (cell); 27} 28} 29 colValues [I] = value; 30 System. out. print (colValues [I] + "--"); 31} 32 data. add (colValues); 33} 34} 35 f. delete (); 36 return data; 37}View Code

2015-4 4

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.