Java modifies Excel content

Source: Internet
Author: User

Java through the POI to modify the instance of Excel data online a lot, but the use of the time but may encounter problems, I passed my test, the success of the code to share to everyone, but also to make a record for themselves, save later use.

This example is a program that operates excel2010

The code is as follows:

ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportOrg.apache.poi.xssf.usermodel.XSSFCell;ImportOrg.apache.poi.xssf.usermodel.XSSFRow;ImportOrg.apache.poi.xssf.usermodel.XSSFSheet;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; /*** Replace all study Subject IDs in the AAA file with the "Research topic", Protocol the beginning of the ID with "protocol". This can be changed by itself, depending on the needs of different flexible changes *@authorSun **/       Public classWriteexcelpoi { Public voidWriteexcelpoi () {Try{String FileName= "D:\\aaa.xlsx";//Modify the D-drive aaa.xlsx fileXssfworkbook XWB =NewXssfworkbook (NewFileInputStream (fileName)); Map<string, string> fields =NewHashmap<string, string>(); fields= Getfieldmap ();//gets the collection of fields to modifyString fillstr= "";//storing data in AAA filesString[] Fillsplit=NULL; Xssfsheet Xsheet= Xwb.getsheetat (0);//get the first sheet of an Excel table                 for(inti = 0; I <= xsheet.getlastrownum (); i++) {//Traverse all the rows                    if(Xsheet.getrow (i) = =NULL){//This behavior empty executes the next loop                        Continue; }                                         for(intj = 0; J <= Xsheet.getrow (i). Getphysicalnumberofcells (); J + +) {//Traverse all columns of the current row                        if(Xsheet.getrow (i). Getcell (j) = =NULL){//perform the Next loop for null//System.out.println ("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");                            Continue; } fillstr= (Xsheet.getrow (i)). Getcell (j). ToString ();//gets the data for the current cellFillsplit=fillstr.split ("_");//cutting, my data is "_" as the delimiter of this can be changed according to their own circumstances//Xssfrow Xrow = Xsheet.createrow (i);//Xssfcell XCell = Xrow.createcell (j);Xssfcell Xcell=xsheet.getrow (i). Getcell (j);//get the Cell object, this block can not be written on the side of the two code, can not use createxxx, the words will only be the first column of data to get rid ofXcell.setcellvalue (Fields.get (Fillsplit[0].trim ()) = =NULL? FillStr:fields.get (Fillsplit[0].trim ()));//modify the data to see if the data matches the data in the field collection and does not match the use of metadata//System.out.println (Fields.get (Fillsplit[0].trim ()));}} FileOutputStream out=NewFileOutputStream (fileName);                Xwb.write (out);            Out.close (); } Catch(Exception e) {e.printstacktrace (); }        }        PrivateString getValue (Xssfcell xCell) {if(Xcell.getcelltype () = =Xssfcell.cell_type_boolean) {                returnstring.valueof (Xcell.getbooleancellvalue ()); } Else if(Xcell.getcelltype () = =xssfcell.cell_type_numeric) {                returnstring.valueof (Xcell.getnumericcellvalue ()); } Else {                returnstring.valueof (Xcell.getstringcellvalue ()); }        }        PrivateMap<string, string>Getfieldmap () {Map<string, string> fields =NewHashmap<string, string>(); Fields.put ("Study Subject ID", "Research topic"); Fields.put ("Protocol ID", "protocol");//try{//String fileName = "d:\\yuan.xlsx";//Xssfworkbook XWB = new Xssfworkbook (new FileInputStream (FileName));//        //Xssfsheet Xsheet = Xwb.getsheetat (3);//for (int i = 0; I <= xsheet.getlastrownum (); i++) {//Fields.put (Xsheet.getrow (i). Getcell (0). ToString (), Xsheet.getrow (i). Getcell (1). ToString ());////System.out.println ("---" +xsheet.getrow (i). Getcell (0) + "*---" +fields.get ("A1"));//                }//            }//catch (Exception e) {//e.printstacktrace ();//            }            returnFields ; }          Public Static voidMain (string[] args) {Writeexcelpoi a=NewWriteexcelpoi ();        A.writeexcelpoi (); }    }

Using the jar package in: http://download.csdn.net/detail/gaizhongfeng/8535377

If you have any questions, please point it out, or you can add my qq:212966054.

Java modifies Excel content

Related Article

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.