今天看了一些POI的操做知识,本身就随手写了一个小例子,例子简单,还未深刻去学习,由于在公司的项目中就有导出excel表的功能,我也只是在老员工的基础上本身修改过这方面的功能,可是本身写起来估计就远不如人家已经搭建起来的功能了。因此今天学了一些基础方面的东西,往后还会深刻去学习的!java
我是使用Maven来管理项目的,因此不是导入包,而是配置了pom.xml文件。apache
<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> </dependency> </dependencies>
随后新建了一个类写代码操做:函数
createEx.java学习
package poi; import org.apache.poi.hssf.usermodel.*; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Created by Administrator on 2016/1/17. */ public class createEx { public static String outfile = "D:\\test.xls"; public static void main(String[] args) { HSSFWorkbook workbook = new HSSFWorkbook();//创建excel文件 HSSFFont f = workbook.createFont(); HSSFCellStyle cellStyle = workbook.createCellStyle(); f.setBold(true); f.setColor(HSSFFont.COLOR_RED); cellStyle.setFont(f); HSSFSheet sheet = workbook.createSheet("排名");//创建工做表 HSSFRow row = sheet.createRow((short) 0);//经过索引0建立行 HSSFCell cell = row.createCell(0);//建立单元格 cell.setCellType(HSSFCell.CELL_TYPE_STRING);//单元格格式 cell.setCellValue("第一名");//往单元格中放入值 cell.setCellStyle(cellStyle); try { FileOutputStream outputStream = new FileOutputStream(outfile); workbook.write(outputStream); outputStream.flush(); outputStream.close(); System.out.println("文件生成"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
关于poi操做excel的API比较简单,主要是这几个:spa
HSSFWorkbook(excel文件)、HSSFSheet(一个个工做表)、HSSFRow(每一行)、HSSFCell(单元格)。excel
搞清楚这些所表明的意思就能够很快的操做excel表了,都有相应的操做函数,只须要认真阅读API就好。code