struts2配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="vclassenroll" extends="json-default" namespace="/vclassenroll">
<action name="op/*" class="com.xxx.action.VclassEnrollOperateAction" method="{1}">
<result name="excel" type="stream">
<param name="contentType">
application/vnd.ms-excel
</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">
filename="${fileName}.xls"
</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
struts2对应的action
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.annotation.Resource;
public class VclassEnrollOperateAction extends BaseAction {
private static final long serialVersionUID = -941593970307742358L;
@Resource
private VclassEnrollService vclassEnrollService;
private VclassEnroll vclassEnroll;
private String classId;
private InputStream excelStream;
private String fileName;
public String exportExcel() {
if(vclassEnroll == null){
vclassEnroll = new VclassEnroll();
}
if(StringUtils.isNotNull(classId)){
vclassEnroll.setVclass_id(Integer.parseInt(classId));
List<VclassEnroll> list = vclassEnrollService.getList(vclassEnroll);
// 将OutputStream转化为InputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
List<String> columns = new ArrayList<String>();
columns.add("序号");
columns.add("姓名");
columns.add("年级");
columns.add("班级");
columns.add("电话");
String cname ="";
if(list != null && list.size() > 0){
cname = list.get(0).getCname();
}
JxlExcelUtils.getExcelStream(out, list, "报名记录-"+cname, columns);
excelStream = new ByteArrayInputStream(out.toByteArray());
fileName = createFileName();
}
return "excel";
}
/**
* 以年月日时分秒毫秒+4位随机数的格式来创建一个文件名,不带扩展名
* @return 文件名
*/
public static String createFileName() {
StringBuffer sb = new StringBuffer();
Date date = new Date();
//获取年月日时分秒
sb.append(new SimpleDateFormat("yyyyMMddHHmmss").format(date));
//毫秒
String milli = String.valueOf(date.getTime() % 1000);
while (milli.length() < 3) {
milli = "0" + milli;
}
sb.append(milli);
//四位随机数
String rondom = String.valueOf(new Random().nextInt(10000));
while (rondom.length() < 4) {
rondom = "0" + rondom;
}
sb.append(rondom);
return sb.toString();
}
}
工具类
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class JxlExcelUtils {
/**
* @author
* @param objData 导出内容数组
* @param sheetName 导出工作表的名称
* @param columns 导出Excel的表头数组
* @return
*/
public static int getExcelStream(ByteArrayOutputStream os, List<VclassEnroll> list, String sheetName,List<String> columns) {
int flag = 0;
//声明工作簿jxl.write.WritableWorkbook
WritableWorkbook workbook;
try {
//根据传进来的file对象创建可写入的Excel工作薄
workbook = Workbook.createWorkbook(os);
/*
* 创建一个工作表、sheetName为工作表的名称、"0"为第一个工作表
* 打开Excel的时候会看到左下角默认有3个sheet、"sheet1、sheet2、sheet3"这样
* 代码中的"0"就是sheet1、其它的一一对应。
* createSheet(sheetName, 0)一个是工作表的名称,另一个是工作表在工作薄中的位置
*/
WritableSheet ws = workbook.createSheet(sheetName, 0);
SheetSettings ss = ws.getSettings();
ss.setVerticalFreeze(1);//冻结表头
WritableFont font1 =new WritableFont(WritableFont.createFont("微软雅黑"), 10 ,WritableFont.BOLD);
WritableFont font2 =new WritableFont(WritableFont.createFont("微软雅黑"), 9 ,WritableFont.NO_BOLD);
WritableCellFormat wcf = new WritableCellFormat(font1);
WritableCellFormat wcf2 = new WritableCellFormat(font2);
WritableCellFormat wcf3 = new WritableCellFormat(font2);//设置样式,字体
//创建单元格样式
//WritableCellFormat wcf = new WritableCellFormat();
//背景颜色
wcf.setBackground(jxl.format.Colour.YELLOW);
wcf.setAlignment(Alignment.CENTRE); //平行居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中
wcf3.setAlignment(Alignment.CENTRE); //平行居中
wcf3.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中
wcf3.setBackground(Colour.LIGHT_ORANGE);
wcf2.setAlignment(Alignment.CENTRE); //平行居中
wcf2.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中
/*
* 这个是单元格内容居中显示
* 还有很多很多样式
*/
wcf.setAlignment(Alignment.CENTRE);
//判断一下表头数组是否有数据
if (columns != null && columns.size() > 0) {
//循环写入表头
for (int i = 0; i < columns.size(); i++) {
/*
* 添加单元格(Cell)内容addCell()
* 添加Label对象Label()
* 数据的类型有很多种、在这里你需要什么类型就导入什么类型
* 如:jxl.write.DateTime 、jxl.write.Number、jxl.write.Label
* Label(i, 0, columns[i], wcf)
* 其中i为列、0为行、columns[i]为数据、wcf为样式
* 合起来就是说将columns[i]添加到第一行(行、列下标都是从0开始)第i列、样式为什么"色"内容居中
*/
ws.addCell(new Label(i, 0, columns.get(i), wcf));
}
//判断表中是否有数据
if (list != null && list.size() > 0) {
//循环写入表中数据
for (int i = 0; i < list.size(); i++) {
int j = i+1;
ws.addCell(new Label(0, j, i+""));
ws.addCell(new Label(1, j, list.get(i).getStu_name()));
ws.addCell(new Label(2, j, list.get(i).getGradeid()+""));
ws.addCell(new Label(3, j, list.get(i).getClassid()+""));
ws.addCell(new Label(4, j, list.get(i).getTelephone()+""));
}
}else{
flag = -1;
}
//写入Exel工作表
workbook.write();
//关闭Excel工作薄对象
workbook.close();
//关闭流
os.flush();
os.close();
os =null;
}
}catch (IllegalStateException e) {
System.err.println(e.getMessage());
}
catch (Exception ex) {
flag = 0;
ex.printStackTrace();
}
return flag;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 20:13:28