Java POI Operation EXCEL,XSSF Reading Excel 2007, some cells are fixed values

Source: Internet
Author: User

Originally wanted to look at Java Io,nio, found this piece of knowledge system is quite big. Temporarily write a demo that operates Excel. Due to the time relationship, completed the function, the latter continues to improve.

Function: Read the Excel table (the table is a test results table, a total of more than 10 columns, the first row is the title), the No. 0 column is marked as an ID (increment), the 9th column is marked as a result (default is pass), and the 10th column is marked as a name.

You can use Excel's drag-and-drop functionality, but because the content and styles in Excel often need to be modified, this results in repetitive work. Temporarily write this small demo, look forward to perfect after the function more detailed.

Import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.ss.usermodel.cell;//import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.sheet;import Org.apache.poi.ss.usermodel.workbook;import Org.apache.poi.xssf.usermodel.xssfcell;import Org.apache.poi.xssf.usermodel.xssfrow;import Org.apache.poi.xssf.usermodel.xssfworkbook;import java.io.*;/** * Created by N on 2015/4/29. */public class Insertinfotoexcel {//This method determines the Excel version static Workbook Openworkbook (InputStream in, String filename) thro        WS IOException {Workbook WB = null; if (Filename.endswith (". xlsx")) {wb = new Xssfworkbook (in);//excel} else {wb = (workb    ook) New Hssfworkbook (in);//excel 2003} return WB; }//This method processes the data in Excel, marks the first column as an ID (increment), the 9th column is marked as the result (by default, pass), and the 10th column is marked with the name public static void Setexceldata (String fileName) throws    Exception {InputStream in = new FileInputStream (fileName); Create an input stream Workbook WB = openworkBook (in, FileName);//Gets the Excel file object Sheet Sheet = wb.getsheetat (0);//Gets the file's specified sheet m default to the first row row = null;    int totalrows = Sheet.getlastrownum (); Total number of rows int totalcells = Sheet.getrow (0). Getlastcellnum ();//number of columns, based on the first row of System.out.println ("Number of columns:" + Totalcell        S + "Number of lines:" + totalrows); Get each line in order for (int i = 1; I <= sheet.getlastrownum (); i++) {Xssfrow row = (xssfrow) sheet.getrow (i);            Gets the Row object if (row = = null) {//If empty, does not handle continue; }//marks the No. 0 column as an ID, incremented.                Encounter empty first Regardless, skip if (Row.getcell (0) = null) {Cell CellIndex = Row.getcell (0);                System.out.print (Cellindex.getnumericcellvalue ());            Cellindex.setcellvalue (i);                } else {Xssfcell cellindex = Row.createcell (0);            Cellindex.setcellvalue (i);            }//marks the 9th column as the test result, and encounters an empty mark as pass, not empty. if (Row.getcell (9) = = null) {Xssfcell celLResult = Row.createcell (9);                System.out.print (Cellresult.getstringcellvalue ());            Cellresult.setcellvalue ("PASS"); }//marks the 10th column as the name of the tester.            Both are marked as names, whether they are empty or not. if (Row.getcell) = null) {Xssfcell cellname = Row.getcell (ten);//System.out.print (Celln                Ame.getstringcellvalue ());            Cellname.setcellvalue ("Aashen");                } else {Xssfcell cellname = Row.createcell (10);            Cellname.setcellvalue ("Aashen");        }}//write data, close outputstream out = new FileOutputStream (fileName);        Wb.write (out);        In.close ();    Out.close ();        public static void Main (string[] args) throws Exception {//String filename= "E:" + file.separator+ "Hello.txt";        String fileName = "e://hi.xlsx"; Setexceldata (filename);//file F=new file (filename);//if (f.exists ())//System.out.println ("New F Ile successfully ");//Writer out =new FileWriter (f);//String str= "Hello";//Out.write (str);//Out.close (); }}

Java POI Operation EXCEL,XSSF Reading Excel 2007, some cells are fixed values

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.