jexcelapi jxl

1.导入jxl(net.sourceforge.jexcelapi)

2.书写报表帮助类:

package com.kevin.jexcel.common;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class JexcelHelp {
 private WritableWorkbook workbook;

 public JexcelHelp(HttpServletResponse response, String name)
   throws IOException {
  response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition", "attachment; filename="
    + name + ".xls");
  workbook = Workbook.createWorkbook(response.getOutputStream());
 }

 public WritableSheet getSheet(String name, int index) {
  WritableSheet sheet = workbook.createSheet(name, index);
  return sheet;
 }

 public void addList(WritableSheet sheet, List<String[]> results)
   throws RowsExceededException, WriteException {
  for (int i = 0; i < results.size(); i++) {
   String[] element = results.get(i);
   for (int j = 0; j < element.length; j++) {
    String text = element[j];
    if (text == null)
     text = "";
    Label label = new Label(j, i, text);
    sheet.addCell(label);
   }
  }
 }

 public void generate() throws IOException, WriteException {
  workbook.write();
  workbook.close();
 }
}

3.使用:

package com.kevin.jexcel.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.springframework.stereotype.Service;
import com.kevin.entity.Sysuser;
import com.kevin.jexcel.common.JexcelHelp;
import com.kevin.sysuser.service.SysuserService;

@Service
public class JexcelService {
 @Resource
 private SysuserService sysuserService;

 public void export(HttpServletResponse response) throws IOException,
   RowsExceededException, WriteException {
  // 取出数据.
  List<Sysuser> userList = sysuserService.list();
  List<String[]> userList_format = format(userList);

  // 生成报表.
  JexcelHelp jexcelHelp = new JexcelHelp(response, "userlist");
  WritableSheet firstSheet = jexcelHelp.getSheet("1", 0);
  jexcelHelp.addList(firstSheet, userList_format);
  
  // 设置报表头.
  firstSheet.insertRow(0);
  firstSheet.mergeCells(0,0,3,0);
  WritableFont font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, false);
  WritableCellFormat cellFormat = new WritableCellFormat (font);
  cellFormat.setAlignment(Alignment.CENTRE);

  Label header= new Label(0, 0, "用户信息表", cellFormat);
  firstSheet.addCell(header);
  
  jexcelHelp.generate();
 }

 /**
  * 格式化数据.
  *
  * @param userList
  * @return
  */
 private List<String[]> format(List<Sysuser> userList) {
  List<String[]> userList_format = new ArrayList<String[]>();
  for (Sysuser user : userList) {
   String[] user_format = new String[4];
   user_format[0] = user.getId().toString();
   user_format[1] = user.getName();
   user_format[2] = user.getSexEnum().getLabel();
   user_format[3] = user.getState().toString();
   userList_format.add(user_format);
  }
  return userList_format;
 }
}

4.导出的excel文件如果单元格是数字的话,左上角有绿色小三角。可将excel设置成忽略拼写,将绿色小三角去掉。

 文件-》选项-》公式-》错误检查-》去掉“允许后台错误检查”的勾。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值