POI Excel text vertical display (figure)

Source: Internet
Author: User

I am now using poi-3.8 version, which briefly describes the download and the API location of the framework.

@ Author YHC


Poi: POI Download Page

 

A Brief Introduction to POI Quick Guide to Excel operations:

Decompress the downloaded file and follow this path to open the API: \ poi-bin-3.8-20120326 \ poi-3.8 \ docs \ index.html

 

My current operation only requires a jar package: poi-3.8-20120326.jar

The following is the code for setting the text to be displayed vertically:


[Java]
Package org. excel. test;
 
Import java. io. FileOutputStream;
Import java. util. Calendar;
Import java. util. Date;
 
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. ss. usermodel. Cell;
Import org. apache. poi. ss. usermodel. CellStyle;
Import org. apache. poi. ss. usermodel. CreationHelper;
Import org. apache. poi. ss. usermodel. Font;
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. util. CellRangeAddress;
 
/**
* Excel Export
* @ Author Administrator
*
*/
Public class POIExcelExportTest {
 
Public static void main (String [] args) throws Exception {
// Create an Excel file
Workbook wb = new HSSFWorkbook ();
// Create an output stream object to write the created Excel File
FileOutputStream fileOut = new FileOutputStream ("WORKBOOK.xls ");
//
CreationHelper createHelper = wb. getCreationHelper ();
// Create a sheet
Sheet sheet = wb. createSheet ("Appendix ");
// Merge a cell, which will be illustrated below
Sheet. addMergedRegion (new CellRangeAddress (
0, // first row (0-based)
10, // last row (0-based)
0, // first column (0-based)
1 // last column (0-based)
));

// Create a row object with the subscript starting from 0
Row row1 = sheet. createRow (0 );
// Create a style
CellStyle cellStyle1 = wb. createCellStyle ();
// Set the left and right alignment to center
CellStyle1.setAlignment (CellStyle. ALIGN_CENTER );
// Center it vertically
CellStyle1.setVerticalAlignment (CellStyle. VERTICAL_CENTER );
// Set true to display the content in the Cell in multiple rows
CellStyle1.setWrapText (true );
// Create a cell with the subscript starting from 0
Cell c1 = row1.createCell (0 );
// Create rich text. If you want to display a font for each line, write the text below me. If you want
// Remove the \ r \ n between multiple rows.
C1.setCellValue (
CreateHelper. createRichTextString
("After \ r \ n and \ r \ n, \ r \ n" +
", \ R \ n this \ r \ n only \ r \ n value \ r \ n! "
));
// Set the style
C1.setCellStyle (cellStyle1 );
// Write a file
Wb. write (fileOut );
// Close the output stream
FileOut. close ();
}
}

Package org. excel. test;

Import java. io. FileOutputStream;
Import java. util. Calendar;
Import java. util. Date;

Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. ss. usermodel. Cell;
Import org. apache. poi. ss. usermodel. CellStyle;
Import org. apache. poi. ss. usermodel. CreationHelper;
Import org. apache. poi. ss. usermodel. Font;
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. util. CellRangeAddress;

/**
* Excel Export
* @ Author Administrator
*
*/
Public class POIExcelExportTest {

Public static void main (String [] args) throws Exception {
// Create an Excel file
Workbook wb = new HSSFWorkbook ();
// Create an output stream object to write the created Excel File
FileOutputStream fileOut = new FileOutputStream ("WORKBOOK.xls ");
//
CreationHelper createHelper = wb. getCreationHelper ();
// Create a sheet
Sheet sheet = wb. createSheet ("Appendix ");
// Merge a cell, which will be illustrated below
Sheet. addMergedRegion (new CellRangeAddress (
0, // first row (0-based)
10, // last row (0-based)
0, // first column (0-based)
1 // last column (0-based)
));

// Create a row object with the subscript starting from 0
Row row1 = sheet. createRow (0 );
// Create a style
CellStyle cellStyle1 = wb. createCellStyle ();
// Set the left and right alignment to center
CellStyle1.setAlignment (CellStyle. ALIGN_CENTER );
// Center it vertically
CellStyle1.setVerticalAlignment (CellStyle. VERTICAL_CENTER );
// Set true to display the content in the Cell in multiple rows
CellStyle1.setWrapText (true );
// Create a cell with the subscript starting from 0
Cell c1 = row1.createCell (0 );
// Create rich text. If you want to display a font for each line, write the text below me. If you want
// Remove the \ r \ n between multiple rows.
C1.setCellValue (
CreateHelper. createRichTextString
("After \ r \ n and \ r \ n, \ r \ n" +
", \ R \ n this \ r \ n only \ r \ n value \ r \ n! "
));
// Set the style
C1.setCellStyle (cellStyle1 );
// Write a file
Wb. write (fileOut );
// Close the output stream
FileOut. close ();
}
}
After running the above Code:

 

 


Now let's explain the code for merging cells in the above Code:

 

 


 

Author: yhc13429826359
 

 

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.