Reputation: 255
I have this DAO..
public class AreaDAO {
private Database database;
public AreaDAO(Database database) {
this.database = database;
}
public List<Area> list() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Area> areas = new ArrayList<Area>();
try {
connection = database.getConnection();
statement = connection.prepareStatement("select distinct img_hub, country from ifs_db limit 50");
resultSet = statement.executeQuery();
while (resultSet.next()) {
Area area = new Area();
area.setImg_hub(resultSet.getString("img_hub"));
area.setCountry(resultSet.getString("country"));
areas.add(area);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return areas;
}
}
this produce a list of data...
How can I let the user download all the data in excel by just clicking on the link..
I' researched the we and found this...(by BalusC)
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=name.xls");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
I place it to my servlet, how ever I'm just getting a blank worksheet... how can I use this so I can let the user download a
Upvotes: 1
Views: 5724
Reputation: 1109532
I understand that you're referring to my answer on this question? Note the // ...
line, that's where you should populate the workbook with the data from the DB. The OP of that question already knew that, so it was omitted for brevity.
You can find on the POI HSSF own site a quick guide how to use the API.
Here's a kickoff example how to create a sheet with a cell.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=name.xls");
WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
sheet.addCell(new Label(0, 0, "Hello World"));
// ...
workbook.write();
workbook.close();
You can add cells while iterating over the List<Area>
. E.g.
for (int i = 0; i < list.size(); i++) {
Area area = list.get(i);
sheet.addCell(new Label(0, i, area.getImgHub()));
sheet.addCell(new Label(1, i, area.getCountry()));
}
Upvotes: 4