struts2에서 excel를 출력할 경우가 있는데
인터넷 검색결과 내가 하고 싶은 방식으로 되어 있는 소스가 없었다.
그래서 spring mvc abstractExcelView를 참고해서 struts2용 AbstractView를 만들었다.
[code java]package pmis.common.excel; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; import com.opensymphony.xwork2.util.ValueStack; public abstract class AbstractExcelResult implements Result { /** The content type for an Excel response */ private static final String CONTENT_TYPE = "application/vnd.ms-excel"; /** The extension to look for existing templates */ private static final String EXTENSION = ".xls"; private String url; public void setUrl(String url) { this.url = url; } @Override public void execute(ActionInvocation invocation) throws Exception { try { HSSFWorkbook workbook; HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); String requestURI = request.getRequestURI(); workbook = new HSSFWorkbook(); buildExcelDocument(invocation.getStack(), workbook, ServletActionContext.getRequest(), ServletActionContext.getResponse()); // Set the content type. response.setContentType(CONTENT_TYPE); response.setHeader("Content-disposition", "attachment;filename="+StringUtils.defaultIfEmpty(url, StringUtils.substring(requestURI,StringUtils.lastIndexOf(requestURI, "/"), StringUtils.indexOf(requestURI, ".")))+EXTENSION); ServletOutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } protected abstract void buildExcelDocument( ValueStack valueStack, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception; protected HSSFCell getCell(HSSFSheet sheet, int row, int col) { HSSFRow sheetRow = sheet.getRow(row); if (sheetRow == null) { sheetRow = sheet.createRow(row); } HSSFCell cell = sheetRow.getCell(col); if (cell == null) { cell = sheetRow.createCell(col); } return cell; } } [/code]
위의 소스를 확장해서 각각의 페이지별로 Excel를 만들때는
[code java]package pmis.system.loginstat; import java.math.BigDecimal; 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.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import pmis.common.excel.AbstractExcelResult; import com.opensymphony.xwork2.util.ValueStack; public class LoginStatExcelResult extends AbstractExcelResult { @Override protected void buildExcelDocument(ValueStack valueStack, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { //여기에 구현 } } [/code]
그리고
xml 페이지에선
result type 선언
[code xml]
action 선언에선
[code xml]
action 선언에서 <result type="excel"> 로 하였는데 result type 선언한 loginExcel을 사용해야 하는 것이 아닌가요?
답글삭제@김진수 - 2010/10/16 07:49
답글삭제네 그게 맞네요. action 선언부는 그냥 제가 써서 오타가 있나보내요 ㅡㅡ;;
만들어 놓은지가 몇달되고 소스도 없다보니.. ^^;;