POI Excel 08

Source: Internet
Author: User
Tags word wrap

@ Author YHC

Read and override Excel

Click me !! Check the Excel creation code !!!!! <<==== The Excel file used to create the test is POI Excel 02 [different types of cells in the workbook]

 

[Java]
// Initialize the input stream
InputStream indium = new FileInputStream ("D:/workbook.xls ");
// Create an object for reading the corresponding file
Workbook wb = WorkbookFactory. create (indium );
// Get 0th working Thicknesses
Sheet sheet = wb. getSheetAt (0 );
// Get the small mark of row 3rd starting from 0, so it is 2
Row row = sheet. getRow (2 );
// Obtain the first cell 4D tag in the third row starting from 0. Therefore, it is three.
Cell cell = row. getCell (3 );
// Check whether the cell is defined. If no definition is defined, NULL is returned.
//; Otherwise, a logical cell is returned. If not, a logical cell is created.
//?? Why do we need to judge if the cell is empty?
// It exists and has been defined. If you are creating, the original cell
// It will be overwritten by the cell you re-created, which can save
// Your space, because the object is not re-created
If (cell = null)
Cell = row. createCell (3 );
// Set the cell type to String text type.
Cell. setCellType (Cell. CELL_TYPE_STRING );
// Set the value
Cell. setCellValue ("a test ");
 
// Re-write a file such
FileOutputStream fileOut = new FileOutputStream ("D:/workbook.xls ");
Wb. write (fileOut );
FileOut. close ();

// Initialize the input stream
InputStream indium = new FileInputStream ("D:/workbook.xls ");
// Create an object for reading the corresponding file
Workbook wb = WorkbookFactory. create (indium );
// Get 0th working Thicknesses
Sheet sheet = wb. getSheetAt (0 );
// Get the small mark of row 3rd starting from 0, so it is 2
Row row = sheet. getRow (2 );
// Obtain the first cell 4D tag in the third row starting from 0. Therefore, it is three.
Cell cell = row. getCell (3 );
// Check whether the cell is defined. If no definition is defined, NULL is returned.
//; Otherwise, a logical cell is returned. If not, a logical cell is created.
//?? Why do we need to judge if the cell is empty?
// It exists and has been defined. If you are creating, the original cell
// It will be overwritten by the cell you re-created, which can save
// Your space, because the object is not re-created
If (cell = null)
Cell = row. createCell (3 );
// Set the cell type to String text type.
Cell. setCellType (Cell. CELL_TYPE_STRING );
// Set the value
Cell. setCellValue ("a test ");

// Re-write a file such
FileOutputStream fileOut = new FileOutputStream ("D:/workbook.xls ");
Wb. write (fileOut );
FileOut. close (); after running:

 


Use the \ n line break in the cell:


[Java]
// Create an Excel file
Workbook wb = new HSSFWorkbook (); // or new XSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ();
// Create a row
Row row = sheet. createRow (2 );
// Create a cell
Cell cell = row. createCell (2 );
// Set the value in the cell to use the \ n linefeed.
Cell. setCellValue ("Use \ n with word wrap on to create a new line ");
 
// If you need a line break, you need to set
// The cell style wrap = true. The Code is as follows:
CellStyle cs = wb. createCellStyle ();
Cs. setWrapText (true );
Cell. setCellStyle (cs );
 
// Add the Row Height to adapt to the height of the two lines of text, and set the height unit (pixel)
Row. setHeightInPoints (2 * sheet. getDefaultRowHeightInPoints ()));
 
// Adjust the column width to adapt to the content
Sheet. autoSizeColumn (short) 2 );
// Write a file
FileOutputStream fileOut = new FileOutputStream ("ooxml-newlines.xlsx ");
Wb. write (fileOut );
FileOut. close ();

// Create an Excel file
Workbook wb = new HSSFWorkbook (); // or new XSSFWorkbook ();
// Create a workbook
Sheet sheet = wb. createSheet ();
// Create a row
Row row = sheet. createRow (2 );
// Create a cell
Cell cell = row. createCell (2 );
// Set the value in the cell to use the \ n linefeed.
Cell. setCellValue ("Use \ n with word wrap on to create a new line ");

// If you need a line break, you need to set
// The cell style wrap = true. The Code is as follows:
CellStyle cs = wb. createCellStyle ();
Cs. setWrapText (true );
Cell. setCellStyle (cs );

// Add the Row Height to adapt to the height of the two lines of text, and set the height unit (pixel)
Row. setHeightInPoints (2 * sheet. getDefaultRowHeightInPoints ()));

// Adjust the column width to adapt to the content
Sheet. autoSizeColumn (short) 2 );
// Write a file
FileOutputStream fileOut = new FileOutputStream ("ooxml-newlines.xlsx ");
Wb. write (fileOut );
FileOut. close (); after running:

 

Jar package required:

 
Author: yhc13429826359
 


 

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.