码迷,mamicode.com
首页 > 编程语言 > 详细

java读写excel之POI篇—002

时间:2015-09-21 19:52:24      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

poi创建excel文件

  • 创建后缀为 .xls excel文件

    @Test
    public void testCreateWorkbook_xls() {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
        HSSFSheet sheetOne = hssfWorkbook.createSheet();
        HSSFSheet sheetTwo = hssfWorkbook.createSheet();
        HSSFSheet sheetThree = hssfWorkbook.createSheet("三");
        //保存为excel文件,xls后缀
        FileOutputStream fileOutputStream = null;
        String filePath = "D:\\test.xls";
        try {
            fileOutputStream = new FileOutputStream(filePath);
            hssfWorkbook.write(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.toString());
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(e.toString());
            }
        }
    }

  • 创建后缀为 .xlsx excel文件

    //生成Workbook OOXML形式(.xlsx)
    @Test
    public void testCreateWorkbook_xlsx(){ 
        //保存为excel文件,xlsx后缀
        String filePath = "D:\\test.xlsx";
        FileOutputStream fileOutputStream = null;
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet xssfSheetOne = xssfWorkbook.createSheet();
        XSSFSheet xssfSheetTwo = xssfWorkbook.createSheet("two");
        try {
            fileOutputStream = new FileOutputStream(filePath);
            xssfWorkbook.write(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

使用版本为 poi-3.10.1

比较简单,不多说!

java读写excel之POI篇—002

标签:

原文地址:http://my.oschina.net/u/233752/blog/509275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!