日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workb

發布時間:2024/4/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。