Skip to content

Instantly share code, notes, and snippets.

@infojunkie
Last active March 23, 2018 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infojunkie/2573048 to your computer and use it in GitHub Desktop.
Save infojunkie/2573048 to your computer and use it in GitHub Desktop.
HelloPOI - first experiment with Apache POI vs. PHPExcel
<project name="HelloPOI" basedir="." default="run">
<path id="project.classpath">
<pathelement path="."/>
<fileset dir=".">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile">
<javac srcdir="." destdir=".">
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<java classname="HelloPOI">
<arg value="${arg0}"/>
<classpath refid="project.classpath"/>
</java>
</target>
</project>
#!/usr/bin/php
<?php
require('PHPExcel/Classes/PHPExcel.php');
$workbook = PHPExcel_IOFactory::load($argv[1]);
$sheet = $workbook->getSheet(0);
foreach ($rit = $sheet->getRowIterator() as $row) {
foreach ($cit = $row->getCellIterator() as $cell) {
echo $cell->getCoordinate() . " - ";
switch ($cell->getDataType()) {
case PHPExcel_Cell_DataType::TYPE_STRING:
echo "STRING - ";
echo $cell->getValue();
break;
case PHPExcel_Cell_DataType::TYPE_NUMERIC:
echo "NUMBER - ";
echo $cell->getValue();
break;
case PHPExcel_Cell_DataType::TYPE_FORMULA:
echo "FORMULA - ";
echo $cell->getValue();
break;
case PHPExcel_Cell_DataType::TYPE_BOOL:
echo "BOOLEAN - ";
echo $cell->getValue();
break;
default:
echo $cell->getDataType();
}
echo "\n";
}
}
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.util.*;
public class HelloPOI {
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream(args[0]);
POIFSFileSystem poifs = new POIFSFileSystem(fin);
HSSFWorkbook wb = new HSSFWorkbook(poifs);
Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print("STRING - ");
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print("DATE - ");
System.out.println(cell.getDateCellValue());
} else {
System.out.print("NUMBER - ");
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print("BOOLEAN - ");
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print("FORMULA - ");
System.out.println(cell.getCellFormula());
break;
default:
System.out.print(cell.getCellType());
System.out.println();
}
}
}
}
}
@infojunkie
Copy link
Author

Run as ant -Darg0=/home/infojunkie/Desktop/hello.xls

@infojunkie
Copy link
Author

Added HelloPHP.php as the PHPExcel equivalent. Interesting to reveal differences between two libraries.

@armyofda12mnkeys
Copy link

just curious, notice any differences between PHPExcel and Apache POI from the performance standpoint?
Like I need to process a decent amount of rows (lets say 10k) from xlsx/xls/csv files.
I need get files in memory quick and read from them (no writing needed).
Was curious if ran into any issues with either library.
Don't think POI handles csv files too it seems, so thats a disadvantage there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment