我正在尝试使用spring hibernate将mysql表中的数据导出为excel。我的excel导出在导出后没有数据。我们将不胜感激。这是我的代码。谢谢提前
ExcelExport.java
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {
@SuppressWarnings("unchecked")
List<MOH> mohs = (List<MOH>) model.get("mohs");
HSSFSheet sheet = workbook.createSheet("MOH Form")
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.getCell(0).setCellStyle(style);
header.createCell(1).setCellValue("Data");
header.getCell(1).setCellStyle(style);
int rowCount = 1;
for (MOH aBook : mohs) {
HSSFRow aRow = sheet.createRow(rowCount++);
aRow.createCell(0).setCellValue(aBook.getSurvey_id());
aRow.createCell(1).setCellValue(aBook.getName());
Controller.java
@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
ModelAndView mav = new ModelAndView("showMOH");
List<MOH> mohs = new ArrayList<MOH>();
return new ModelAndView("excelView", "mohs", mohs);
答案 0 :(得分:2)
可能有更多选择: -
1.使用hibernate,您可以使用Apache POI执行此操作。
这是tutorial with example,将指导您完成整个过程
2.您还可以使用BIRT并将数据库生成为您想要的任何格式。
3.您可以使用ResultSet将数据直接从MySQL导出到Excel。见how to export ResultSet to Excel。
4.如果您使用struts2,可以使用datagrid将其导出为CSV或Excel。
答案 1 :(得分:0)
它的工作
在您的控制器中
@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel(Model model) {
List<String> usersGateways = uDAO.GetGwRoleUser();
List<User> users = gatewayManagedDAO.findAll();
return new ModelAndView(new ExcelView(), "users ", users );
}
}
在ExcelView中
public class ExcelView extends AbstractXlsView{
@Override
public void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-exported-file.xls\"");
@SuppressWarnings("unchecked")
List<User> users= (List<GatewayManage>) model.get("users");
// create excel xls sheet
Sheet sheet = workbook.createSheet("Users Detail");
sheet.setDefaultColumnWidth(30);
// create style for header cells
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("Arial");
style.setFillForegroundColor(HSSFColor.BLUE.index);
//style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//font.setBold(true);
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);
// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("First Name");
header.getCell(0).setCellStyle(style);
header.createCell(1).setCellValue("Last Name");
header.getCell(1).setCellStyle(style);
header.createCell(2).setCellValue("Number");
header.getCell(2).setCellStyle(style);
header.createCell(3).setCellValue("Age");
header.getCell(3).setCellStyle(style);
int rowCount = 1;
for(User user : users){
Row userRow = sheet.createRow(rowCount++);
gatewayRow.createCell(0).setCellValue(user.getFirstName());
gatewayRow.createCell(1).setCellValue(gateway.getLastName());
gatewayRow.createCell(2).setCellValue(gateway.getNumber());
gatewayRow.createCell(3).setCellValue(gateway.getAge());
}
}
}
你可以用你的(Studen,aBook ....)替换我的用户类,这样就可以了!