POI使用
官方网址:http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbookBusy Developers' Guide to HSSF and XSSF Features
createPatriarch?on the?HSSFSheet?class. This has the effect erasing any other shape information stored in that sheet. By default POI will leave shape records alone in the sheet unless you make a call to this method.To create a shape you have to go through the following steps:
Create the patriarch.Create an anchor to position the shape on the sheet.Ask the patriarch to create the shape.Set the shape type (line, oval, rectangle etc...)Set any other style details converning the shape. (eg: line thickness, etc...)
createGroup()?and then creating the shapes using those groups.It's also possible to create groups within groups.
WarningAny group you create should contain at least two other shapes or subgroups.Here's how to create a shape group:
HSSFChildAnchor. What happens is that the created group has it's own coordinate space for shapes that are placed into it. POI defaults this to (0,0,1023,255) but you are able to change it as desired. Here's how:Graphics?and?Graphics2d.WarningIt's important to not however before continuing that?Graphics2d?is a poor match to the capabilities of the Microsoft Office drawing commands. The older?Graphics?class offers a closer match but is still a square peg in a round hole.All Graphics commands are issued into an?HSSFShapeGroup. Here's how it's done:
Graphics2d?object we create an EscherGraphics2d object and pass in the graphics object we created. Finally we call a routine that draws into the EscherGraphics2d object.The vertical points per pixel deserves some more explanation. One of the difficulties in converting Graphics calls into escher drawing calls is that Excel does not have the concept of absolute pixel positions. It measures it's cell widths in 'characters' and the cell heights in points. Unfortunately it's not defined exactly what type of character it's measuring. Presumably this is due to the fact that the Excel will be using different fonts on different platforms or even within the same platform.
Because of this constraint we've had to implement the concept of a verticalPointsPerPixel. This the amount the font should be scaled by when you issue commands such as drawString(). To calculate this value use the follow formula:
HSSFClientAnchor.getAnchorHeightInPoints().Many of the functions supported by the graphics classes are not complete. Here's some of the functions that are known to work.
- fillRect()fillOval()drawString()drawOval()drawLine()clearRect()
Functions that are not supported will return and log a message using the POI logging infrastructure (disabled by default).
createPicture()?on the drawing patriarch. At the time of writing the following types are supported:- PNGJPGDIB
It should be noted that any existing drawings may be erased once you add a image to a sheet.
WarningPicture.resize() works only for JPEG and PNG. Other formats are not yet supported.Reading images from a workbook:
WarningTo calculate column width Sheet.autoSizeColumn uses Java2D classes that throw exception if graphical environment is not available. In case if graphical environment is not available, you must tell Java that you are running in headless mode and set the following system property:java.awt.headless=true?.XSSF:
(Since POI-3.7)
Autofilters
Workbook wb = new HSSFWorkbook(); //or new XSSFWorkbook(); Sheet sheet = wb.createSheet(); sheet.setAutoFilter(CellRangeAddress.valueOf("C5:F200"));Conditional Formatting
Workbook workbook = new HSSFWorkbook(); // or new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting(); ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "0"); FontFormatting fontFmt = rule1.createFontFormatting(); fontFmt.setFontStyle(true, false); fontFmt.setFontColorIndex(IndexedColors.DARK_RED.index); BorderFormatting bordFmt = rule1.createBorderFormatting(); bordFmt.setBorderBottom(BorderFormatting.BORDER_THIN); bordFmt.setBorderTop(BorderFormatting.BORDER_THICK); bordFmt.setBorderLeft(BorderFormatting.BORDER_DASHED); bordFmt.setBorderRight(BorderFormatting.BORDER_DOTTED); PatternFormatting patternFmt = rule1.createPatternFormatting(); patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index); ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "-10", "10"); ConditionalFormattingRule [] cfRules = { rule1, rule2 }; CellRangeAddress[] regions = { CellRangeAddress.valueOf("A3:A5") }; sheetCF.addConditionalFormatting(regions, cfRules);