十万火急!POI不能准确读取excel批注 求高人帮助!
试了很久都没法实现这个555 一开始还以为很简单的~ 详情如下 我用了POI读取excel单元格值和相应的批注。我是在工作表逐行循环,每一行再循环每个cell,
如果当前cell的批注不为空,就显示当前cell的内容和相应的批注,cell的内容的确是按每行搜索,
但是如果cell的单元格是公式,当读取当前cell的批注cell.getCellComment().getString().toString()并不是按每行搜索,而是按列搜索出来。我看过很多次代码都不清楚是哪里的问题, 请高手们帮忙下 先谢谢了!
excel中
结果
excel文件
程序代码:
1. import import import import import org.apache.poi.hssf.usermodel.HSSFWorkbook;
7. import org.apache.poi.hssf.usermodel.HSSFSheet;
8. import org.apache.poi.hssf.usermodel.HSSFCell;
9. import org.apache.poi.hssf.usermodel.HSSFRow;
10. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
11. import org.apache.poi.hssf.util.HSSFColor;
12. import org.apache.poi.ss.usermodel.Cell;
13. import org.apache.poi.ss.usermodel.CellStyle;
14. import org.apache.poi.ss.usermodel.CreationHelper;
15. import org.apache.poi.ss.usermodel.Font;
16. import org.apache.poi.ss.usermodel.IndexedColors;
17. import org.apache.poi.ss.usermodel.Row;
18. import org.apache.poi.ss.usermodel.Sheet;
19. import org.apache.poi.ss.usermodel.Workbook;
20. import org.apache.poi.ss.usermodel.WorkbookFactory;
21. import java.text.DecimalFormat;
22. import java.util.Date;
23. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
24.
25. public class readexcel {
26. public readexcel() {
27. }
28.
29. private String readformula(Cell cell){
30. DecimalFormat df = new DecimalFormat("0.000");
31. try{
32. return df.format(cell.getNumericCellValue());
33. }catch(Exception e){
34. return "";
35. }
36. }
37.
38. //读取有标注的单元格的字符串及相应标注
39. //@param filepath 文件路径
40. public void readDataFromExcel(String filepath){
41. String Strcell="";
42. String comment = "";
43. Sheet sheet=null;
44. try{
45. InputStream is = new FileInputStream(filepath);
46. //根据输入流创建Workbook对象
47. Workbook wb = WorkbookFactory.create(is);
48. //get到Sheet对象
49. int numsheet = wb.getNumberOfSheets();
50. for (int a = 0; a < numsheet; a++) { //循环表格
51. sheet = wb.getSheetAt(a);
52. //这个必须用接口
53. for (Row row : sheet) {
54. int filledColumns = row.getLastCellNum();
55. Cell cell = null;
56. // 循环遍历所有列
57. for (int i = 0; i < filledColumns; i++) {
58. // 取得当前Cell
59. cell = row.getCell((short) i);
60. if (cell!=null && cell.getCellComment() != null ) {
61. Strcell = "";
62. comment = "";
63.
64. //cell.getCellType是获得cell里面保存的值的type
65. //如Cell.CELL_TYPE_STRING
66. switch (cell.getCellType()) {
67. // 如果当前Cell的Type为NUMERIC
68. case HSSFCell.CELL_TYPE_NUMERIC:
69. // 判断当前的cell是否为Date
70. if (HSSFDateUtil.isCellDateFormatted(cell)) {
71. // 如果是Date类型则,取得该Cell的Date值
72. Date date = cell.getDateCellValue();
73. // 把Date转换成本地格式的字符串
74. Strcell = cell.getDateCellValue().toLocaleString();
75. }
76. // 如果是纯数字
77. else {
78. // 取得当前Cell的数值
79. Integer num = new Integer((int) cell
80. .getNumericCellValue());
81. Strcell = String.valueOf(num);
82. }
83. break;
84. case Cell.CELL_TYPE_BOOLEAN:
85.
86. //得到Boolean对象的方法
87. //cell.getBooleanCellValue();
88. break;
89. case Cell.CELL_TYPE_FORMULA:
90.
91. //读取公式
92. Strcell = readformula(cell);
93. break;
94. case Cell.CELL_TYPE_STRING:
95.
96. //读取String
97. Strcell = cell.getRichStringCellValue().toString();
98. break;
99.
100. }
101. comment = cell.getCellComment().getString().toString();
102. System.out.println(Strcell+","+comment);
103. }
104. }
105.
106. }
107.
108. }
109. is.close();
110.
111. }
112. catch(Exception e){
113. e.printStackTrace();
114. }
115.
116. }
117.
118.
119.
120. public static void main(String arg[]){
121. readexcel re=new readexcel();
122. re.readDataFromExcel("D:\\temp\\test.xls");
123. }
124. } 





