Where The Streets Have No Name

엑셀 데이터 다운로드 본문

Developement/Java

엑셀 데이터 다운로드

highheat 2011. 10. 26. 14:10
<bean id="categoryExcelView" class="egovframework.rte.test.web.CategoryExcelView" />
 
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
	<property name="order" value="0" />
</bean>
@RequestMapping("/sale/listExcelVOCategory.do")
public ModelAndView selectCategoryVOList() throws Exception {
	 
	List<UsersVO> lists = new ArrayList<UsersVO>();
	 
	UsersVO users = new UsersVO();
	 
	//Map<String, String> mapCategory = new HashMap<String, String>();
	users.setId("0000000001");
    users.setName("Sample Test");
	users.setDescription("This is initial test data.");
	users.setUseYn("Y");
	users.setRegUser("test");
	 
	lists.add(users);
	 
	users.setId("0000000002");
	users.setName("홍길동");
	users.setDescription("test Deso1111");
	users.setUseYn("Y");
	users.setRegUser("test");
	 
	lists.add(users);
	 
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("category", lists);
	 
	return new ModelAndView("categoryExcelView", "categoryMap", map);
}
package egovframework.rte.test.web;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import egovframework.rte.test.service.UsersVO;

public class CategoryExcelView extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest req, HttpServletResponse res) throws Exception {

		HSSFCell cell = null;

		HSSFSheet sheet = wb.createSheet("User List");
		sheet.setDefaultColumnWidth((short) 12);

		// put text in first cell
		cell = getCell(sheet, 0, 0);
		setText(cell, "User List");

		// set header information
		setText(getCell(sheet, 2, 0), "id");
		setText(getCell(sheet, 2, 1), "name");
		setText(getCell(sheet, 2, 2), "description");
		setText(getCell(sheet, 2, 3), "use_yn");
		setText(getCell(sheet, 2, 4), "reg_user");

		Map<String, Object> map = (Map<String, Object>) model.get("categoryMap");
		List<Object> categories = (List<Object>) map.get("category");

		boolean isVO = false;

		if (categories.size() > 0) {
			Object obj = categories.get(0);
			isVO = obj instanceof UsersVO;
		}

		for (int i = 0; i < categories.size(); i++) {

			if (isVO) { // VO

				UsersVO category = (UsersVO) categories.get(i);

				cell = getCell(sheet, 3 + i, 0);
				setText(cell, category.getId());

				cell = getCell(sheet, 3 + i, 1);
				setText(cell, category.getName());

				cell = getCell(sheet, 3 + i, 2);
				setText(cell, category.getDescription());

				cell = getCell(sheet, 3 + i, 3);
				setText(cell, category.getUseYn());

				cell = getCell(sheet, 3 + i, 4);
				setText(cell, category.getRegUser());
			} else { // Map

				Map<String, String> category = (Map<String, String>) categories.get(i);

				cell = getCell(sheet, 3 + i, 0);
				setText(cell, category.get("id"));

				cell = getCell(sheet, 3 + i, 1);
				setText(cell, category.get("name"));

				cell = getCell(sheet, 3 + i, 2);
				setText(cell, category.get("description"));

				cell = getCell(sheet, 3 + i, 3);
				setText(cell, category.get("useyn"));

				cell = getCell(sheet, 3 + i, 4);
				setText(cell, category.get("reguser"));

			}
		}
		
		//xls확장자로 다운로드
		res.setContentType("application/x-msdownload");
		res.setHeader("Content-Disposition", "attachment; filename=\"aaa.xls\"");
	}

}