2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.exporters;
|
2010-03-17 23:30:16 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.ProjectManager;
|
|
|
|
import com.google.refine.browsing.Engine;
|
|
|
|
import com.google.refine.browsing.FilteredRows;
|
|
|
|
import com.google.refine.browsing.RowVisitor;
|
|
|
|
import com.google.refine.model.Cell;
|
|
|
|
import com.google.refine.model.Column;
|
|
|
|
import com.google.refine.model.Project;
|
|
|
|
import com.google.refine.model.Row;
|
2010-03-17 23:30:16 +01:00
|
|
|
|
|
|
|
public class XlsExporter implements Exporter {
|
|
|
|
public String getContentType() {
|
|
|
|
return "application/xls";
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean takeWriter() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
|
2010-04-08 22:46:02 +02:00
|
|
|
throw new RuntimeException("Not implemented");
|
2010-03-17 23:30:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void export(Project project, Properties options, Engine engine,
|
|
|
|
OutputStream outputStream) throws IOException {
|
|
|
|
|
|
|
|
Workbook wb = new HSSFWorkbook();
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
wb.setSheetName(0, ProjectManager.singleton.getProjectMetadata(project.id).getName());
|
|
|
|
|
|
|
|
int rowCount = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
org.apache.poi.ss.usermodel.Row r = s.createRow(rowCount++);
|
|
|
|
|
|
|
|
int cellCount = 0;
|
|
|
|
for (Column column : project.columnModel.columns) {
|
|
|
|
org.apache.poi.ss.usermodel.Cell c = r.createCell(cellCount++);
|
|
|
|
c.setCellValue(column.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
RowVisitor visitor = new RowVisitor() {
|
|
|
|
Sheet sheet;
|
|
|
|
int rowCount;
|
|
|
|
|
|
|
|
public RowVisitor init(Sheet sheet, int rowCount) {
|
|
|
|
this.sheet = sheet;
|
|
|
|
this.rowCount = rowCount;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-05-21 00:10:34 +02:00
|
|
|
@Override
|
|
|
|
public void start(Project project) {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void end(Project project) {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:13:19 +02:00
|
|
|
public boolean visit(Project project, int rowIndex, Row row) {
|
2010-03-17 23:30:16 +01:00
|
|
|
org.apache.poi.ss.usermodel.Row r = sheet.createRow(rowCount++);
|
|
|
|
|
|
|
|
int cellCount = 0;
|
|
|
|
for (Column column : project.columnModel.columns) {
|
|
|
|
org.apache.poi.ss.usermodel.Cell c = r.createCell(cellCount++);
|
|
|
|
|
|
|
|
int cellIndex = column.getCellIndex();
|
|
|
|
if (cellIndex < row.cells.size()) {
|
|
|
|
Cell cell = row.cells.get(cellIndex);
|
2010-03-19 01:32:52 +01:00
|
|
|
if (cell != null) {
|
2010-03-17 23:30:16 +01:00
|
|
|
if (cell.recon != null && cell.recon.match != null) {
|
2010-06-24 22:13:51 +02:00
|
|
|
c.setCellValue(cell.recon.match.name);
|
2010-03-19 01:32:52 +01:00
|
|
|
|
2010-03-17 23:30:16 +01:00
|
|
|
HSSFHyperlink hl = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
|
2010-06-24 22:13:51 +02:00
|
|
|
hl.setLabel(cell.recon.match.name);
|
|
|
|
hl.setAddress("http://www.freebase.com/view" + cell.recon.match.id);
|
2010-03-17 23:30:16 +01:00
|
|
|
|
|
|
|
c.setHyperlink(hl);
|
2010-03-19 01:32:52 +01:00
|
|
|
} else if (cell.value != null) {
|
|
|
|
Object v = cell.value;
|
|
|
|
|
|
|
|
if (v instanceof Number) {
|
|
|
|
c.setCellValue(((Number) v).doubleValue());
|
|
|
|
} else if (v instanceof Boolean) {
|
|
|
|
c.setCellValue(((Boolean) v).booleanValue());
|
|
|
|
} else if (v instanceof Date) {
|
|
|
|
c.setCellValue((Date) v);
|
|
|
|
} else if (v instanceof Calendar) {
|
|
|
|
c.setCellValue((Calendar) v);
|
|
|
|
} else if (v instanceof String) {
|
2010-09-28 05:44:30 +02:00
|
|
|
String s = (String) v;
|
|
|
|
if (s.length() > 32767) {
|
|
|
|
// The maximum length of cell contents (text) is 32,767 characters
|
|
|
|
s = s.substring(0, 32767);
|
|
|
|
}
|
|
|
|
c.setCellValue(s);
|
2010-03-19 01:32:52 +01:00
|
|
|
}
|
2010-03-17 23:30:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}.init(s, rowCount);
|
|
|
|
|
2010-05-20 02:13:19 +02:00
|
|
|
FilteredRows filteredRows = engine.getAllFilteredRows();
|
2010-03-17 23:30:16 +01:00
|
|
|
filteredRows.accept(project, visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.write(outputStream);
|
|
|
|
outputStream.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|