标签:
创建后缀为 .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
比较简单,不多说!
标签:
原文地址:http://my.oschina.net/u/233752/blog/509275