当前位置:编程学习 > JAVA >>

十万火急!POI不能准确读取excel批注 求高人帮助!

试了很久都没法实现这个555 一开始还以为很简单的~ 详情如下                      
 我用了POI读取excel单元格值和相应的批注。我是在工作表逐行循环,每一行再循环每个cell,              
如果当前cell的批注不为空,就显示当前cell的内容和相应的批注,cell的内容的确是按每行搜索,
但是如果cell的单元格是公式,当读取当前cell的批注cell.getCellComment().getString().toString()并不是按每行搜索,而是按列搜索出来。我看过很多次代码都不清楚是哪里的问题, 请高手们帮忙下 先谢谢了! 

excel中:


excel文件:
http://dl.javaeye.com/topics/download/c870f8fc-0565-36e9-a077-fb73656453f5

显示结果:



代码:

# import java.io.InputStream;  
# import java.io.FileInputStream;  
# import java.io.FileInputStream;  
# import java.io.InputStream;  
#   
# import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
# import org.apache.poi.hssf.usermodel.HSSFSheet;  
# import org.apache.poi.hssf.usermodel.HSSFCell;  
# import org.apache.poi.hssf.usermodel.HSSFRow;  
# import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
# import org.apache.poi.hssf.util.HSSFColor;  
# import org.apache.poi.ss.usermodel.Cell;  
# import org.apache.poi.ss.usermodel.CellStyle;  
# import org.apache.poi.ss.usermodel.CreationHelper;  
# import org.apache.poi.ss.usermodel.Font;  
# import org.apache.poi.ss.usermodel.IndexedColors;  
# import org.apache.poi.ss.usermodel.Row;  
# import org.apache.poi.ss.usermodel.Sheet;  
# import org.apache.poi.ss.usermodel.Workbook;  
# import org.apache.poi.ss.usermodel.WorkbookFactory;  
# import java.text.DecimalFormat;  
# import java.util.Date;  
# import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
#   
# public class readexcel {  
#   public readexcel() {  
#   }  
#     
#   private String readformula(Cell cell){  
#       DecimalFormat df = new DecimalFormat("0.000");  
#    try{  
#      return df.format(cell.getNumericCellValue());  
#    }catch(Exception e){  
#    return "";  
#    }  
#  }  
#   
#      //读取有标注的单元格的字符串及相应标注  
#   //@param filepath 文件路径  
#  public void readDataFromExcel(String filepath){  
#       String Strcell="";  
#       String comment = "";  
#       Sheet sheet=null;  
#       try{  
#         InputStream is = new FileInputStream(filepath);  
#         //根据输入流创建Workbook对象  
#         Workbook wb = WorkbookFactory.create(is);  
#         //get到Sheet对象  
#         int numsheet = wb.getNumberOfSheets();  
#         for (int a = 0; a < numsheet; a++) { //循环表格  
#           sheet = wb.getSheetAt(a);  
# //这个必须用接口  
#           for (Row row : sheet) {  
#             int filledColumns = row.getLastCellNum();  
#                Cell cell = null;  
#                // 循环遍历所有列  
#                      for (int i = 0; i < filledColumns; i++) {  
#                         // 取得当前Cell  
#                 cell = row.getCell((short) i);  
#               if (cell!=null && cell.getCellComment() != null ) {  
#                 Strcell = "";  
#                 comment = "";  
#   
#                 //cell.getCellType是获得cell里面保存的值的type  
#                 //如Cell.CELL_TYPE_STRING  
#                 switch (cell.getCellType()) {  
#                   // 如果当前Cell的Type为NUMERIC  
#                   case HSSFCell.CELL_TYPE_NUMERIC:   
#               // 判断当前的cell是否为Date  
#                     if (HSSFDateUtil.isCellDateFormatted(cell)) {  
#                  // 如果是Date类型则,取得该Cell的Date值  
#                       Date date = cell.getDateCellValue();  
#                  // 把Date转换成本地格式的字符串  
#                       Strcell = cell.getDateCellValue().toLocaleString();  
#                        }  
#               // 如果是纯数字  
#                    else {  
#                  // 取得当前Cell的数值  
#                       Integer num = new Integer((int) cell  
#                        .getNumericCellValue());  
#                       Strcell = String.valueOf(num);  
#                        }  
#                     break;  
#                   case Cell.CELL_TYPE_BOOLEAN:  
#   
#                     //得到Boolean对象的方法  
#                     //cell.getBooleanCellValue();  
#                     break;  
#                   case Cell.CELL_TYPE_FORMULA:  
#   
#                     //读取公式  
#                     Strcell = readformula(cell);  
#                     break;  
#                   case Cell.CELL_TYPE_STRING:  
#   
#                     //读取String  
#                     Strcell = cell.getRichStringCellValue().toString();  
#                     break;  
#                       
#                 }  
#                 comment = cell.getCellComment().getString().toString();  
#                 System.out.println(Strcell+","+comment);  
#               }  
#             }  
#   
#           }  
#   
#         }  
#         is.close();  
#   
#       }  
#       catch(Exception e){  
#          e.printStackTrace();  
#       }  
#   
#  }  
#   
#   
#     
#   public static void main(String arg[]){  
#     readexcel re=new readexcel();  
#     re.readDataFromExcel("D:\\temp\\test.xls");  
#   }  
# } 
--------------------编程问答-------------------- 看不到 图哦 , 用过poi 用过jxl  感觉jxl比poi简单好用很多。。。。。。 --------------------编程问答-------------------- jxl吧 

poi什么破玩意 --------------------编程问答-------------------- 用过jxl,没用过poi帮顶一下  --------------------编程问答-------------------- 帮顶
现在用的都是jxl --------------------编程问答-------------------- 帮顶
学习 --------------------编程问答-------------------- FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();//公式值工具
CellValue cellValue = evaluator.evaluate(cell);
if(cell.getCellType()==HSSFCell.CELL_TYPE_FORMULA){
   switch (cellValue.getCellType()) {
   case HSSFCell.CELL_TYPE_BOOLEAN:
           value=cellValue.getBooleanValue();
           break;
   case HSSFCell.CELL_TYPE_NUMERIC:
           value=cellValue.getNumberValue();
           break;
   case HSSFCell.CELL_TYPE_STRING:
           value=cellValue.getStringValue();
           break;
   case HSSFCell.CELL_TYPE_BLANK:
           value="";
           break;
   case HSSFCell.CELL_TYPE_ERROR:
           value="";
           break;       
          // CELL_TYPE_FORMULA will never happen
   case HSSFCell.CELL_TYPE_FORMULA:
           value="";
           break;
   }
}

此时的value就是有公式的单元格的值
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,