(6) How to operate an Excel file with Apache POI-----Another bug related to POI-3.10 and annotations (comment)

Source: Internet
Author: User

If POI-3.10 inserts data into a worksheet (sheet), you need to be aware that it has a bug that is not easily discoverable. When the cell inside the inserted worksheet (sheet) does not contain any annotations (comment), inserting a row of data will not have any problems. However, if the cell inside the inserted worksheet (sheet) contains any annotations (comment), the file is broken when a row of data is inserted. When the program finishes executing, if you open the Excel file of the inserted data, we will find that it will pop up the following dialog box.

The program code is as follows

Package Com.tibco.poi.xssf;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException ; Import Java.io.fileoutputstream;import Java.io.ioexception;import Org.apache.poi.xssf.usermodel.xssfcell;import Org.apache.poi.xssf.usermodel.xssfrow;import Org.apache.poi.xssf.usermodel.xssfsheet;import Org.apache.poi.xssf.usermodel.xssfworkbook;public class Creatrowtest {//The current file already exists private String Excelpath = "d:\\ Exceltest\\comments.xlsx ";//Insert in the first few lines private int insertstartpointer = 3;//Insert this row of data in the worksheet of the current workbook private String SheetName = "Sheet1";/** * Total ingress method */public static void Main (string[] args) {creatrowtest crt = new Creatrowtest (); crt.inse Rtrows ();} /** * Entry method for inserting a new row of data into an existing Excel file */public void InsertRows () {Xssfworkbook wb = Returnworkbookgivenfilehandle (); Xssfsheet Sheet1 = Wb.getsheet (sheetname);    Xssfrow row = CreateRow (Sheet1, Insertstartpointer); Createcell (row); Saveexcel (WB);} /** * Save Workbook * @param wb */private void Saveexcel (Xssfworkbook wb) {FileOutputStream fileout;try {fileout = new FileOutputStream (Excelpath); Wb.write (fileout); Fileout.close ();} catch (    FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} /** * Create rows to be in and out of cells * @param row * @return */private Xssfcell Createcell (xssfrow row) {Xssfcell cell = row.c Reatecell ((short) 0); Cell.setcellvalue (999999); Row.createcell (1). Setcellvalue (1.2); Row.createcell (2).   Setcellvalue ("This is a string cell"); return cell;} /** * Get the POI object of an existing workbook * @return */private xssfworkbook returnworkbookgivenfilehandle () {Xssfworkbook WB = null;f Ileinputstream FIS = null; File F = new file (Excelpath), try {if (f! = null) {FIS = new FileInputStream (f); wb = new Xssfworkbook (FIS);}} catch (Except Ion e) {return null;} finally {if (FIS! = null) {try {fis.close ();} catch (IOException e) {e.printstacktrace ()}}}   return WB;} /** * Find the number of rows to insert and create a new POI Row Object * @param sheet * @param rowIndex * @return */private xssfrow createrow (XSSFS HSheet, Integer rowIndex) {xssfrow row = null;if (Sheet.getrow (rowIndex)! = null) {int lastrowno = Sheet.getlastrownum ( ); Sheet.shiftrows (RowIndex, Lastrowno, 1);} row = Sheet.createrow (rowIndex); return row;}}

However, it is gratifying that this bug has been resolved in the POI-3.12 version.

(6) How to operate an Excel file with Apache POI-----Another bug related to POI-3.10 and annotations (comment)

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.