excel 테이블 정보 요약, ExcelHandler

5619 단어
최근에 excel표의 내용을 요약하는 데 사용되는 작은 프로그램 (설계된 excel표) 을 썼다. 즉, 먼저 그들에게 특정한 excel 형식에 따라 기입하라고 요구한 다음에 제출하여 폴더에 넣은 다음에 그들의 정보를 한 표에 집합할 수 있다.그래서 POI에서 excel표의 조작을 부분적으로 봉인(string 형식만 봉인)해서 나중에 사용할 수 있도록 했습니다.
ExcelHandler 클래스는 다음과 같습니다.
/*
  @author wesleydeng 2011-9-6
*/
package POI;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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 ExcelHandler
{
	File file;
	HSSFWorkbook workbook;
	HSSFSheet sheet;

	public ExcelHandler(File file, int sheetIndex)// 
	{
		super();
		this.file=file;
		try
		{
			POIFSFileSystem inFS=new POIFSFileSystem(new FileInputStream(file));    
			workbook = new HSSFWorkbook(inFS);
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			System.out.println(file.getName());
			e.printStackTrace();
		}    
   
		sheet = workbook.getSheetAt(sheetIndex);    
	}
	 
	public String getStringValue(int rowIndex,int colIndex)
	{
		HSSFRow myRow=sheet.getRow(rowIndex);
		if(myRow==null)
		{
			throw new IllegalArgumentException("cells("+rowIndex+","+colIndex+") not exist!");
		}
		HSSFCell myCell=myRow.getCell(colIndex);
		if(myCell==null)
		{
			throw new IllegalArgumentException("cells("+rowIndex+","+colIndex+") not exist!");
		}
		
		return myCell.getStringCellValue();
	}
	
	public void setStringValue(int rowIndex,int colIndex,String value)
	{
		HSSFRow myRow=sheet.getRow(rowIndex);
		if(myRow==null)
		{
			myRow=sheet.createRow(rowIndex);
		}
		HSSFCell myCell=myRow.getCell(colIndex);
		if(myCell==null)
		{
			myCell=myRow.createCell(colIndex);
		}
		myCell.setCellValue(value);
	}
	
	public int getColLength(int rowIndex)
	{
		HSSFRow myRow=sheet.getRow(rowIndex);
		if(myRow==null)
		{
			throw new IllegalArgumentException("row:"+rowIndex+" not exist!");
		}
		return (int)sheet.getRow(rowIndex).getLastCellNum();
	}
	public void saveAndClose()
	{
		try
		{
			FileOutputStream outFile = new FileOutputStream(file);
			workbook.write(outFile);
			outFile.close();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static public File createNewFile(String fileFullPath, String sheetName)
	{
		//create file
		File file=new File(fileFullPath);
		try
		{
			file.createNewFile();
		} catch (IOException e1)
		{
			System.out.println("new file"+fileFullPath+"create Error");
		}
		
		//connect to file
		FileOutputStream outStream=null;
		try
		{
			outStream = new FileOutputStream(fileFullPath);
		} catch (FileNotFoundException e)
		{
			System.out.println(fileFullPath+"not found");
		}
		
		//write new Excelfile to file
		try
		{
			HSSFWorkbook myWorkbook=new HSSFWorkbook();
			myWorkbook.createSheet(sheetName);
			myWorkbook.write(outStream);
		} catch (IOException e)
		{
			System.out.println(fileFullPath+"write failed");
		}
		return file;
	}
	
	
}
 
이 ExcelHandler를 사용하는 것은 InfoCollector입니다.
/*
  @author wesleydeng 2011-9-5
*/
package POI;

import java.io.File;

public class InfoCollector
{
	public static void main(String[] args)
	{
	
	
	File folder=new File("C:\\Documents and Settings\\wesley\\ \\08 ");
	File[] excelFiles=folder.listFiles();
	
	ExcelHandler inExcelFile=null;
	File outfile=ExcelHandler.createNewFile(folder.getParent()+"\\ .xls", "sheet1");
	
	ExcelHandler outExcelFile=new ExcelHandler(outfile,0);
	String tempValue;
	//do loop to traverse each files
	for (int fileIndex = 0; fileIndex < excelFiles.length; fileIndex++)
	{
		inExcelFile=new ExcelHandler(excelFiles[fileIndex], 0);
		// colLength
		int colLength=inExcelFile.getColLength(0);
		if(fileIndex==0)// “ ”
		{
			for (int colIndex = 0; colIndex < colLength; colIndex++)
			{
				tempValue=inExcelFile.getStringValue(0, colIndex);
				outExcelFile.setStringValue(0, colIndex, tempValue);
			}
		}
		
		for (int colIndex = 0; colIndex < colLength; colIndex++)
		{
			// ( excel )
			tempValue=inExcelFile.getStringValue(1, colIndex);
			// fileindex+1 
			outExcelFile.setStringValue(fileIndex+1, colIndex, tempValue);
		}
	}
	System.out.println("OK");
	outExcelFile.saveAndClose();

	}
}

좋은 웹페이지 즐겨찾기