(4) How to Use Apache POI to Operate Excel files ----- A regression bug in POI-3.12 was found,

Source: Internet
Author: User

(4) How to Use Apache POI to Operate Excel files ----- A regression bug in POI-3.12 was found,

It was hard to expect the final version of POI-3.12 to come out, and this month finally came out, so the first congestion was fast, download the latest POI-3.12 from this address (https://poi.apache.org/download.html#POI-3.12.

Then, I ran a program and added comments to the first cell (A1) in an Excel file. Unfortunately, the following error was thrown,

Exception in thread "main" java. lang. IllegalArgumentException: Multiple cell comments in one cell are not allowed, cell: A1
At org. apache. poi. xssf. usermodel. XSSFDrawing. createCellComment (XSSFDrawing. java: 318)
At org. apache. poi. xssf. usermodel. XSSFDrawing. createCellComment (XSSFDrawing. java: 52)
At com. tibco. poi. xssf. CellComments. main (CellComments. java: 49)


Use the same code and test it in POI-3.10 without any problems. Therefore, this is a regression problem. Below is the code to reproduce this problem (I used the code in my previous article, but I made a slight modification to the Code in line 33rd, and added the comment (comment) to cell A1 ). So what should we do if we encounter such a regression problem? At this time, we can create a bug for the Bugzilla System of Apache POI, so that they can solve it in the next version. For details about how to develop the bug of Bugzilla, refer to the next chapter.

Import org. apache. poi. ss. usermodel. *; import org. apache. poi. xssf. usermodel. XSSFSheet; import org. apache. poi. xssf. usermodel. XSSFWorkbook; import java. io. IOException; import java. io. fileOutputStream;/*** Demonstrates how to work with excel cell comments. * <p> * Excel comment is a kind of a text shape, * so inserting a comment is very similar to placing a text box in a worksheet * </p> ** @ author Yegor Kozlov */public class CellComments {public static void main (String [] args) throws IOException {// 1. create a workbook object XSSFWorkbook wb = new XSSFWorkbook (); // 2. obtain a POI tool class CreationHelper factory = wb. getCreationHelper (); // 3. create a worksheet XSSFSheet sheet = wb. createSheet (); // 4. obtain the Drawing drawing = sheet. createDrawingPatriarch (); // 5. clientAnchor is an object attached to a WorkSheet. It is fixed in the upper left corner and lower right corner of a cell. clientAnchor Chor = factory. createClientAnchor (); // 6. create a Cell (Cell A1) Cell cell0 = sheet. createRow (0 ). createCell (0); // 6.1. set cell0.setCellValue ("Test") for this cell; // 6.2. add Comment comment0 = drawing to this cell. createCellComment (anchor); RichTextString str0 = factory. createRichTextString ("Hello, World! "); Comment0.setString (str0); comment0.setAuthor (" Apache POI "); cell0.setCellComment (comment0); // 7. create a Cell (Cell 4F) Cell cell1 = sheet. createRow (3 ). createCell (5); // 7.1. set the cell value cell1.setCellValue ("F4"); // 7.2. add Comment comment1 = drawing to this cell. createCellComment (anchor); RichTextString str1 = factory. createRichTextString ("Hello, World! "); Comment1.setString (str1); comment1.setAuthor (" Apache POI "); cell1.setCellComment (comment1); // 8. create a Cell (Cell 4F) Cell cell2 = sheet. createRow (2 ). createCell (2); cell2.setCellValue ("C3"); Comment comment2 = drawing. createCellComment (anchor); RichTextString str2 = factory. createRichTextString ("XSSF can set cell comments"); // 9. Set Font font = wb for the annotation. createFont (); font. setFontName ("Arial"); font. setFontHeightInPoints (short) 14); font. setBoldweight (Font. BOLDWEIGHT_BOLD); font. setColor (IndexedColors. RED. getIndex (); str2.applyFont (font); comment2.setString (str2); comment2.setAuthor ("Apache POI"); comment2.setColumn (2); comment2.setRow (2); // 10. save it as an Excel file String fname = "comments.xlsx"; FileOutputStream out = new FileOutputStream (fname); wb. write (out); out. close ();}}




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.