java使用jxls导出excel功能

在java的开发里,导出excel功能会经常遇到的,而jxls框架提供了很大的方便.
jxls官网:http://jxls.sourceforge.net/,官网的下载包里面也提供详细的文档说明.
下文提供思路,主要以web项目导出excel来举例jxls用法,文末也贴一下java项目导出excel功能(这个简单一点)
直接贴关键代码(其它关于怎么用java config方式的spring mvc不做说明)

1.引用jxls依赖


	net.sf.jxls
	jxls-core
	1.0.5

2.EmployeeController.java

package com.exam.web;
import com.exam.domain.Employee;
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.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    /**
     * 这里接管了response,我们自己把response flush出去,所以返回类型是void(不要用什么String,否则会报响应已提交过的异常哦)
     */
    @RequestMapping("/export")
    public void export(HttpServletRequest request,HttpServletResponse response){
        String templateFileName= request.getServletContext().getRealPath("/") + "/resources/templateFileName.xls";
        String destFileName= "destFileName.xls";
        //模拟数据
        List staff = new ArrayList();
        staff.add(new Employee("Derek", 35, 3000, 0.30));
        staff.add(new Employee("Elsa", 28, 1500, 0.15));
        staff.add(new Employee("Oleg", 32, 2300, 0.25));
        Map beans = new HashMap();
        beans.put("employees", staff);
        XLSTransformer transformer = new XLSTransformer();
        InputStream in=null;
        OutputStream out=null;
        //设置响应
        response.setHeader("Content-Disposition", "attachment;filename=" + destFileName);
        response.setContentType("application/vnd.ms-excel");
        try {
            in=new BufferedInputStream(new FileInputStream(templateFileName));
            Workbook workbook=transformer.transformXLS(in, beans);
            out=response.getOutputStream();
            //将内容写入输出流并把缓存的内容全部发出去
            workbook.write(out);
            out.flush();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in!=null){try {in.close();} catch (IOException e) {}}
            if (out!=null){try {out.close();} catch (IOException e) {}}
        }
    }
}
3.templateFileName.xls(内容是二进制,不容易贴出来)
java使用jxls导出excel功能_第1张图片

这个JEXL语法和JSTL语法十分相似.


这里我们自己处理了Response,没必要交给spring的视图解析器.在返回响应前我们设置了响应的Header和ContentType以便客户端的浏览器解析为弹出下载.这里调用了transformer.transformXLS(in, beans)方法得到一个Workbook,这方法会把模板(至于模板怎么写,详细看官方文档)先读入到输入流再处理,再把Workbook的内容写入到输出流发送出去.


上文源码:http://download.csdn.net/detail/xiejx618/7836191


如果不想使用上面的模板,还有一种更灵活自定义的方法(比如编码定义excel样式,动态增加多个sheet等等),也可以在Controller方法直接返回AbstractXlsView.下面是一个例子:

@RequestMapping(value = "listXls",method = RequestMethod.GET)
    public View listXls() {
        List list = getUsers();
        return new AbstractXlsView() {
            @Override
            protected void buildExcelDocument(Map model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
                addSheet(workbook.createSheet("UserStatistics1"), list.subList(0, 50));//不包括第50个
                addSheet(workbook.createSheet("UserStatistics2"), list.subList(50,list.size()));
            }
        };
    }

    private List getUsers() {
        List list = new ArrayList<>();
        for (int i = 0; i < 103; i++) {
            User user = new User();
            user.setUsername("name" + i);
            user.setPassword("pwd" + i);
            user.setId((long) i);
            list.add(user);
        }
        return list;
    }

    private void addSheet(Sheet sheet, List list) {
        int rowNum=0;
        Row row0 = sheet.createRow(rowNum++);
        row0.createCell(0).setCellValue("ID");
        row0.createCell(1).setCellValue("Username");
        row0.createCell(2).setCellValue("Password");
        row0.createCell(3).setCellValue("Height");
        for (User user : list) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getId());
            row.createCell(1).setCellValue(user.getUsername());
            row.createCell(2).setCellValue(user.getPassword());
            row.createCell(3).setCellValue(user.getHeight());
        }
    }


顺便贴一下java项目(不是web项目)的例子源码:http://download.csdn.net/detail/xiejx618/7836227




你可能感兴趣的:(java)