`
nicegege
  • 浏览: 580351 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jxls实现Excel文件导出

阅读更多
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);
	}
}

 

分享到:
评论

相关推荐

    SpringBoot(33) 整合JXLS实现Excel导入导出

    SpringBoot(33) 整合JXLS实现Excel导入导出

    jxls利用模板生成excel文件

    jxls利用模板生成excel文件, 非常方便

    使用 jxls2.X 导出excel文件

    项目案例 http://blog.csdn.net/lnktoking/article/details/52932679

    简单springboot实现Jxls导出excel

    jxls使用简单,只需定义一个excel模板,在excel中使用jx公式即可轻松实现,具体可查看项目,运行Application.java的main方法即可启动项目,访问htpp://localhost:8080/member/user/userExport即可导出数据生成excel...

    用Jxls导入导出Excel2003和Excel2007数据3

    本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...

    用Jxls导入导出Excel2003和Excel2007数据2

    本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...

    用Jxls导入导出Excel2003和Excel2007数据1

    本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...

    jxls实现的excel导出功能,支持03以上版本

    通过eclipse导入工程,excel需要提前制作模板文件

    用Jxls导入导出Excel2003和Excel2007数据

    本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...

    jxl实现excel大数据导出

    jxl实现excel大数据导出,26000条记录,测试导出时间是19s,例子不算复杂,没有excel样式的处理,可以自己扩展哈,有数据库文件,部署即可看到效果!!! 有三个功能:代码构建数据的导出、数据库数据的导出(从配置...

    jxls-poi导出excel示例代码文件

    jxls,读取json转为excel文件导出

    jxls 基于模版导出excel

    jxls 基于模版导出excel 基于freemarker 导出excel jxls-core-1.0.jar jxls-reader-1.0.jar

    jxls-me:导出使用JXLS实现的Excel文件示例

    我使用Java JXLS轻松导出excel文件。为什么? 使用Java创建和导出excel模板应该比看起来容易。入门假设我们有一个jedi对象的Java集合,我们希望将其输出到Excel中。 绝地班看起来像这样public class Jedi { private ...

    使用 jxls2.X 导出excel文件源码

    项目案例 http://blog.csdn.net/lnktoking/article/details/52932679

    JXLS:excel模板形式导出excel

    #####jXLS - 使用 XLS 模板将数据导出到 Excel 要使用 jXLS 引擎,您必须将 jxls-core jar 放在类路径中。 如果您打算使用 jXLS 读取 XLS 文件,则必须将 jxls-reader jar 文件添加到类路径中。 如果您使用 Maven...

    jxls实现多sheet的报表导出-附件资源

    jxls实现多sheet的报表导出-附件资源

    从数据库将大量数据导出到Excel文件程序总结

    这是我在公司实习的时候研究的课题,主要将大量数据从数据库导出到Excel文件。(一般是值Excel2007及以后版本)因为Excel2003及以前的版本中能保持的数据量是有限制的。Excel2007及以后版本中的数据能保持100万条...

    Excel 表格导出模板

    1、Excel 表格导出的 参考模板,及对应 数据库文件和实体类。 2、百度网盘的下载地址为: https://pan.baidu.com/s/16vVVssbIoiQ045JEQPTErQ 提取码: 89e4 3、阿里云盘的下载地址为:...

    jxls全部jar包(内包含一个可用的例子)

    使用jxls导出Excel例子。文件包括了jxls所需的所有jar包。

    Excel的导入导出demo

    这个是一个poi技术和jxls技术对excel文件的导入导出的小demo。本来我想设置为分数0的他居然不给我这个选项,很尴尬!

Global site tag (gtag.js) - Google Analytics