JAVA导出excel(JFinal)

1. 后台代码

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.jfinal.core.Controller;

public class HelloController extends Controller{
    HelloService helloService = enhance(HelloService.class);//事务加强   1.这里enhance加强  2.service中具体方法  添加@Before(Tx.class)

    public void index() {
        render("HelloFile.jsp");
    }

    public void exportExcel(){
        String fileName = "xxxxxx.xls";
        try {
            fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        HttpServletResponse response = getResponse();
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        OutputStream output = null;
        try {
            output = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);

        HSSFWorkbook wb = new HSSFWorkbook();
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定当单元格内容显示不下时自动换行
        cellStyleTitle.setWrapText(true);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定当单元格内容显示不下时自动换行
        cellStyle.setWrapText(true);
        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontName("宋体");
        font.setFontHeight((short) 250);
        cellStyleTitle.setFont(font);
        // ------------------------------------------------------------------
        HSSFSheet sheet = wb.createSheet();  
        sheet.setDefaultRowHeightInPoints(25);
        sheet.setColumnWidth(0, 256 * 20);
        sheet.setColumnWidth(1, 256 * 30);
        sheet.setColumnWidth(2, 256 * 50);
        sheet.setColumnWidth(4, 256 * 30);

        // 创建报表头部  
        HSSFRow row0 = sheet.createRow(0);
        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, (short) 0, (short) 2)); //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列   
        sheet.addMergedRegion(new CellRangeAddress(0, 0, (short) 3, (short) 11)); //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列   
        HSSFCell cell0 = row0.createCell(0);
        cell0.setCellStyle(cellStyleTitle);  
        cell0.setCellValue(new HSSFRichTextString("xxxxxxxxx")); 

        cell0 = row0.createCell(3);
        cell0.setCellStyle(cellStyleTitle);  
        cell0.setCellValue(new HSSFRichTextString("xxxxxxxxxx")); 
        // 定义第一行  
        HSSFRow row1 = sheet.createRow(1); 

        HSSFCell cell1 = row1.createCell(0);  
        //第一行第一列    
        cell1.setCellStyle(cellStyleTitle);  
        cell1.setCellValue(new HSSFRichTextString("姓名"));  
        //第一行第二列  
        cell1 = row1.createCell(1);  
        cell1.setCellStyle(cellStyleTitle);  
        cell1.setCellValue(new HSSFRichTextString("联系电话"));  
        //第一行第三列  
        cell1 = row1.createCell(2);  
        cell1.setCellStyle(cellStyleTitle);  
        cell1.setCellValue(new HSSFRichTextString("公司名称"));   
        //第一行第四列  
        cell1 = row1.createCell(3);  
        cell1.setCellStyle(cellStyleTitle);  
        cell1.setCellValue(new HSSFRichTextString("人数"));   

//       List<User> list = xxxService.getList();//从数据库
//        for(int x=0;x<list.size();x++){
//          User z = list.get(x);
//          HSSFRow row = sheet.createRow(x + 2); 
//          
//          HSSFCell cell = row.createCell(0);  
//            cell.setCellStyle(cellStyle);  
//            cell.setCellValue(new HSSFRichTextString(z.getStr("name")));             
//            cell = row.createCell(1);  
//            cell.setCellStyle(cellStyle);  
//            cell.setCellValue(new HSSFRichTextString(z.getStr("phone")));               
//            cell = row.createCell(2);  
//            cell.setCellStyle(cellStyle);  
//            cell.setCellValue(new HSSFRichTextString(z.getStr("company")));         
//            cell = row.createCell(3);  
//            cell.setCellStyle(cellStyle);  
//            cell.setCellValue(new HSSFRichTextString(Integer.toString(z.getInt("count"))));         
//        }
        try {  
            bufferedOutPut.flush();  
            wb.write(bufferedOutPut);  
            bufferedOutPut.close();  
        } catch (IOException e) {  

        }   

        renderNull();
    }
}

2.前台代码

<button onclick="exportExcel()">导出excel</button>
<script>
function exportExcel(){
    window.open("<%=request.getContextPath()%>/exportExcel")
}

3.jar包下载
下载地址:http://pan.baidu.com/s/1nvhX3N7

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值