Poi operation Excel table

Posted by evanluke on Tue, 01 Feb 2022 12:11:32 +0100

The workbook is created by creating org apache. poi. ss. usermodel. Created by an instance of workbook. Either directly create a specific class (org.apache.poi.hssf.usermodel.HSSFWorkbook or org.apache.poi.xssf.usermodel.XSSFWorkbook), or use the convenient factory class org apache. poi. ss. usermodel. WorkbookFactory.

Worksheets are created by calling createSheet() from an existing instance of the Workbook, and the created worksheets are automatically added to the Workbook in order. The worksheet itself has no worksheet name (label at the bottom); You can call Workbook Setsheetname (sheetindex, "sheetname", encoding) to set the name associated with the worksheet. For HSSF, the name can be in 8-bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) or Unicode (HSSFWorkbook.ENCODING_UTF_16). The default encoding of HSSF is 8 bits per character. For XSSF, the name is automatically processed to Unicode.

Create a row by calling createRow(rowNumber) from an existing Sheet instance. Only rows with cell values should be added to the worksheet. To set the height of a row, you simply call setRowHeight(height) on the row object. The height must be in twips or 1 / 20 of the point. There is also a setrowhightinpoints method if you prefer.

Cells are created by calling createCell(column, type) from an existing row. Only cells with values should be added to the row. The cell type of the cell should be set to cell CELL_ TYPE_ Numeric or cell CELL_ TYPE_ Strings, depending on whether they contain numeric or text values. The cell must also have a value set. Set the value by calling setCellValue with String or double as a parameter. A single cell has no width; You must call setcolumnwidth (columnindex, width) (in 1 / 256 units of characters) on the Sheet object. (you cannot do this alone in the GUI).

Cells are styled using CellStyle objects, which in turn contain references to Font objects. These are created through the Workbook object by calling createCellStyle() and createFont(). After creating an object, you must set its parameters (color, border, etc.). To set the Font for CellStyle, call setFont(fontobj).

After generating the Workbook, you can write it out by calling write(outputStream) from the Workbook instance and passing it to OutputStream (such as FileOutputStream or ServletOutputStream). You must close OutputStream yourself. HSSF will not close it for you.

org.apache.poi.hssf.dev.HSSF test class

// Create a new file
FileOutputStream out = new FileOutputStream("workbook.xls");
// Create a new workbook
 workbook  wb = new HSSFWorkbook();
// Create a new worksheet
Sheet s = wb.createSheet();
// Declare a row object reference
 that 's ok r = Empty;
// Declare a cell object reference
 Cell c = Empty;
// Create 3 cell styles
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
CellStyle cs3 = wb.createCellStyle();
data format df = wb.createDataFormat();
// Create 2 font objects
 typeface f = wb.createFont();
typeface f2 = wb.createFont();
//Set font 1 to 12 point type
f.setFontHeightInPoints((short) 12);
//Make it blue
f.setColor((short)0xc);
// Bold
//arial is the default font
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
//Set font 2 to 10 point type
f2.setFontHeightInPoints((short) 10);
//Make it red
f2.setColor( (short)Font.COLOR_RED );
//Make it bold
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
f2.setStrikeout(true);
//Set cell style
cs.setFont(f);
//format cell
cs.setDataFormat(df.getFormat("#,##0.0"));
//Set thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color
cs2.setFillPattern((short) CellStyle.SOLID_FOREGROUND);
//To format cells as text, see data formatting for a complete list
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
//Set font
cs2.setFont(f2);
// Set sheet name in Unicode format
wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F" +
                   "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430");
// In the case of pure ascii
// wb.setSheetName(0, "HSSF Test");
// Create a worksheet with 30 rows (0-29)
int rownum;
for (rownum = (short) 0; rownum < 30; rownum++)
{
    // Create a row
    r = s.createRow(rownum);
    // Every other line
    If ((rownum % 2) == 0)
    {
        // Make row height larger (in twips - 1 / 20 point)
        r.setHeight((short)0x249);
    }
    //r.setRowNum((short) rownum);
    // Create 10 cells (0-9) (+ = 2), which becomes apparent later
    for (short cellnum = (short) 0; cellnum < 10; cellnum += 2)
    {
        // Create a numeric cell
        c = r.createCell(cellnum);
        // Do some stupid mathematical operations to demonstrate decimals
        c.setCellValue(rownum * 10000 + cellnum
                + (((double) rownum / 1000)
                + ((double) cellnum / 10000)));
        String cell value;
        // Create a string cell (see why + = 2
        c = r.createCell((short) (cellnum + 1));
        // Every other line
        If ((rownum % 2) == 0)
        {
            // Set this cell as the first cell style we defined
            c.setCellStyle(cs);
            // Set the string value of the cell to "Test"
            c.setCellValue("test");
        }
        other
        {
            c.setCellStyle(cs2);
            // Set the string value of the cell to "\ u0422\u0435\u0441\u0442"
            c.setCellValue("\u0422\u0435\u0441\u0442");
        }
        // Make this column wider
        s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
    }
}
//Use BLANKS to draw a thick black border on the bottom row
// Forward 2 lines
 Number of rows++;
Number of rows++;
r = s.createRow(rownum);
// Define the third style as the default
// Except for a thick black border at the bottom
cs3.setBorderBottom(cs3.BORDER_THICK);
//Create 50 cells
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
{
    //Create a blank type cell (no value)
    c = r.createCell(cellnum);
    // Set to thick black border style
    c.setCellStyle(cs3);
}
//End drawing thick black border
// Show me how to add / name and delete worksheets
// Create a worksheet, set its title, and then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
//End deleted sheet
// Write workbook to output stream
// Close our file (don't blow out our file handle)
wb.write(out);
Close ();

This article refers to the official API document of Apache for the purpose of recording relevant technologies. If there is infringement, please contact the author to delete it

Link: http://poi.apache.org/components/spreadsheet/how-to.html#user_api

Topics: Excel poi