最近有点无聊研究了下java的poi尝试做了下无课表的导入以及生成
这篇博客主要是稍微介绍下利用java如何实现excel的导入导出,如何生成excel
java操作excel有两种方法:POI和JXL
POI:
效率高,操作相对复杂,支持公式,宏,图像表,一些企业应用上会非常使用
能够修饰单元格属性
支持字体,数字,日期操作
JXL:
效率低
操作简单
部分支持
能够修饰单元格属性,格式支持不如POI强大
支持字体,数字,日期操作
这里围绕的是利用poi来操作
首先需要导入poi所需要的jar包:
poi-3.0.1.jar
poi-contrib-3.0.1-FINAL.jar
poi-scratchpad-3.0.1-FINAL.jar
或者maven配置如下
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
这样我们就可以开始excel练习了
Excel的生成(导出)
我们可以通过
HSSFWorkbook() wb = new HSSFWorkbook();
新建一个工作簿,每个Excel都有对应的表单,我们可以用上面的HSSFWorkbook对象来生成
新建一个表明为无课表的表单,这样我们就可以对这个表单进行操作了
HSSFSheet sheet = wb.createSheet("无课表");
在表单中创建行,如下面创建第行
HSSFRow row1 = sheet.createRow(0);
也可以在创建的第行中再创建列
HSSFCell cell = new row1.createCell(0);
我们可以用for循环把上面创建行列套进去,并且根据所需要的条件,这样我们就可以生成一张工作表
同样在行列对应的一个单元格内可以设置值,如在第0行第0列设置值:
cell.setCellValue("值在这");
以及可以将单元格合并,可以调用工作表里面的方法
sheet.addMergedRegion();传入一个CellRangeAddress对象,设置第几行到第几行,第几列到第几列合并在一起:(下面为这只第0行到第0行,第0列到第7列合并在一起)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
也可以给字体设置对应的颜色,大小,调用HSSFWorkbook对象的createCellStyle()方法
CellStyle style = wb.createCellStyle();
可以设置是否自动换行,垂直居中,水平居中
//设置自动换行
style.setWrapText(true);
// 水平居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFWorkbook对象调用字体的对象(来设置字体)
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 20);// 设置字体大小
//然后设置在上面那个style样式上
style.setFont(font);
最后把style样式设进对应的行列上就行啦
cell.setCellStyle(style);
利用java读取excel
其实跟上面写入类似,这次在创建HSSFWorkbook对象的时候需要传入对应的excel路径和工作表名称,用来生成可以读写的工作表
(这里读取的是d盘下的无课表excel里面的名为无课表的表单)
FileInputStream fis = new FileInputStream("d://无课表.xls");
wb = new HSSFWorkbook(fis);
// 获取excel里面的sheet
HSSFSheet sheet = wb.getSheet("无课表");
可以调用HSSFSheet的getrow(int x)方法getCell(int y)来获得表格里面行数列数对应的内容
//获得第0行
HSSFRow row = sheet.getRow(0);
//获得第0行的第0列(返回的是里面的内容Cell类型可以转成字符串)
row.getCell(0);
要设置值同样可以调用HSSFCell里面的.setCellValue(),如果要在内容上追加可以用
(给第0行第0列的元素追加内容)
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
cell.setCellValue(cell +","+ "追加的内容");
读入和写入最后都需要调用FileOutputStream把文件写入的硬盘中去
FileOutputStream output = new FileOutputStream("d://无课表.xls");
wb.write(output);//wb为上面新建的HSSFWorkbook()对象
output.close();