Java uses POI to generate Excel Force line break (reprint)

Source: Internet
Author: User

Use POI to create a simple Myxls.xls file

The usual package is org.apache.poi.hssf.usermodel.*;
Example:

Import java.io.*;
Import org.apache.poi.hssf.usermodel.*;

public class Zoomsheet {

Public Zoomsheet () {
}

public static void Main (String args[])
Throws IOException {

Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet Sheet1 = wb.createsheet ("new sheet");
FileOutputStream fileout = new FileOutputStream ("Workbook.xls");
Wb.write (fileout);
Fileout.close ();
}
}

Class:
Hssfworkbook create an XLS object; Hssfworkbook HW = new Hssfworkbook ();
Set the partition display; Hw.setrepeatingrowsandcolumns (sheet index, row, column, row, column);
Hssfsheet Create sheet (worksheet) in XLS; Hssfsheet sheet = hw.createsheet ("Sheet1"); Sheet1 is the name of the sheet can be the default
Set the column height; Sheet.setcolumnwidth (short) short;
Hssfrow create the lines in the XLS; Hssfrow row = sheet.createrow (0); 0 indicates the first row
Set the row height; Row.setheight (short);
Hssffont Create a font in XLS; Hssffont font = Hw.createfont ();
Set the font size; Font.setfontheightinpoints ((short) 54);
set to italic; Font.setitalic (TRUE);
Set text delete line; Font.setstrikeout (TRUE);
Hssfcellstyle setting cell style; Hssfcellstyle style = Wb.createcellstyle ();
add fonts; Style.setfont (font);
Hssfcell set the cell; Hssfcell cell = Row.createcell ((short) 0);
Cell horizontal alignment; Style.setalignment (align); Cell level 0 Normal 1 left Align 2 center 3 right 4 fill 5 justified 6 center selection
Cell vertical alignment; Style.setverticalalignment (align); Cell Vertical 0 Home 1 Center 2 3

The lower border of the cell is a thin line; Style.setborderbottom (short);
Use the same command together, set the color; Style.setbottombordercolor (short);
Cell left box; Style.setborderleft (short);
Style.setleftbordercolor (short);
Cell right border; Style.setborderright (short);
Style.setrightbordercolor (short);
The top border of the cell; Style.setbordertop (short);
Style.settopbordercolor (short);
Cell character number (Chinese); Cell.setencoding (HSSFCELL.ENCODING_UTF_16); Chinese
The value displayed by the cell; Cell.setcellvalue ("Chinese Medicine"); The types of values are: Double,int,string,date,boolean
Cell background color; Style.setfillforegroundcolor (short);
Pattern type; Style.setfillpattern (short);
Cell merging; Sheet.addmergedregion (Row, (short) column, row, (short) column));


cell style added; Cell.setcellstyle (style);




Print settings
Importing package import Org.apache.poi.hssf.usermodel.HSSFPrintSetup;
Create a Print Setup object Hssfprintsetup HPS = Hs.getprintsetup ();
Set A4 paper Hps.setpapersize ((short) 9);
Set the page to landscape print mode Hps.setlandscape (true);

Sets the print page to center horizontally sheet.sethorizontallycenter (true);
Sets the print page to center vertically sheet.setverticallycenter (true);

Online found articles are said in the text in Excel plus \n,\n\r,\r\n, anyway, all kinds of, and even more strange is that the cell added <br>
Later I tried to use the \ r after the effect is generated in the file, you use the open, and will not wrap, if you use the mouse in the cell after a click on the line will be automatically wrapped.


Can be done in the following ways,
1. First use the POI style in the cells that need to be forced to wrap, and set the style to wrap
# Hssfcellstyle Cellstyle=workbook.createcellstyle ();
# Cellstyle.setwraptext (TRUE);
# Cell.setcellstyle (CellStyle);
2. followed by a cell that requires a newline, use \ to force a newline
1. Hssfcell cell = Row.createcell ((short) 0);
2. Cell.setcellstyle (CellStyle); Cell.setcellvalue (New hssfrichtextstring ("Hello\ r \ n world!"));
This makes it possible to force a line break,
The effect of line break is to force a line break in the cell.
Hello
world!

Java uses POI to generate Excel Force line break (reprint)

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.