`
cynan168
  • 浏览: 37932 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

POI/JXL 对EXCEL操作

阅读更多

/**
     * 利用POI,解析EXCEL
     * @param filePath
     */
    public static void parseExcel(String filePath) {
        try {
            InputStream is = new FileInputStream(filePath);
            HSSFWorkbook workbook = new HSSFWorkbook(is);
            HSSFSheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum();// 行

            HSSFRow row;
            HSSFCell cell;
            String value = "";

            for (int i = 0; i <= rowNum; i++) {
                row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();// 列
                for (int j = 0; j < cellNum; j++) {
                    cell = row.getCell((short) j);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

                    int cellType = cell.getCellType();
                    if (cellType == cell.CELL_TYPE_NUMERIC) {
                        DecimalFormat format = new DecimalFormat("#");
                        value = format.format(cell.getNumericCellValue());
                    } else {
                        value = cell.getStringCellValue();
                    }
                    System.out.print(value + " ");
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在....");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

 

 

/**
     * JXL 读取 Excel 文件
     *
     * @throws IOException
     * @throws BiffException
     */
    public static String readExcelJXL() {
        File file = new File(path);
        StringBuffer sb = new StringBuffer();
        try {
            WorkbookSettings wks = new WorkbookSettings();
            wks.setEncoding("gbk");
            Workbook workbook = Workbook.getWorkbook(file,wks);
            Sheet sheet = workbook.getSheet(0);
            int colnum = sheet.getColumns();
            int rownum = sheet.getRows();
            for (int i = 0; i < colnum; i++) {
                for (int j = 0; j < rownum; j++) {
                    Cell cell = sheet.getCell(i, j);
                    sb.append(cell.getContents()+" ");
                }
                sb.append("<br>");
            }
            workbook.close();
        } catch (Exception e) {
            System.out.println("readLine err:" + e);
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

 

 

Jxl can't support Chinese.(I used JDK1.4)

POI no this problem.(I used JDK1.4)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics