Most of the times i use Apache POI for my ms document parsing, but the other day i found an interesting tool, very easy to use, that allows you to parse excel. It is called JXL.
Here a very simple program that will parse the contents of an excel sheet and display the values in the console:
Here a very simple program that will parse the contents of an excel sheet and display the values in the console:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Convertor {
private String inputFile;
private FileWriter fosStream;
private PrintWriter out = null;
private String outputFilePath = "";
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
fosStream = new FileWriter(outputFilePath + "output.xml");
out = new PrintWriter(new BufferedWriter(fosStream));
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<Document>");
for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (cell.getContents() == null
|| cell.getContents().equals("")) {
continue;
}
out.println("<" + cell.getColumn() + "" + cell.getRow()
+ ">");
out.println(cell.getContents());
out.println("</" + cell.getColumn() + "" + cell.getRow()
+ ">");
}
}
} catch (BiffException e) {
e.printStackTrace();
}
out.write("</Document>");
out.flush();
out.close();
}
public static void main(String[] args) throws IOException {
Convertor test = new Convertor();
test.setInputFile("input.xls");
test.read();
}
}