---
title: "POI/JXL 对EXCEL操作"
author: "cynan168"
date: "2009-11-19T13:16:58Z"
tags: []
slug: "poi-jxl-excel"
draft: false
lastmod: "2009-11-19T13:16:58Z"
id: 520294
url: "https://cynan168.iteye.com/blog/520294"
---

/\**  
     * 利用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 &lt;= rowNum; i++) {  
                row = sheet.getRow(i);  
                int cellNum = row.getLastCellNum();// 列  
                for (int j = 0; j &lt; 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 &lt; colnum; i++) {  
                for (int j = 0; j &lt; rownum; j++) {  
                    Cell cell = sheet.getCell(i, j);  
                    sb.append(cell.getContents()+" ");  
                }  
                sb.append("&lt;br&gt;");  
            }  
            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)