十万火急!POI不能准确读取excel批注 求高人帮助!
10-01-05
tanyongji
试了很久都没法实现这个555 一开始还以为很简单的~ 详情如下
我用了POI读取excel单元格值和相应的批注。我是在工作表逐行循环,每一行再循环每个cell,
如果当前cell的批注不为空,就显示当前cell的内容和相应的批注,cell的内容的确是按每行搜索,
但是如果cell的单元格是公式,当读取当前cell的批注cell.getCellComment().getString().toString()并不是按每行搜索,而是按列搜索出来。我看过很多次代码都不清楚是哪里的问题, 请高手们帮忙下 先谢谢了!
# 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"); # } # } <p class="indent"> |
excel中是:

结果:

excel文件
[该贴被tanyongji于2010-01-10 10:40修改过]