POI

JAVA中操做Excel的有兩種比較主流的工具包: JXL 和 POI 。apache

  JXL只能操做Excel 95, 97, 2000也即以.xls爲後綴的excel。JXL的官網爲:http://www.andykhan.com/jexcelapi。而poi能夠操做Excel 95及之後的版本,便可操做後綴爲 .xls 和 .xlsx兩種格式的excel。api

  POI全稱 Poor Obfuscation Implementation,直譯爲「可憐的模糊實現」,利用POI接口能夠經過JAVA操做Microsoft office 套件工具的讀寫功能。官網:http://poi.apache.org ,POI支持office的全部版本。dom

  對於只操做2003 及之前版本的excel,只須要poi-3.10.1-20140818.jar ,若是須要同時對2007及之後版本進行操做則須要複製poi-ooxml-3.10.1-20140818.jar,poi-ooxml-schemas-3.10.1-20140818.jar,以及複製在ooxml-lib目錄下的xmlbeans-2.6.0.jar,dom4j-1.6.1.jar。工具

複製代碼
@Test
    //輸出03版本的excel
    public void test1() throws IOException {
        //建立工做簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //建立工做表
        HSSFSheet sheet = workbook.createSheet("工做簿1");
        //建立行
        HSSFRow row = sheet.createRow(3);
        //建立單元格,第三行第三列
        HSSFCell cell = row.createCell(3);

        cell.setCellValue("HelloWord!");
        //輸出到硬盤
        FileOutputStream outputStream = new FileOutputStream("d:\\test.xls");
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }

    @Test
    //讀取03版本的excel
    public void test2() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("d:\\test.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row = sheet.getRow(3);
        HSSFCell cell = row.getCell(3);

        System.out.println(cell.getStringCellValue());
        workbook.close();
        fileInputStream.close();
    }
    @Test
    //輸出07及以上版本excel
    public void test3() throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("表1");
        XSSFRow row = sheet.createRow(2);
        XSSFCell cell = row.createCell(2);
        cell.setCellValue("helloworld!");
        FileOutputStream outputStream = new FileOutputStream("d:\\test.xlsx");
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
複製代碼

讀取各類版本字體

複製代碼
    public void test4() throws IOException {
        String fileName = "d:\\test.xlsx";
        boolean is03Excel = true;
        FileInputStream fileInputStream = new FileInputStream(fileName);
        if(fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
        {
            if(fileName.matches("^.+\\.(?i)(xlsx)$"))
                is03Excel = false;
        }
        Workbook workbook = is03Excel?new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(3);
        Cell cell = row.getCell(3);

        System.out.println(cell.getStringCellValue());
        workbook.close();
        fileInputStream.close();
    }
複製代碼

設置樣式spa

複製代碼
public void test5() throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        //合併單元格
        CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 2, 2, 4);
        HSSFSheet sheet = workbook.createSheet("hello world!");
        sheet.addMergedRegion(cellRangeAddress);
        //設置居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //設置字體
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)20);
        cellStyle.setFont(font);
        //設置背景,先設置背景填充模式
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(HSSFColor.RED.index);

        HSSFRow row = sheet.createRow(2);
        HSSFCell cell = row.createCell(2);
        cell.setCellValue("hello world!");
        cell.setCellStyle(cellStyle);
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test1.xls");
        workbook.write(fileOutputStream);
        workbook.close();
        fileOutputStream.close();
    }
複製代碼
相關文章
相關標籤/搜索