jxls Excel 파일 내보내기

4727 단어
package net.chinaedu.projects.g3mini.common;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class JxlsUtil {
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/system/downloadSchool.do")
	public ModelAndView downTeacherTemplate(HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");

			response.setHeader("Content-Disposition", "attachment;fileName=" + "school.xls"); // 
			/*String filePath = request.getRealPath("") + "/teacherTemplate/" + Constants.TEACHER_TEMPLATE;
			File templateFile = new File(filePath);
			InputStream in = new FileInputStream(templateFile);*/
			OutputStream os = response.getOutputStream();
			String templateFile=request.getRealPath("")+"/jxls_template.xls";
			Map<String, Object> beans=new HashMap<String, Object>();
			
			// fruits
			List<Map<String,String>> fruitList=new ArrayList<Map<String,String>>();
			
			Map<String,String> fruit=null;
			fruit=new HashMap<String, String>();
			fruit.put("name", " ");
			fruit.put("price", "100");
			fruitList.add(fruit);
			
			fruit=new HashMap<String, String>();
			fruit.put("name", " ");
			fruit.put("price", "200");
			fruitList.add(fruit);
			
			beans.put("fruits",fruitList);
			exportExcel(templateFile, beans, os);
			//FileCopyUtils.copy(in, os);

			/*os.flush();
			os.close();

			in.close();*/
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
	/**
	 *  excel
	 * @param templateFile - excel 
	 * @param beans -  
	 * @param os -  
	 */
	public static void exportExcel(String templateFile,Map<String,Object> beans,OutputStream os) {
	     XLSTransformer transformer = new XLSTransformer();
	    // InputStream is=JxlsUtil.class.getClassLoader().getResourceAsStream(templateFile);
	     //String filePath = request.getRealPath("") + "/" +templateFile;
	     //InputStream is=new FileInputStream(templateFile);
	     
	     try {
	    	InputStream is=new FileInputStream(templateFile);
			Workbook workbook=transformer.transformXLS(is,beans);
			workbook.write(os);
		} catch (Exception e) {
			e.printStackTrace();
			//throw new RuntimeException(" excel !");
		} 
	}
	
	public static void main(String[] args) throws ParsePropertyException, InvalidFormatException, IOException {
		OutputStream os=new FileOutputStream("F:/new.xls");
		String templateFile="F:/jxls_template.xls";
		Map<String, Object> beans=new HashMap<String, Object>();
		
		// fruits
		List<Map<String,String>> fruitList=new ArrayList<Map<String,String>>();
		
		Map<String,String> fruit=null;
		fruit=new HashMap<String, String>();
		fruit.put("name", " ");
		fruit.put("price", "100");
		fruitList.add(fruit);
		
		fruit=new HashMap<String, String>();
		fruit.put("name", " ");
		fruit.put("price", "200");
		fruitList.add(fruit);
		
		beans.put("fruits",fruitList);
		exportExcel(templateFile, beans, os);
	}
}

좋은 웹페이지 즐겨찾기