实现下载网页数据,并生成xls文件
实现下载网页数据,并生成xls文件
思路:
知识点:
- 用datatable实现表格信息录入
- 用ajaxx传递在datatable选中的行的信息
- 用split拿到一串用 , 隔开的数据
- 对excle文件的操作
例子:
1. 新建文件:objects.json(以json结尾) 2
2. 添加数据:(必须以这样的格式添加数据,在[ ]里面添加数据) 2
(1) 在strurs.xml添加对应的shili.action 5
(2) 在org.ceshi.ceshi添加shilit方法 5
4. 这里以将shuju转换成了一个没有,的二维数组ss 6
{
"draw": 1,
"recordsTotal": 200,
"recordsFiltered": 200,
"data":[
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
}
,。。。。。。
]
}
- 导入头文件(bootstrap.css文件 jquery.dataTable.css文件 jquery.js文件 bootstrap.js文件 jquery.dataTable.js文件 )
<link rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link
href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"
rel="stylesheet" />
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script
src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script
src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
- 写表格
<table id="example"class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Name</th><th>Position</th><th>Office</th>
<th>Extn.</th><th>Start date</th><th>Salary</th>
</tr>
</thead>
</table>
样式:
- 写入两个button:
<button class="btn btn-sm btn-primary m-t-n-xs" type="submit"id="shengcheng">
<strong>生成表格</strong>
</button>
<button class="btn btn-sm btn-primary m-t-n-xs" type="submit"id="xiazai">
<strong>下载</strong>
/button>
- 给example表格添加数据
<script type=”text/javascript”>
$(function(){
var s=$(‘#example’).DataTable({
serviceSize:true,
ajax:{
url:’objects.json’, //这个url的地址必须是刚才准备的数据源的位置
dataSrc:’data’
type:’post’
},
columns:[
{“data”,”name”},
{“data”,”position”},
{“data”,”office”},
{“data”,”extn”},
{“data”,”start_date”},
{“data”,”salary”}
]
});
});
</script>
效果:
- 添加选中样式
- 选中的style样式
<style type=”text/css”>
.selected{
background-color:#a7aaab;
cursor:pointer; //让光标变成手的形状
}
</style>
- 给选中的行添加 selectd样式 这部分是jquery知识 用到了on toggleClass方法
$(‘#example tbody’).on(‘click’,’tr’,function({
$(this).toggleClass(‘selected’);
});
- 效果:
- 下载功能的实现
$(‘#shengcheng’).click(function(){
var stu=””;//用于拼接字符串
if($(“#example”).DataTable().rows(‘.selected’).data().length<=0){
}else{
s.rows(‘.selected’).data().each(function(value,index){
stu+=value['name']+','+value['position']+','+value['office']+','+value['extn']+','+value['start_date']+','+value['salary']+',';
});
}
alert("你选中 了:" + s.rows('.selected').data().length + "项" + stu);
//上面是用来拼接选中的信息
//下面是用来传递拼接好的字符串
sc(stu);
});
function sc(stu){
$.ajax({
url:”shili.action”,
type :’post’,
data:{
“shuju”:stu
},
async:false,
dataType:”json”,
success :function(){
alert(“表格生成完毕”);
};
});
};
<action name="shilit" class="org.ceshi.ceshi" method="shilit">
<result type="json" name="success">
<param name="root">s</param>
</result>
</action>
Airi Satou,Accountant,Tokyo,5407,2008/11/28,$162700,
String []s=shuju.split(","); System.out.println(s.length); String ss[][] = new String[1000][1000]; int j=0; int i; for (i=0; i < (s.length/6); i++) { while(j!=-1){ ss[i][j]=s[j]; System.out.print(ss[i][j]+" "); j++; if(j%6==0){ System.out.println(); break; } } }
|
操作excle文件需要导入架包可以在网上下载
//获取路径 String path = ServletActionContext.getServletContext().getRealPath("/excleFile"); //创建文件 File file=new File(path+’/cj.xls’); //判断文件是否存在,不存在的话 创建新的 if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } file.createNewFile(); //创建工作簿 WritableWorkbook wwb=Workbook.createWorkbook(file); //创建表 sheet WritableSheet ws=wwb.createSheet(“sheet0”,0); //创建Label Label one = new Label(0, 0, "name"); Label two = new Label(1, 0, "position"); Label three = new Label(2, 0, "office"); Label four = new Label(3, 0, "extn"); Label five = new Label(4, 0, "start"); Label six = new Label(5, 0, "salary"); 注意 label里面第一个参数是列 第二个参数是行 //将label添加到ws里面 ws.addCell(one); ws.addCell(two); ws.addCell(three); ws.addCell(four); ws.addCell(five); ws.addCell(six); //循环ss二维数组,并将数据添加到wa里面 int m=0;// int ms=0;//列 for(i=0;i<s.length/6;i++){ while(m!=-1){ ws.addCell(new Label(ms,i+1,ss[i][m])); System.out.println("值为:"+ss[i][m]+" "); System.out.println("ms="+ms+"i+1:"+(i+1)); m++;ms++; if(ms==6) { ms=0; } if(m%6==0){ break; } }} wwb.write(); wwb.close(); |
$(“#xiazai”).click(functio1n(){
window.location.href=”http://IP地址/工程名/excleFile/cj.xls”;
});
finished
效果:
网页选中这三条数据
点击生成按钮
点击下载按钮
查看数据