You are on page 1of 4

<?

php 
set_time_limit(0); 
error_reporting(E_ALL); 

#Set the workbook to use and its sheet. In this example we use a spreads
heet that 
#comes with the Excel installation called: SOLVSAMP.XLS 

$workbook = "C:\Program Files\Microsoft office\Office10\Samples\SOLVSAMP
.XLS"; 
$sheet = "Quick Tour"; 

#Instantiate the spreadsheet component. 
    $ex = new COM("Excel.sheet") or Die ("Did not connect"); 

#Get the application name and version     
    print "Application name:{$ex->Application->value}<BR>"; 
    print "Loaded version: {$ex->Application->version}<BR>"; 

#Open the workbook that we want to use. 
    $wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not 
open"); 

#Create a copy of the workbook, so the original workbook will be preserv
ed. 
    $ex->Application->ActiveWorkbook->SaveAs("Ourtest");   
    #$ex->Application->Visible = 1; #Uncomment to make Excel visible. 

# Read and write to a cell in the new sheet 
# We want to read the cell E11 (Advertising in the 4th. Quarter) 
    $sheets = $wkb->Worksheets($sheet);    #Select the sheet 
    $sheets->activate;                 #Activate it 
    $cell = $sheets->Cells(11,5) ;    #Select the cell (Row Column numbe
r) 
    $cell->activate;                #Activate the cell 
    print "Old Value = {$cell->value} <BR>";    #Print the value of the 
cell:10000 
    $cell->value = 15000;            #Change it to 15000 
    print "New value = {$cell->value}<BR> ";#Print the new value=15000 

#Eventually, recalculate the sheet with the new value. 
    $sheets->Calculate;            #Necessary only if calc. option is ma
nual 
#And see the effect on total cost(Cell E13) 
    $cell = $sheets->Cells(13,5) ;    #Select the cell (Row Column numbe
r) 
    $number = Number_format($cell->value); 
    print "New Total cost =\$$number - was \$47,732 before.<BR>"; 
#Should print $57,809 because the advertising affects the Corporate over
head in the 
# cell formula. 

#Example of use of the built-in functions in Excel: 
#Function: PMT(percent/12 months,Number of payments,Loan amount) 
    $pay = $ex->application->pmt(0.08/12,10,10000); 
    $pay = sprintf("%.2f",$pay); 
        print "Monthly payment for $10,000 loan @8% interest /10 months: 
\$ $pay<BR>"; 
#Should print monthly payment = $ -1,037.03     
    
#Optionally, save the modified workbook 
    $ex->Application->ActiveWorkbook->SaveAs("Ourtest");                 
      
#Close all workbooks without questioning 
    $ex->application->ActiveWorkbook->Close("False");     
    unset ($ex); 

?>

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
/**
* A simple POI example of opening an Excel spreadsheet
* and writing its contents to the command line.
* @author Tony Sintes
*/
public class POIExample {
public static void main( String [] args ) {
try {
InputStream input =
POIExample.class.getResourceAsStream( "qa.xls" );
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);

// Iterate over each row in the sheet


Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println( "Row #" + row.getRowNum() );

// Iterate over each cell in the row and print out the
cell's content
Iterator cells = row.cellIterator();
while( cells.hasNext() ) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println( "Cell #" + cell.getCellNum() );
switch ( cell.getCellType() ) {
case HSSFCell.CELL_TYPE_NUMERIC:

System.out.println( cell.getNumericCellValue() );
break;
case HSSFCell.CELL_TYPE_STRING:

System.out.println( cell.getStringCellValue() );
break;
default:
System.out.println( "unsuported sell
type" );
break;
}
}

} catch ( IOException ex ) {
ex.printStackTrace();
}
}
}

You might also like