Java POI Excel( poi:純java操作excel的api )

POI官方網址:http://poi.apache.org/
POI的功能實在很強大,而且是apache的子項目,它下面又包含一些Component,比如處理Excel XLS,PowerPoint PPT,Word DOC,Outlook MSG,Excel XLSX等,下面就簡單講下poi處理excel的一些內容。

下面的jar包來源於當前最新的poi 3.6版本。

1.poi來生成excel

package com.test.poi;

import java.io.FileOutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;

public class WriteExcel {

public static void main(String[] args) throws Exception {
    // 創建Excel的工作書冊 Workbook,對應到一個excel文檔
    HSSFWorkbook wb = new HSSFWorkbook();

    // 創建Excel的工作sheet,對應到一個excel文檔的tab
    HSSFSheet sheet = wb.createSheet("sheet1");

    // 設置excel每列寬度
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // 創建字體樣式
    HSSFFont font = wb.createFont();
    font.setFontName("Verdana");
    font.setBoldweight((short) 100);
    font.setFontHeight((short) 300);
    font.setColor(HSSFColor.BLUE.index);

    // 創建單元格樣式
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // 設置邊框
    style.setBottomBorderColor(HSSFColor.RED.index);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);

    style.setFont(font);// 設置字體

    // 創建Excel的sheet的一行
    HSSFRow row = sheet.createRow(0);
    row.setHeight((short) 500);// 設定行的高度
    // 創建一個Excel的單元格
    HSSFCell cell = row.createCell(0);

    // 合併單元格(startRow,endRow,startColumn,endColumn)
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));

    // 給Excel的單元格設置樣式和賦值
    cell.setCellStyle(style);
    cell.setCellValue("hello world");

    // 設置單元格內容格式
    HSSFCellStyle style1 = wb.createCellStyle();
    style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));

    style1.setWrapText(true);// 自動換行

    row = sheet.createRow(1);

    // 設置單元格的樣式格式

    cell = row.createCell(0);
    cell.setCellStyle(style1);
    cell.setCellValue(new Date());

    // 創建超鏈接
    HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
    link.setAddress("
http://www.baidu.com");
    cell = row.createCell(1);
    cell.setCellValue("百度");
    cell.setHyperlink(link);// 設定單元格的鏈接

    FileOutputStream os = new FileOutputStream("e://workbook.xls");
    wb.write(os);
    os.close();
}

}

2.poi讀取excel
package com.test.poi;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcel {

public static void main(String[] args) throws Exception {
    HSSFWorkbook wb = null;
    POIFSFileSystem fs = null;
    try {
      fs = new POIFSFileSystem(new FileInputStream("e://workbook.xls"));
      wb = new HSSFWorkbook(fs);
    } catch (IOException e) {
      e.printStackTrace();
    }

    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(0);
    HSSFCell cell = row.getCell(0);
    String msg = cell.getStringCellValue();
    System.out.println(msg);
}
public static void method2() throws Exception {

    InputStream is = new FileInputStream("e://workbook.xls");
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));

    ExcelExtractor extractor = new ExcelExtractor(wb);
    extractor.setIncludeSheetNames(false);
    extractor.setFormulasNotResults(false);
    extractor.setIncludeCellComments(true);

    String text = extractor.getText();
    System.out.println(text);
}

public static void method3() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("e://workbook.xls"));
    HSSFSheet sheet = wb.getSheetAt(0);

    for (Iterator<Row> iter = (Iterator<Row>) sheet.rowIterator(); iter.hasNext();) {
      Row row = iter.next();
      for (Iterator<Cell> iter2 = (Iterator<Cell>) row.cellIterator(); iter2.hasNext();) {
        Cell cell = iter2.next();
        String content = cell.getStringCellValue();// 除非是sring類型,否則這樣迭代讀取會有錯誤
        System.out.println(content);
      }
    }
}
}

注:HSSFWorkbook,XSSFWorkbook的區別:前者是解析出來excel 2007 以前版本的,後綴名爲xls的,後者是解析excel 2007 版的,後綴名爲xlsx。

在實際應用中,要對excel文件進行判斷,該用哪個workbook來對其進行解析處理,而且,通常把這些方法都做了相應封裝,使其更面向對象,上例只是main方法的簡單示例而已,僅供參考!

 

轉自:http://apps.hi.baidu.com/share/detail/14791434

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章