Java poi out of Excel line wrapping problem

Source: Internet
Author: User

The POI action Excel implements the line break problem.

Package Jp.co.misumi.mdm.batch.common.jobrunner;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Org.apache.poi.encrypteddocumentexception;import Org.apache.poi.openxml4j.exceptions.invalidformatexception;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.cellstyle;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.ss.usermodel.workbookfactory;import Org.apache.poi.xssf.usermodel.xssfworkbook;import Com.amazonaws.services.devicefarm.model.test;import Jp.co.misumi.mdm.batch.common.def.batchconstants;import JP.CO.MISUMI.MDM.CORE.COMMON.EXCEL.WRITER.EXCELWRITEREXT; Public classdemo{ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub                    Try{Test (); }            Catch(encrypteddocumentexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            Catch(invalidformatexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println ("---END---"); }         Public Static voidTest () throws IOException, Encrypteddocumentexception, invalidformatexception {//initializing the input streamInputStream INP; Try{Xssfworkbook WB=NULL; Try{WB=NewXssfworkbook ("d:/workbook.xlsx"); }            Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                                    //create a read corresponding file generation object//get a No. 0 working thinSheet Sheet = Wb.getsheetat (0); //get the 3rd row of the small label starting from 0, so it's 2.Row row = Sheet.getrow (2); //Get the third row 4th cell 4D small mark starting from 0 so it's three.Cell cell = Row.getcell (3); //See if the cell is defined and returns null if no definition is defined//, otherwise returns a logical cell, if none is created//?? Why do I need to be judged empty first, because if the cell is already//exists and has been defined, if you are creating, the original cell//will be overwritten by the cells you re-create, which can save//your space because there is no re-creation of the object            if(Cell = =NULL) Cell= Row.createcell (3); //the type of the cell that is set is string string literal typeCell.setcelltype (cell.cell_type_string); //Set Value//setting a value in a cell uses a \ n line breakString xxx ="Use \ n with Word wrap on to create a new line";              Cell.setcellvalue (XXX); //Cell.setcellvalue ("a Test"); CellStyle CS=Wb.createcellstyle (); Cs.setwraptext (true);                        Cell.setcellstyle (CS); //increase the height of the row to fit the height of 2 lines of text, set the height units (pixels)Row.setheightinpoints ((2*sheet.getdefaultrowheightinpoints ())); //adjust the column width to fit content//Sheet.autosizecolumn ((short) 2); //re-write as fileFileOutputStream fileout =NewFileOutputStream ("d:/workbook1.xlsx");              Wb.write (fileout);          Fileout.close (); }finally{                    }    }}

This is because even with this method can not change, because I extracted from the DB string with the result is translated into "\\n", the problem can not change the line, there is no way, can only be extracted to the other identifiers and then to replace.

Java poi out of Excel line wrapping problem

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.