This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workb
生活随笔
收集整理的這篇文章主要介紹了
This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workb
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/
POI按照源單元格設置目標單元格格式??
2011-11-24 00:25:07|??分類: 日記 |??標簽:java??eclipse??javaexcel??poi?? |字號大中小?訂閱
poi按照一個源單元格設置目標單元格格式,如果兩個單元格不在同一個workbook, 要用 HSSFCellStyle下的cloneStyleFrom()方法, 而不能用 HSSFCell下的setCellStyle()方法。public void copyHssfRow(HSSFRow destRow, HSSFRow sourceRow){ int currentColumnNum = 0; HSSFCell tempCell = null; HSSFCellStyle tempCellStyle = null; System.out.println("ExcelHandler.java copy specific excel row starts------"); for(Iterator i = sourceRow.cellIterator(); i.hasNext();){ tempCell = (HSSFCell)i.next(); //確保這個destRow的待賦值cell有值 if(destRow.getCell(currentColumnNum) == null){ destRow.createCell(currentColumnNum); } destRow.getCell(currentColumnNum).setCellValue(tempCell.getStringCellValue()); //紅色字體的目的是把目的單元格的格式設置為源單元格的格式,這是正確的方式 tempCellStyle = tempCell.getCellStyle(); destRow.getCell(currentColumnNum).getCellStyle().cloneStyleFrom(tempCellStyle); System.out.println("DestCell name: " + destRow.getCell(currentColumnNum).getStringCellValue() + " SourceCell name: " + tempCell.getStringCellValue()); currentColumnNum++; } System.out.println("ExcelHandler.java copy specific excel row ends------"); }
最初紅色的代碼是這樣的: tempCellStyle = tempCell.getCellStyle(); destRow.getCell(currentColumnNum).setCellStyle(tempCellStyle); 然后出現了報錯: java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook.
再去google搜索這個報錯,發現poi手冊中HSSFCellStyle有一個方法會拋出這個異常。
verifyBelongsToWorkbook
public void verifyBelongsToWorkbook(HSSFWorkbook?wb) Verifies that this style belongs to the supplied Workbook. Will throw an exception if it belongs to a different one. This is normally called when trying to assign a style to a cell, to ensure the cell and the style are from the same workbook (if they're not, it won't work) Throws: java.lang.IllegalArgumentException?- if there's a workbook mis-match 再往下一看,就柳暗花明了。poi要求一個單元格的格式和單元格本身都在一個worksheet,不同的worksheet間不能共用HSSFCellStyle。想了下,這樣是對的,這樣就可以保證同一個style不會同時屬于兩個不同sheet的不同單元格,如果對一個單元格做了修改,另一個也會受到牽連。單元格之間的格式相當一個格式集,格式集中的參數數值我們可以一樣,但是我們的格式集不能是同一個。cloneStyleFrom
public void cloneStyleFrom(CellStyle?source)Clones all the style information from another HSSFCellStyle, onto this one. This HSSFCellStyle will then have all the same properties as the source, but the two may be edited independently. Any stylings on this HSSFCellStyle will be lost! The source HSSFCellStyle could be from another HSSFWorkbook if you like. This allows you to copy styles from one HSSFWorkbook to another.Specified by:cloneStyleFrom?in interface?CellStyle?
總結
以上是生活随笔為你收集整理的This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workb的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在PLSQL中,存储过程的输出参数(va
- 下一篇: 和我一起学 Selenium WebDr