用apache POI 操作 Excel有幾個關(guān)鍵的地方:
[1]讀文件流
這個問題是一個IO問題
InputStream in = new FileInputStream("/tmp/aaa.xls");
[2]如何取得Excel的操作對象
這個也就相當于,Excel的工作區(qū),在這個里面你可以取得當前excel文件的相關(guān)信息
??? ??? ??? ??? POIFSFileSystem poifs = new POIFSFileSystem(fis);
??? ??? ??? ??? HSSFWorkbook wb = new HSSFWorkbook(poifs);
HSSFWorkbook 對象,是我們最想得到的對象。
以后的所有操作都是從這里開始的。
[3]如何取得sheet的數(shù)目
wb.getNumberOfSheets()
[4]如何根據(jù)index取得sheet對象
??? ??? HSSFSheet sheet = wb.getSheetAt(0);
有了Sheet就相當于取得了一張表一樣。
[5]如何取得有效的行數(shù)
??? ??? int rowcount = sheet.getLastRowNum();
[6]如何根據(jù)index取得行對象
HSSFRow row = sheet.getRow(i);
有了行對象,就可以取得每一個單元對象
[7]如何知道一個行有多少個單元
colcount = row.getLastCellNum();
[8]如何取得一個單元對象
HSSFCell cell = row.getCell(j);
[9]如何取得單元的值
此處僅以字符串為例
??? ??? ??? ??? ??? if(cell!=null){
??? ??? ??? ??? ??? ??? ??? ??? System.out.println("cell is: "+cell.getStringCellValue());
??? ??? ??? ??? ??? ??? ??? }
下面是我的測試的完整的程序。我也是從網(wǎng)上找的資料,然后自己又做了測試。在此又做了整理。
感謝網(wǎng)上提供此參考資料的朋友。
package demo.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
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;
public class ExcelDemo {
??? public static void main(String[] args) {
??? ??? File f = new File("/home/zhangyi/dell500.xls");
??? ??? if (f.exists()) {
??? ??? ??? // read
??? ??? ??? try {
??? ??? ??? ??? InputStream fis = new FileInputStream(f);
??? ??? ??? ??? POIFSFileSystem poifs = new POIFSFileSystem(fis);
??? ??? ??? ??? HSSFWorkbook wb = new HSSFWorkbook(poifs);
??? ??? ??? ??? List retList = new ArrayList();
??? ??? ??? ??? System.out.println("sheet number : " + wb.getNumberOfSheets());
??? ??? ??? ???
??? ??? ??? ???
??? ??? ??? ??? HSSFSheet s = wb.getSheetAt(0);
??? ??? ??? ??? System.out.println("sheet obj is : "+s);
??? ??? ??? ???
??? ??? ??? ???
??? ??? ??? ???
??? ??? ??? ???
??? ??? ??? ??? for (int h = 0; h < wb.getNumberOfSheets(); ++h) {
??? ??? ??? ??? ??? List list = new ArrayList();
??? ??? ??? ??? ??? HSSFSheet sheet = wb.getSheetAt(h);
??? ??? ??? ??? ??? int rowcount = sheet.getLastRowNum();
??? ??? ??? ??? ??? rowcount++;
??? ??? ??? ??? ??? System.out.print("-----sheet[" + h + "]: row count = "
??? ??? ??? ??? ??? ??? ??? + rowcount);
??? ??? ??? ??? ???
??? ??? ??? ??? ??? int colcount = 0;
??? ??? ??? ??? ??? for (int i = 0; i < rowcount; ++i) {
??? ??? ??? ??? ??? ??? HSSFRow row = sheet.getRow(i); // i=0 indicate the first
??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? // row
??? ??? ??? ??? ??? ??? if (row == null)
??? ??? ??? ??? ??? ??? ??? continue; // without the row, break and continue;
??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? if (colcount == 0) { // colunm count set to column of
??? ??? ??? ??? ??? ??? ??? // the first effective row
??? ??? ??? ??? ??? ??? ??? colcount = row.getLastCellNum();
??? ??? ??? ??? ??? ??? ??? System.out.println(", column count = " + colcount);
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? String[] fieldValue = new String[colcount];
??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? for (short j = 0; j < colcount; ++j) { // column data in
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? // the current
??? ??? ??? ??? ??? ??? ??? HSSFCell cell = row.getCell(j);
??? ??? ??? ??? ??? ??? ??? // fieldValue[j] = getCellStringValue(cell);
??? ??? ??? ??? ??? ??? ??? if(cell!=null){
??? ??? ??? ??? ??? ??? ??? ??? System.out.println("cell is: "+cell.getStringCellValue());
??? ??? ??? ??? ??? ??? ??? }
//??? ??? ??? ??? ??? ??? ??? System.out.println("cell is : " +cell.getCellComment());
??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? list.add(fieldValue);
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? retList.add(list);
??? ??? ??? ??? }
??? ??? ??? } catch (FileNotFoundException e) {
??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? }
??? }
}
|----------------------------------------------------------------------------------------|
版權(quán)聲明 版權(quán)所有 @zhyiwww
引用請注明來源 http://m.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-06-24 16:10
zhyiwww 閱讀(670)
評論(0) 編輯 收藏