java應用poi的例子

此例子是一個導入導入excel的程序,前題是必須要有poi包(poi,poi-contrib,poi-scratchpad),明確excel文件包含sheet,sheet包括row和cell
1.將包中需要用到的功能做一個程序封裝起來,以方便自己使用
ConvertXLS .java
package utils.xls;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.FileOutputStream;
import java.io.*;
 
public class ConvertXLS {
    private String xlsfilename="";
    private HSSFSheet sheet=null;
    private HSSFWorkbook wb = null;
    private ConvertXLS(String xlsfilename) {
        POIFSFileSystem fs = null;
        try {
            fs = new POIFSFileSystem(new FileInputStream(xlsfilename));
            wb = new HSSFWorkbook(fs);
        } catch (IOException ex) {
            System.out.println("ERR >> an error in constructor of ConvertXLS !");
            ex.printStackTrace();
        }
    }
    //
    private ConvertXLS(InputStream is) {
        POIFSFileSystem fs = null;
        try {
            fs = new POIFSFileSystem(is);
            wb = new HSSFWorkbook(fs);
        } catch (IOException ex) {
            System.out.println("ERR >> an error in constructor of ConvertXLS !");
            ex.printStackTrace();
        }
    }
    //
    private ConvertXLS(String xlsfilename,int flag) {
        this.xlsfilename=xlsfilename;
        wb=new HSSFWorkbook();
    }
    //根據sheet號得到sheet
    private void getSheet(int sheetunit){
       sheet = wb.getSheetAt(sheetunit);
    }
    //read from xls file
    public static ConvertXLS getReadConvertXLS(String xlsfilename){
        ConvertXLS cxls=new ConvertXLS(xlsfilename);
        return cxls;
    }
    //
    public static ConvertXLS getReadConvertXLS(InputStream is){
        ConvertXLS cxls=new ConvertXLS(is);
        return cxls;
    }
    //write into xls file
    public static ConvertXLS getWriteConvertXLS(String xlsfilename){
        return new ConvertXLS(xlsfilename,0);
    }
    //以上各方法是爲了得到excel文件對象,着情使用
    //數據二維表格式的數據集導入到命名的sheet中()
    public void setData(String sheetname,String[][] data){
        try {
            FileOutputStream fileOut = new FileOutputStream(xlsfilename);
            HSSFSheet sheet=wb.createSheet(sheetname);
            for(short i=0;i<data.length;i++){
                HSSFRow row=sheet.createRow(i);
                for(short j=0;j<data[0].length;j++){
                    HSSFCell cell=row.createCell(j);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue(data[i][j]);
                }
            }
            wb.write(fileOut);
        } catch (FileNotFoundException ex) {
            System.out.println("ERR >> an error in method setData of class ConvertXLS !");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("ERR >> an error in method setData of class ConvertXLS !");
            ex.printStackTrace();
        }
    }
    //數據二維表格式的數據集導入到相應索引的sheet中
    public String[][] getData(int sheetunit,int columncount){
        int firstrow,lastrow,firstcol;
        getSheet(sheetunit);
        firstrow=sheet.getFirstRowNum();
        lastrow=sheet.getLastRowNum();
        HSSFRow row=null;
        if(lastrow-firstrow > 0)
            row = sheet.getRow(firstrow);
        if (row == null)
            row = sheet.createRow(firstrow);
        firstcol=row.getFirstCellNum();
        return getData(firstrow,lastrow,firstcol,firstcol+columncount-1);
    }
    //從相應索引sheet中將表格數據導出並在於二維數組中
    public String[][] getData(int sheetunit){
        int firstrow,lastrow,firstcol,lastcol;
        getSheet(sheetunit);
        firstrow=sheet.getFirstRowNum();
        lastrow=sheet.getLastRowNum();
        HSSFRow row=null;
        if(lastrow-firstrow >= 0)
            row = sheet.getRow(firstrow);
        else
            return null;
        if (row == null)
            row = sheet.createRow(firstrow);
        firstcol=row.getFirstCellNum();
        lastcol=row.getLastCellNum()-1;
        return getData(firstrow,lastrow,firstcol,lastcol);
    }
    //根據指定的行列將相應索引sheet中的表格數據導出到二維數組中
    public String[][] getData(int sheetunit,int startrow,int endrow,int startcolumn,int endcolumn){
        getSheet(sheetunit);
        return getData(startrow,endrow,startcolumn,endcolumn);
    }
    //
    private String[][] getData(int startrow,int endrow,int startcolumn,int endcolumn){
        HSSFRow row=null;
        HSSFCell cell=null;
        String data[][]=new String[endrow-startrow+1][endcolumn-startcolumn+1];
        for(int i=0;i<data.length;i++){
            row=sheet.getRow(i);
            for(short j=0;j<data[0].length;j++){
                cell = row.getCell(j);
                data[i][j]=new String();
                switch(cell.getCellType()){
                   case 0 :{
                       if(cell.getCellStyle().getIndex()==(short)22){
                           DateFormat d=new SimpleDateFormat("yyyy-MM-dd");
//                                   HSSFDataFormat.getBuiltinFormat(cell.getCellStyle().getDataFormat()));
                           if(cell.getDateCellValue()!=null)
                               data[i][j] = d.format(cell.getDateCellValue());
                       }
                       else
                           data[i][j] = Double.toString(cell.getNumericCellValue());
                   }
                       break;
                   default:data[i][j]=cell.getStringCellValue();
                       break;
                }
            }
        }
        return data;
    }
}
 
2.應用例子
ConvertXLS cl=ConvertXLS.getReadConvertXLS("1234.xls");//得到excel文件對象,用於讀數據
String s[][]=cl.getData(0);//取得0sheet中的數據
cl=ConvertXLS.getWriteConvertXLS("4321.xls");//得到新excel文件對象,用於寫數據
cl.setData("sh",s);//將1234.xls中得到的數據導入到4321.xls中
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章