java and excel

來源:互聯網
上載者:User

現在MS Excel在使用者中作為資料上傳和上報中使用的非常普遍,在java中處理excel是非常簡單的要求,但是j2se中沒有提供相關的類,Apache POI給我們提供了很好的一個工具。通過他來處理excel非常方便,重要的一點是他是open source的。我在這裡簡要的據一個demo描述怎樣讀excel裡面的資料。

import org.apache.poi.hssf.usermodel.*;
import java.io.FileInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class TestPOI
{

public static void main(String[] args) throws Exception{
POIFSFileSystem fs  =
 new POIFSFileSystem(new FileInputStream("book1.xls"));
 HSSFWorkbook wb = new HSSFWorkbook(fs);
 HSSFSheet sheet = wb.getSheetAt(0);
 HSSFRow row = sheet.getRow(2);
 HSSFCell cell = row.getCell((short)3);
String value = cell.getStringCellValue();
System.out.println(" the excel value is " + value);

}
}

在使用POI的時候覺得有一點不好就是如果儲存格的內容是數字,你使用getStringCellValue()的會有異常拋出,所以在用的時候先調用cell.getCellType(),根據cell的類型來調用不同的方法。

POI的網址在 http://jakarta.apache.org/poi/

除了POI外,還有F1 FOR JAVA處理EXCEL,不過他是商業的。功能挺強的,是很強的報表工具,地址在 http://www.reportingengines.com

還有jexcel也可以處理excel網址在http://threebit.net/projects/jexcel.shtml

提到處理excel的話,我覺得webofficecomponent不的不提,覺得他是很好的工具,他是M$提供的com控制項,在網頁中把一個table中的內容顯示在excel web控制項中間,並且可以直接倒出為excel,demo如下:

width="100%" height="90%">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.