主题:POI 使用总结

http://www.iteye.com/topic/211707

 

主题:POI 使用总结

 

之前用POI做了一段时间的报表,也对POI的一些方法重写了,用起来还算方便。也没什么特别的,主要就是对HSSFWorkbook,HSSFSheet,HSSFRow, HSSFCell的操作了,掌握了对这四个东西的控制,你想怎么写就怎么写。
1,首先写一个abstract class用来overwrite HSSF。

Java代码 复制代码
  1. package  com.eagle.excel;  
  2.   
  3. import  java.util.ArrayList;  
  4.   
  5. import  org.apache.poi.hssf.usermodel.HSSFCell;  
  6. import  org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  7.   
  8. /**  
  9.  * @author eyas  
  10.  * @version 1.0  
  11.  * @date Apr 28, 2007 10:41:26 PM  
  12.  */   
  13. public   abstract   class  ExportInfoToExcel  
  14. {     
  15.         /**  
  16.          * create report header  
  17.         */   
  18.     protected   abstract   void  createHead();  
  19.   
  20.         /**  
  21.          *create report body  
  22.          *columns:table header  
  23.          *itemList:row data  
  24.          */   
  25.     protected   abstract   void  createBody(String[] columns, ArrayList itemlist);  
  26.   
  27.         /**  
  28.          *create report footer, ex:someone total information.  
  29.          */   
  30.     protected   abstract   void  createFoot();  
  31.            
  32.         /**  
  33.          * write data to a Excel cell.this method is overloadabled.  
  34.          */   
  35.     public   void  createCell(HSSFCellStyle cellStyle, HSSFCell cell, String  value)  
  36.     {  
  37.         cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
  38.         cell.setCellStyle(cellStyle);  
  39.         cell.setCellValue(value);  
  40.     }  
  41. }  
package com.eagle.excel;

import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;

/**
 * @author eyas
 * @version 1.0
 * @date Apr 28, 2007 10:41:26 PM
 */
public abstract class ExportInfoToExcel
{	
        /**
         * create report header
        */
	protected abstract void createHead();

        /**
         *create report body
         *columns:table header
         *itemList:row data
         */
	protected abstract void createBody(String[] columns, ArrayList itemlist);

        /**
         *create report footer, ex:someone total information.
         */
	protected abstract void createFoot();
         
        /**
         * write data to a Excel cell.this method is overloadabled.
         */
	public void createCell(HSSFCellStyle cellStyle, HSSFCell cell, String  value)
	{
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellStyle(cellStyle);
		cell.setCellValue(value);
	}
}



2,再写一个interface 用来给Excel 传递数据,并生成Excel file,

Java代码 复制代码
  1. package  com.eagle.excel;  
  2.   
  3. import  java.io.File;  
  4. import  java.util.ArrayList;  
  5. import  java.util.Calendar;  
  6.   
  7. /**  
  8.  * @author eyas  
  9.  * @version 1.0  
  10.  * @date Apr 28, 2007 10:41:26 PM  
  11.  */   
  12. public   interface  ExportInfoInterface  
  13. {  
  14.   
  15.     public   void  setDate(Calendar startDate);  // report print date   
  16.     public   void  setColumns(String[] columns);  // report columns   
  17.   
  18.     public   void  setDataList(ArrayList itemList);  //data   
  19.   
  20.     public   void  produceExportFile(File exportfile)  throws  Exception; //create excel file   
  21.     public   void  setOtherArg(Object arg1); // other arguments   
  22. }  
package com.eagle.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * @author eyas
 * @version 1.0
 * @date Apr 28, 2007 10:41:26 PM
 */
public interface ExportInfoInterface
{

	public void setDate(Calendar startDate); // report print date
	public void setColumns(String[] columns); // report columns

	public void setDataList(ArrayList itemList); //data

	public void produceExportFile(File exportfile) throws Exception;//create excel file
	public void setOtherArg(Object arg1);// other arguments
}



3,然后就可以写一个类,用来接收数据,extends ,implements 上面的abstract class and interface.

Java代码 复制代码
  1. package  com.eagle.excel;  
  2.   
  3. import  java.io.File;  
  4. import  java.io.FileOutputStream;  
  5. import  java.io.IOException;  
  6. import  java.util.ArrayList;  
  7. import  java.util.Calendar;  
  8. import  org.apache.poi.hssf.usermodel.HSSFRow;  
  9. import  org.apache.poi.hssf.usermodel.HSSFSheet;  
  10. import  org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  11. import  org.apache.poi.hssf.util.Region;  
  12.   
  13. /**  
  14.  * @author eyas  
  15.  * @version 1.0  
  16.  * @date Apr 28, 2007 10:43:26 PM  
  17.  */   
  18. public   class  CreateExcelFile  extends  ExportInfoToExcel  implements  ExportInfoInterface  
  19. {  
  20.     private  HSSFWorkbook workbook;  
  21.     private  HSSFSheet sheet;  
  22.     private  ArrayList dataList;  
  23.     private  Calendar date;  
  24.         private  String[] columns;  
  25.         private   int  lineX =  0 ;  
  26.     public  CreateExcelFile ()  
  27.     {  
  28.            //initialized someone arguments   
  29.     }  
  30.   
  31.     @Override   
  32.     protected   void  createBody()  
  33.     {     
  34.         HSSFRow bodyTile = sheet.createRow(lineX++);  
  35.         for  ( int  i =  0 ; i < columns.length; i++)  
  36.         {  
  37.             createCell(bodyTile.createCell((short ) i), columns[i]);  
  38.         }  
  39.         HSSFRow bodyRow;  
  40.         for  ( int  i =  0 ; i < dataList.size(); i++)  
  41.         {  
  42.             bodyRow = sheet.createRow(lineX++);  
  43.                     createCell(null ,bodyRow.createCell(( short 0 ),(i+ 1 ));  
  44.                     createCell(null ,bodyRow.createCell(( short 1 ), "eyas" );  
  45.                     createCell(null ,bodyRow.createCell(( short 2 ), "software" );  
  46.                         ...  
  47.         }  
  48.     }  
  49.   
  50.     @Override   
  51.     protected   void  createFoot()  
  52.     {  
  53.         HSSFRow totalRow = sheet.createRow(lineX++);  
  54.                 createCell(null ,bodyRow.createCell(( short 0 ), "total data" );  
  55.     }  
  56.   
  57.     @Override   
  58.     protected   void  createHead()  
  59.     {  
  60.         lineX = 0 ;  
  61.         sheet.addMergedRegion(new  Region( 0 , ( short 0 0 , ( short 12 ));  
  62.         HSSFRow headRow0 = sheet.createRow(lineX++);  
  63.         createCell(null ,headRow0.createCell(( short 0 ),  "My Excel Report" );  
  64.   
  65.     }  
  66.   
  67.     public   void  produceExportFile(File exportfile)  throws  Exception  
  68.     {  
  69.         try   
  70.         {  
  71.             workbook = new  HSSFWorkbook();  
  72.             sheet = workbook.createSheet("First Page" );  
  73.             createHead();  
  74.             createBody();  
  75.             FileOutputStream fw = new  FileOutputStream(exportfile);  
  76.             workbook.write(fw);  
  77.             fw.flush();  
  78.             fw.close();  
  79.             workbook = null ;  
  80.             sheet = null ;  
  81.             fw = null ;  
  82.                          System.out.println("Export success!" );  
  83.         } catch  (IOException ioe)  
  84.         {  
  85.             ioe.printStackTrace();  
  86.         }catch  (Exception e)  
  87.         {  
  88.             e.printStackTrace();  
  89.         }  
  90.     }  
  91.   
  92.     public   void  setColumns(String[] columns)  
  93.     {  
  94.         this .columns = columns;  
  95.     }  
  96.   
  97.     public   void  setDataList(ArrayList dataList)  
  98.     {  
  99.         this .dataList = dataList;  
  100.     }  
  101.   
  102.     public   void  setOtherArg(Object arg1)  
  103.     {  
  104.   
  105.     }  
  106.   
  107.     public   void  setDate(Calendar date)  
  108.     {  
  109.         this .date = date;  
  110.     }  
  111.   
  112. }  
package com.eagle.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
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.Region;

/**
 * @author eyas
 * @version 1.0
 * @date Apr 28, 2007 10:43:26 PM
 */
public class CreateExcelFile extends ExportInfoToExcel implements ExportInfoInterface
{
	private HSSFWorkbook workbook;
	private HSSFSheet sheet;
	private ArrayList dataList;
	private Calendar date;
        private String[] columns;
        private int lineX = 0;
	public CreateExcelFile ()
	{
           //initialized someone arguments
	}

	@Override
	protected void createBody()
	{	
		HSSFRow bodyTile = sheet.createRow(lineX++);
		for (int i = 0; i < columns.length; i++)
		{
			createCell(bodyTile.createCell((short) i), columns[i]);
		}
		HSSFRow bodyRow;
		for (int i = 0; i < dataList.size(); i++)
		{
		    bodyRow = sheet.createRow(lineX++);
                    createCell(null,bodyRow.createCell((short) 0),(i+1));
                    createCell(null,bodyRow.createCell((short) 1),"eyas");
                    createCell(null,bodyRow.createCell((short) 2),"software");
                        ...
		}
	}

	@Override
	protected void createFoot()
	{
		HSSFRow totalRow = sheet.createRow(lineX++);
                createCell(null,bodyRow.createCell((short) 0),"total data");
	}

	@Override
	protected void createHead()
	{
		lineX = 0;
		sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 12));
		HSSFRow headRow0 = sheet.createRow(lineX++);
		createCell(null,headRow0.createCell((short) 0), "My Excel Report");

	}

	public void produceExportFile(File exportfile) throws Exception
	{
		try
		{
			workbook = new HSSFWorkbook();
			sheet = workbook.createSheet("First Page");
			createHead();
			createBody();
			FileOutputStream fw = new FileOutputStream(exportfile);
			workbook.write(fw);
			fw.flush();
			fw.close();
			workbook = null;
			sheet = null;
			fw = null;
                         System.out.println("Export success!");
		} catch (IOException ioe)
		{
			ioe.printStackTrace();
		}catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void setColumns(String[] columns)
	{
		this.columns = columns;
	}

	public void setDataList(ArrayList dataList)
	{
		this.dataList = dataList;
	}

	public void setOtherArg(Object arg1)
	{

	}

	public void setDate(Calendar date)
	{
		this.date = date;
	}

}


仅对产生Excel报表,使用POI几个步骤的总结,不能保证代码完全 runable,如有改进建议,敬请发表。

你可能感兴趣的:(java,apache,Excel)