poi操做

今天看了一些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

相關文章
相關標籤/搜索