您的位置:首页 > 其它

ext导出 excel 一个解决方案

2010-11-17 13:41 806 查看
http://hi.baidu.com/sunxiong0/blog/item/ca4a0012caaf4e5bf819b899.html

Ext导出excel很麻烦的,我们可以打开一个window.open()或window.location.href=''发送请求;把操作excel的工作交给后台的action处理就行了。

Ext的代码:

Ext.onReady(function() {
Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'under';
var currentHeight = document.body.clientHeight;
var clientWidth = window.screen.width;
var Member = Ext.data.Record.create([{
name : 'id',
type : 'int'
}, {
name : 'devCode',
type : 'string'
}, {
name : 'devName',
type : 'string'
}, {
name : 'devModel',
type : 'string'
}, {
name : 'devDate',
type : 'string'
}, {
name : 'devTime',
type : 'string'
}, {
name : 'devReason',
type : 'string'
}, {
name : 'devState',
type : 'string'
}, {
name : 'processor',
type : 'string'
}, {
name : 'processTime',
type : 'string'
}, {
name : 'processResult',
type : 'string'
}, {
name : 'promoter',
type : 'string'
}, {
name : 'promoteTime',
type : 'string'
}, {
name : 'upgentDegree',
type : 'string'
}
, {
name : 'extStauts',
type : 'int'
}, {
name : 'reasonDescription',
type : 'string'
}, {
name : 'longTime',
type : 'string'
} ])
var itemStore = new Ext.data.GroupingStore({
groupField : 'devState',
groupOnSort : true,
sortInfo : {
field : "devState",
direction : "DESC"
},
proxy : new Ext.data.HttpProxy({
url : 'selectByToday.action'
}),
reader : new Ext.data.JsonReader({
root : 'members',
totalProperty : 'count',
fields : Member,
remoteSort : true,
applySort : {
field : "devName",
direction : "ASC"
}
}) });

// 创建工具栏
var toobar = new Ext.Toolbar([{
text : '导出Excel表格',
iconCls : 'go',
tooltip : '导出Excel表格',
handler : process }]);

// 创建选择模式对象
var cb = new Ext.grid.CheckboxSelectionModel({
singleSelect : true})
// 创建工具栏组件
var itemGrid = new Ext.grid.GridPanel({

// iconCls:'icon-grid',
id :'gridPanel',
applyTo : 'MalfunctionEquipmentGrid',
title : '当天已处理过的故障设备列表',
frame : true,// 渲染表格面板
tbar : toobar,// 顶端工具栏
store : itemStore,// 表格对应的数据集
stripeRows : true,// 显示斑马线
resizeable : true,// 设是否可以改变列宽置
collapsible : true, // 允许展开和收缩
height : Ext.get("MalfunctionEquipmentGrid").getHeight(),
bodyStyle : 'height:100%;width:100%;',
loadMask : true, // 使Gird加载数据时显示遮罩效果
autoScroll : true,
// enableColumnHide:false,//是否可以手动隐藏列
autoExpandColumn : 6,// 自动扩展最后一列
viewConfig:{
forceFit: true
// 自动充满表格
},
view : new Ext.grid.GroupingView({
forceFit : true,
groupTextTpl: '{text} (共{[values.rs.length]} 条 )',
groupByText : '使用当前字段进行分组',
showGroupsText : '表格分组',
showGroupName : true,
startCollapsed : true,
hideGroupedColumn : false
}),
sm : cb, // 设置表格的选择模式
columns : [ // 设置表的行号
cb, // 在表格中显示复选框
new Ext.grid.RowNumberer({
header : '行号',
width : 40
}), {
header : "编号",
dataIndex : 'id',
tooltip : '编号',
sortable : true,
hidden : true
}, {
header : "设备编号",
width : 50,
dataIndex : 'devCode',
tooltip : '设备编号',
sortable : true,
renderer :linker11
}, {
header : "设备名称",
width : 40,
dataIndex : 'devName',
tooltip : '设备名称',
sortable : true,
renderer : linker12
}, {
header : "设备型号",
width : 20,
dataIndex : 'devModel',

tooltip : '设备型号',
sortable : true,
renderer : linker12
}, {
header : "发生日期",
width : 35,
dataIndex : 'devDate',
tooltip : '故障发生日期',
sortable : true,
renderer : linker12
}, {
header : "发生时间",
width : 30,
dataIndex : 'devTime',
tooltip : '故障发生时间',
sortable : true,
renderer : linker12
}, {
header : "修复时间",
width : 30,
dataIndex : 'processTime',
tooltip : '故障修复时间',
sortable : true,
renderer : linker13
}, {
header : "修复耗时",
width : 30,
dataIndex : 'longTime',
tooltip : '故障修复耗时',
sortable : true
}, {
header : "处理人",
width : 20,
dataIndex : 'processor',
tooltip : '故障处理人',
sortable : true,
renderer : linker12
}, {
header : "处理结果",
width : 30,
dataIndex : 'processResult',
tooltip : '故障处理结果',
sortable : true,
renderer : linker12
}, {
header : "故障原因描述",
width : 40,
dataIndex : 'reasonDescription',
tooltip : '故障原因描述',
sortable : true,
renderer : linker12
}, {
header : "设备状态",
width : 30,
dataIndex : 'extStauts',
tooltip : '设备状态',
sortable : true,
hidden : true,
renderer : linker7
}, {
header : "处理发起人",
width : 30,
dataIndex : 'promoter',
tooltip : '故障处理发起人',
sortable : true,
hidden : true,
renderer : linker12
}
, {
header : "发起时间",
width : 30,
dataIndex : 'promoteTime',
tooltip : '故障发起时间',
sortable : true,
hidden : true,
renderer : linker12
}, {
header : "紧急程度",
width : 30,
dataIndex : 'upgentDegree',
tooltip : '紧急程度',
sortable : true,
hidden : true,
renderer : linker8
}, {
header : "处理状态",
width : 30,
dataIndex : 'devState',
sortable : true,
hidden : true
}

],
bbar : new Ext.PagingToolbar({
displayInfo : true,
afterPageText : ' 页 共 {0} 页',
beforePageText : '第 ',
firstText : '第一页 ',
prevText : '前一页',
nextText : '下一页',
lastText : '最后一页',
refreshText : '刷新',
pageSize : 30,
store : itemStore,
displayMsg : '显示第{0}条到{1}条记录,共{2}条记录',
emptyMsg : '没有记录'
}),
listeners : {

'cellclick' : function(grid, rowIndex, columnIndex, e) {
if (columnIndex == '3') {
// var smmx = new Ext.grid.CheckboxSelectionModel();
var win;
var rows = grid.getSelectionModel().getSelections();
if (rows.length > 1) {
Ext.MessageBox.alert('提示信息', '每次只能选择一个记录!');
return false;
} else {
id = rows[0].get('id');

var win3;
Ext.Ajax.request({
url : 'loadHistoryVendrecordMessageById.action?id=' + id,
waitMsg : "正在加载表单数据,请稍候...",
success : function(response, options) {
var r = Ext.decode(response.responseText);
if (r.success == true) {
var field;
if (!win) {
win3 = new Ext.Window({
layout : 'fit',
title : '查看详情',
width : 650,
height : 200,
closeAction : 'close',
plain : true,
modal : true,
html : r.data,
autoScroll : true,
buttonAlign : 'center',
buttons : [{
text : '确定',
handler : function() {
win3
.close();
}
}]
});
}
win3.show();
} else {
Ext.Msg.alert("提示信息", '查看故障设备信息失败');
}
}
});

}
}
}
}

})

itemStore.load({
params : {
start : 0,
limit : 30
}
});
window.onresize = function() {
itemGrid.setHeight(100);
itemGrid.setHeight(Ext.get('MalfunctionEquipmentGrid').getHeight());
};
function linker11(val) {
return '<u><font color="#11C2EE">'+val+'</font></u>'

}

function linker13(val) {

v = Ext.util.Format.substr(val,0,10);
v1 = Ext.util.Format.substr(val,11,16)
return "<span ext:qtip=" + v+"|"+v1 + ">" + v1 + "</span>";

}
function linker12(val) {
return "<span ext:qtip=" + val+ ">" + val + "</span>";

}
// 故障原因的显示转换
function linker(val) {
if (val == '1') {
return '通讯故障';
}
if (val == '2') {
return '模块故障';
}
if (val == '3') {
return '营业性故障';
}
if (val == '4') {
return '其它故障';
}
if (val == '5') {
return '手工录入';
}
return val;
}

function linker8(val){
if (val == '特急') {
return '<font color="#FF0000">特急</font>';
}
if (val == '紧急') {
return '<font color="#FF7F50">紧急</font>';
}
if (val == '一般') {
return '一般';
}

}

function linker1(val){
if (val == '1') {
return '<font color="#FF0000">关闭</font>';
}
if (val == '2') {
return '<font color="#FF7F50">维护</font>';
}
if (val == '3') {
return '未知';
}
if (val == '0') {
return '正常';
}
}
function linker2(val){
if (val == '1') {
return '<font color="#FF0000">通讯断</font>';
}
if (val == '2') {
return '<font color="#FF7F50">警告</font>';
}
if (val == '3') {
return '未知';
}
if (val == '0') {
return '正常';
}
if (val == '5') {
return '未录入';
}
}

function linker3(val){
if (val == '1') {
return '<font color="#FF0000">故障</font>';
}
if (val == '2') {
return '<font color="#FF7F50">警告</font>';
}
if (val == '3') {
return '未知';
}
if (val == '0') {
return '正常';
}
if (val == '5') {
return '未录入';
}
}
function linker4(val){
if (val == '1') {
return '<font color="#FF7F50">不足</font>';
}
if (val == '2') {
return '<font color="#FF0000">缺钞</font>';
}
if (val == '3') {
return '未知';
}
if (val == '0') {
return '充足';
}
if (val == '5') {
return '未录入';
}
}

function linker5(val){
if (val == '3') {
return '<font color="#FF0000">故障</font>';
}
if (val == '2') {
return '<font color="#FF7F50">缺纸</font>';
}
if (val == '1') {
return '<font color="#FF7F50">纸少</font>';
}
if (val == '0') {
return '正常';
}
if (val == '4') {
return '无';
}
if (val == '5') {
return '未录入';
}
}
function linker6(val){
if (val == '3') {
return '<font color="#FF0000">故障</font>';
}
if (val == '2') {
return '<font color="#FF7F50">缺纸</font>';
}
if (val == '1') {
return '<font color="#FF7F50">纸少</font>';
}
if (val == '0') {
return '正常';
}
if (val == '4') {
return '无';
}
if (val == '5') {
return '未录入';
}
}
function linker7(val){
if (val == '1') {
return '通讯故障';
}
if (val == '2') {
return '模块故障';
}
if (val == '3') {
return '营业性故障';
}
if (val == '0') {
return '其它故障';
}
if (val == '5') {
return '未录入';
}

}

//导出excel的函数
function process(){
Ext.MessageBox.confirm('确认提示', '你确定要导出Excel吗?',
function deldbconfig(btn) {
if (btn == 'yes') {
var msgTip = Ext.MessageBox.show({
title : '提示',
width : 350,
msg : '正在生成,请稍候......'
})
var _url = 'excelToHme.action?flag=2';
window.location.href(_url);

// window
// .open(_url, '_self',
// 'width=1,height=1,toolbar=no,menubar=no,location=no'); // 注意这里
// 用window.open()
// 否则不出现下载对话框
msgTip.hide();

}
})

}

});

后台:

/**
* 导出Excel——当天已处理的故障列表
*/
@SuppressWarnings("unchecked")
public String excelToHme() {
String flag = request.getParameter("flag");//用来区别是导出历史记录还是当天记录的
List<HistoryMalfunctionEquipment> listResult = null;
if(flag.equals("1")){//1表示历史记录
listResult = (List<HistoryMalfunctionEquipment>) session
.getAttribute("AllListResultHME");//在gird做查询的时候将查询的结果放在session里面,现在去取
}else if(flag.equals("2")){//2表示当天记录

listResult = (List<HistoryMalfunctionEquipment>) session
.getAttribute("listResultHME");
}
response.reset();
response.setContentType("application/vnd.ms-excel");

String titletop = "已处理的故障设备列表";

String[] title = { "设备编号", "设备名称", "设备型号", "故障发生日期", "故障发生时间",
"故障修复时间", "修复耗时", "故障处理人", "故障处理结果", "故障原因描述", "设备故障",
"故障处理发起人", "故障发起时间", "紧急程度", " 设备运行状态", "通讯状态", "模块状态", "钱箱状态",
"流水打印机状态", "收条打印机状态" };

List<CellView> cellviewList = new ArrayList<CellView>();

CellView cellview = null;

for (int i = 0; i < title.length; i++) {

cellview = new CellView();
cellview.setSize(4000); // 设置单元格的宽度
cellviewList.add(cellview);

}
try {

response.setHeader("Content-disposition", "attachment; filename="
+ new String("已处理的故障设备列表.xls".getBytes("GBK"),
"ISO8859_1"));

WritableWorkbook wwb;

wwb = Workbook.createWorkbook(response.getOutputStream());

WritableSheet sheet = wwb.createSheet(titletop, 0);

// 设置标题
Label label;

sheet.mergeCells(0, 0, 19, 0);// 合并单元格,

// 设置字体
jxl.write.WritableFont titalwfont = new jxl.write.WritableFont(
WritableFont.createFont("黑体"), 20);

titalwfont.setBoldStyle(WritableFont.BOLD);

WritableCellFormat titalfont = new WritableCellFormat(titalwfont);

titalfont.setAlignment(Alignment.CENTRE);

label = new Label(0, 0, titletop, titalfont);

sheet.addCell(label);
// begin
WritableCellFormat font = new WritableCellFormat();
font.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
// end
// 备注

jxl.write.WritableFont topmemowfont = new jxl.write.WritableFont(
WritableFont.createFont("宋体"), 12);

WritableCellFormat topmemofontstart = new WritableCellFormat(
topmemowfont);

topmemofontstart.setAlignment(Alignment.CENTRE);

WritableCellFormat topmemofontend = new WritableCellFormat(
topmemowfont);

topmemofontend.setAlignment(Alignment.RIGHT);

// 设值表头
Label labels;

WritableCellFormat topwcf = new WritableCellFormat();

topwcf.setAlignment(Alignment.CENTRE);

topwcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);

for (int i = 0; i < title.length; i++) {

labels = new Label(i, 1, title[i], topwcf);

sheet.setColumnView(i, cellviewList.get(i));

sheet.addCell(labels);
}

// 以下是定义格式 Label为String型的,jxl.write.Number为数字
Label devCode;
Label devName;
Label devModel;
Label devDate;
Label devTime;
Label processor;
Label processTime;
Label longTime;
Label processResult;
Label promoter;
Label promoteTime;

Label upgentDegree;
Label extStatus;

Label devStatus;
Label commStatus;

Label moduStatus;
Label boxsStatus;
Label prjStatus;
Label prrStatus;

Label reasonDescription;

WritableCellFormat containtwcf = new WritableCellFormat();

containtwcf.setAlignment(Alignment.RIGHT);

containtwcf.setBorder(Border.ALL, BorderLineStyle.THIN,
Colour.BLACK);

for (int i = 2; i < listResult.size() + 2; i++) {

devCode = new Label(0, i, listResult.get(i - 2).getDevCode(),
font);

devName = new Label(1, i, listResult.get(i - 2).getDevName(),
font);

devModel = new Label(2, i, listResult.get(i - 2).getDevModel(),
font);

devDate = new Label(3, i, listResult.get(i - 2).getDevDate(),
font);

devTime = new Label(4, i, listResult.get(i - 2).getDevTime(),
font);

processTime = new Label(5, i, listResult.get(i - 2)
.getProcessTime(), font);

longTime = new Label(6, i, listResult.get(i - 2).getLongTime(),
font);

processor = new Label(7, i, listResult.get(i - 2)
.getProcessor(), font);

processResult = new Label(8, i, listResult.get(i - 2)
.getProcessResult(), font);

reasonDescription = new Label(9, i, listResult.get(i - 2)
.getReasonDescription(), font);

extStatus = new Label(10, i, parseState(listResult.get(i - 2)
.getExtStauts(), 0), font);

promoter = new Label(11, i,
listResult.get(i - 2).getPromoter(), font);

promoteTime = new Label(12, i, listResult.get(i - 2)
.getPromoteTime(), font);

upgentDegree = new Label(13, i, listResult.get(i - 2)
.getUpgentDegree(), font);

devStatus = new Label(14, i, parseState(listResult.get(i - 2)
.getDevStatus(), 1), font);

commStatus = new Label(15, i, parseState(listResult.get(i - 2)
.getCommStatus(), 2), font);

moduStatus = new Label(16, i, parseState(listResult.get(i - 2)
.getModuStatus(), 3), font);

boxsStatus = new Label(17, i, parseState(listResult.get(i - 2)
.getBoxsStatus(), 4), font);

prjStatus = new Label(18, i, parseState(listResult.get(i - 2)
.getPrjStatus(), 5), font);

prrStatus = new Label(19, i, parseState(listResult.get(i - 2)
.getPrrStatus(), 6), font);

sheet.addCell(devCode);

sheet.addCell(devName);

sheet.addCell(devModel);

sheet.addCell(devDate);

sheet.addCell(devTime);

sheet.addCell(processTime);

sheet.addCell(longTime);

sheet.addCell(processor);

sheet.addCell(processResult);

sheet.addCell(reasonDescription);

sheet.addCell(extStatus);

sheet.addCell(promoter);

sheet.addCell(promoteTime);

sheet.addCell(upgentDegree);

sheet.addCell(devStatus);

sheet.addCell(commStatus);

sheet.addCell(moduStatus);

sheet.addCell(boxsStatus);

sheet.addCell(prjStatus);

sheet.addCell(prrStatus);

}

wwb.write();

wwb.close();

response.getOutputStream().flush();

response.getOutputStream().close();

} catch (IOException e) {
log.error(e);
} catch (RowsExceededException e) {
log.error(e);
} catch (WriteException e) {
log.error(e);
}

return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: