I'm using Apache POI and I need to put a border in a range of cells or merged region. I am merging the cells with three rows and five columns. But I am not able to add the border to it. So how do I do this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You will find the RegionUtil class useful for settings the borders for a range of cells.Look here:
http://poi.apache.org/apidocs/index.html
Use:
And use method:
See also "Using the Convenience Functions" on link:
https://poi.apache.org/spreadsheet/quick-guide.html#FooterPageNumbers
I recommend you use getMergedRegions, that method will return the list of merged regions in the sheet. Then you can iterate each region to apply borders. For example:
then, you can call to this method when you did all the merged that you need in mySheet.
First of all it would be nice to know which sheet format you are trying to create. Because when you merge null cells in HSSF it is totally normal while XSSF creates malformed file that cause errors while opening it in Microsoft EXCEL. Styles tend to behave equally in both cases. You need to assign same style (in your case style that includes the border) to each cell you are merging. My suggestion create a function that checks and corrects, sets style to all cells in the merging region. Here is example of my own:
. . .
. . .
And finally you call it before actual merge call something like this:
Hope it helps.
My solution was to merge the cells by their positions, then created a cell (reference to the first block of the merged cells) to assign a value and then set the border throught the HSSFRegionUtil