Wenas, les comparto parte del codigo para generar excel mediante spring mvc, de varias formas
Código PHP:
package com.controlador;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import com.servicio.searchService;
import com.utils.excelReporte;
import com.utils.excelReporteTiempo;
.....................
@Controller
public class inventoryControl {
..........................
@RequestMapping(value="/excelmvc")
public ModelAndView list() {
logger.debug("generar reporte excel..");
return new ModelAndView(new excelReporte());
}
usando el AbstractExcelView en mi clase excelReporte
Código PHP:
package com.utils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.*;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
public class excelReporte extends AbstractExcelView {
@Override
protected void buildExcelDocument (Map model,
HSSFWorkbook workbook,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Set the response mode, perform this controller automatically download page, rather than direct use Excel to open
response.setContentType ("APPLICATION / OCTET-STREAM");
//response.setHeader ("Content-Disposition","Attachment; filename =Excel.xls ");
response.addHeader("Content-Disposition", "attachment; filename=\"myexcel.xls\"");
// Construct data
genericoBean stu1 = new genericoBean ("gaoxiang1", "male1", "20060101", "1");
genericoBean stu2 = new genericoBean ("gaoxiang2", "male2", "20060102", "2");
genericoBean stu3 = new genericoBean ("gaoxiang3", "male3", "20060103", "3");
genericoBean stu4 = new genericoBean ("gaoxiang4", "male4", "20060104", "4");
genericoBean stu5 = new genericoBean ("gaoxiang5", "male5", "20060105", "5");
ArrayList stuList = new ArrayList ();
stuList.add (stu1);
stuList.add (stu2);
stuList.add (stu3);
stuList.add (stu4);
stuList.add (stu5);
// Excel header
HSSFSheet sheet = workbook.createSheet("studentList");
HSSFRow header = sheet.createRow (0); // row 0
// Generate the title bar
header.createCell ((short) 0).setCellValue ("name");
header.createCell ((short) 1).setCellValue ("sex");
header.createCell ((short) 2).setCellValue ("date");
header.createCell ((short) 3).setCellValue ("count");
HSSFCellStyle cellStyle = workbook.createCellStyle ();
cellStyle.setDataFormat (HSSFDataFormat.getBuiltinFormat ("mm / dd / yyyy"));
// Fill data
int rowNum = 1;
for (Iterator iter = stuList.iterator (); iter.hasNext () ;) {
genericoBean element = (genericoBean) iter.next ();
HSSFRow row = sheet.createRow (rowNum ++);
row.createCell ((short) 0). setCellValue (element.getAttr1(). toString ());
row.createCell ((short) 1). setCellValue (element.getAttr2(). toString ());
row.createCell ((short) 2). setCellValue (element.getAttr3(). toString ());
row.getCell ((short) 2). setCellStyle (cellStyle);
row.createCell ((short) 3). setCellValue (element.getAttr4());
}
// Column is calculated as the sum of
HSSFRow row = sheet.createRow (rowNum);
row.createCell ((short) 0). setCellValue ("TOTAL:");
String formual = "SUM (D2: D" + rowNum + ")"; // D2 to D [rownum] cell Ki (count data)
row.createCell ((short) 3). setCellFormula (formual);
}
}
Y en el jsp
Código PHP:
....
<a href="<%=request.getContextPath()%>/excelmvc.htm" >Download in Excel doc</a>