`
bcyy
  • 浏览: 1823497 次
文章分类
社区版块
存档分类
最新评论

jsp将table导出成excel+用js将table导出excel,中文乱码问题解决方案+action层导出excel

 
阅读更多

由于产品升级,居然把以前extjs用的grid被替换成table了。。。郁闷。。。

但还要保留导出功能,没办法,只能研究。


开始我以为通过js可以完成这项任务,代码也查到了,但是它的导出结果是直接用excel打开此table,与预想的不一样:预想的是弹出框,询问用户是下载还是直接打开。

后来想了想,觉得js是运行在客户端的,所以要实现这个下载功能应该不太现实。。于是放弃用js,查新的方法:

最终,我的解决方法是,在action中通过一系列的处理,最终返回一个页面,而这个页面中,body里只有一个table,然后把这个文件的传输格式改变,最终达到目的。


先把用js直接在excel中把table打开的代码贴上来,也许日后会用上:

<script language=javascript>
function tableToExcel() {
window.clipboardData.setData("Text",document.all('table1').outerHTML);
try
{
var ExApp = new ActiveXObject("Excel.Application")
var ExWBk = ExApp.workbooks.add()
var ExWSh = ExWBk.worksheets(1)
ExApp.DisplayAlerts = false
ExApp.visible = true
}
catch(e)
{
alert("您的电脑没有安装Microsoft Excel软件!")
return false
}
ExWBk.worksheets(1).Paste;
}
</script>

用这个时要注意两点,在有的浏览器版本上,直接运行这段代码会报automation服务器不认识对象这个错误(大概是这个,昨天晚上在家里时出现的)

解决方法:把ie的安全项中用自定义:对"未识别的activeX控件"改为启用(大概是这个吧,具体名字也忘了)


比较简单吧。。下面说说用jsp导出:


这个其实更简单,只需要在生成table的页面前,加上

<%
response.setContentType("application/msexcel;charset=GBK");
response.setHeader("Content-disposition", "attachment; filename=excelname.xls");
%>


就好了。。。

要注意两点:1.生成的excel中可能没有边框,所以要在jsp的table中,加上border="1"

2.对于中文,会有乱码。

我尝试了几种方式,最终发现把所有编码都改为gbk就可以了(其他页面都用的Utf-8)



<%@ page contentType="text/html; charset=GBK"%>


response.setContentType("application/msexcel;charset=GBK");
response.setHeader("Content-disposition", "attachment; filename=excelname.xls");


注意:下面这句话也要加上:

<meta content="text/html;charset=GBK" http-equiv="Content-Type">

之所以像上面这样做(有些麻烦),是因为最终生成的table不是一个实体类的集合,而是一个dto对象的集合。所以如果用后台再去取,再转化,有些麻烦。



Action层导出:

如果直接是某个实体类对象的集合,用下面这种方法来生成excel也是可以的。

public void exportForm(){

HttpServletRequest request = ServletActionContext.getRequest();
try {
request.setCharacterEncoding("gb2312");

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String sDate = sdf.format(now);
String file = sDate+"_VehicleInfo.xls";


HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition" ,"attachment;filename="+file+"");
OutputStream os = response.getOutputStream();
String[] title = {"车牌号码","车牌颜色","车辆类型","车辆颜色","车主姓名","证件号码","通讯地址","电话号码","Email地址","停车类型","卡号","登记时间"};
int i=0;


WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("车辆信息", 0);
ws.setColumnView(0,15);
ws.setColumnView(1,15);
ws.setColumnView(2,20);
ws.setColumnView(3,15);
ws.setColumnView(4,18);
ws.setColumnView(5,15);
ws.setColumnView(6,35);
ws.setColumnView(7,15);
ws.setColumnView(8,20);
ws.setColumnView(9,15);
ws.setColumnView(10,20);
ws.setColumnView(11,20);
ws.setColumnView(12,20);

for( i=0;i<title.length;i++){
WritableFont wf = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, false);
WritableCellFormat wcfF = new WritableCellFormat(wf);
Label labelCF = new Label(i, 0, title[i],wcfF);
ws.addCell(labelCF);
}

i=1;
String szPlateNO = null;
String szPlateColor = null;
String szVehicleType = null;
String szVehicleColor = null;
String szOwnerName = null;
String szPostCard = null;
String szAddress = null;
String szTelephoneNo = null;
String szEmail = null;
String szParkingType = null;
String szCardNo = null;
String szRegeditTime = null;

//根据selectedIds,生成一个List


//List list = this.pageBean.getList();

List list = vehicleService.queryExportVehicleList(exportIds);
Iterator it = list.iterator(); // 获得一个迭代子


while (it.hasNext()) {
Vehicle v = (Vehicle)it.next(); // 得到下一个元素

szPlateNO = v.getPlateNo();
Label labelC0 = new Label(0,i,szPlateNO);
ws.addCell(labelC0);

int tmp = v.getPlateColor();
if(tmp == 1){
szPlateColor = "蓝色";
}else{
szPlateColor = "其他";
}

Label labelC1 = new Label(1,i,szPlateColor);
ws.addCell(labelC1);

tmp = v.getVehicleType();
if(tmp ==1){
szVehicleType = "军警汽车";
}else{
szVehicleType = "大型汽车";
}
Label labelC2 = new Label(2,i,szVehicleType);
ws.addCell(labelC2);


tmp = v.getVehicleColor();

if(tmp == 1){
szVehicleColor = "白色";
}else{
szVehicleColor = "其它";
}


Label labelC3 = new Label(3,i,szVehicleColor);
ws.addCell(labelC3);

szOwnerName = v.getOwnerName();
Label labelC4 = new Label(4,i,szOwnerName);
ws.addCell(labelC4);

szPostCard = v.getPostCard();
Label labelC5 = new Label(5,i,szPostCard);
ws.addCell(labelC5);

szAddress = v.getAddress();
Label labelC6 = new Label(6,i,szAddress);
ws.addCell(labelC6);

szTelephoneNo = v.getTelephoneNo();
Label labelC7 = new Label(7,i,szTelephoneNo);
ws.addCell(labelC7);

szEmail = v.getEmail();
Label labelC8 = new Label(8,i,szEmail);
ws.addCell(labelC8);

tmp = v.getParkingType();
if( tmp == 0){
szParkingType = "固定车";
}else{
szParkingType = "授权车";
}

Label labelC9 = new Label(9,i,szParkingType);
ws.addCell(labelC9);


szCardNo = v.getCardNo();
Label labelC10 = new Label(10,i,szCardNo);
ws.addCell(labelC10);

szRegeditTime = v.getRegisterTime();
Label labelC11 = new Label(11,i,szRegeditTime);
ws.addCell(labelC11);

i++;
}
//写入Exel工作表
os.flush();
wwb.write();
//关闭Excel工作薄对象
wwb.close();
os.close();
} catch (Exception e) {

e.printStackTrace();
}
}




















分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics