これが私の場合にうまく機能する私のテスト済みソリューションです。データベースから直接ロードされた大量のデータをExcelシートに生成することができます。
Connection conn = getDs().getConnection();
// such a configuration of prepared statement is mandatory for large amount of data
PreparedStatement ps = conn.prepareStatement(MY_QUERY, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
// load huge amount of data per 50000 items
ps.setFetchSize(50000);
ResultSet rs = ps.executeQuery();
// calculate count of returned data earlier
final CountWrapper countWrapper = new CountWrapper(countCalculatedEarlier);
CustomResultSetIterator<MyObject> rowsIter = new CustomResultSetIterator<MyObject>(rs, new org.apache.commons.dbutils.BasicRowProcessor(), MyObject.class) {
private Long iterCount = 0L;
@Override
public boolean hasNext() {
// you can't call isLast method on defined as forward only cursor of result set, hence there is a homegrown calculation whether last item is reached or not
return iterCount < countWrapper.getCount().longValue();
};
@Override
public MyObject next() {
iterCount++;
return super.next();
};
};
VelocityContext context = new VelocityContext();
// place an interator here instead of collection object
context.put("rows", rowsIter);
Template t = ve.getTemplate(template);
File file = new File(parthToFile);
FileWriter fw = new FileWriter(file);
// generate on the fly in my case 1 000 000 rows in Excel, watch out such an Excel may have 1GB size
t.merge(context, fw);
// The CustomResultSetIterator is a:
public class CustomResultSetIterator<E> implements Iterator<E> {
//...implement all interface's methods
}