@ 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