实现下载网页数据,并生成xls文件

实现下载网页数据,并生成xls文件

思路:

知识点:

  1. 用datatable实现表格信息录入
  2. 用ajaxx传递在datatable选中的行的信息
  3. 用split拿到一串用 , 隔开的数据
  4. 对excle文件的操作

例子:

 

目录

一:准备数据源 2

1. 新建文件:objects.json(以json结尾) 2

2. 添加数据:(必须以这样的格式添加数据,在[ ]里面添加数据) 2

二:新建网页:Myjsp.jsp 2

① 生成表格按钮 点击事件 4

扫描二维码关注公众号,回复: 5905965 查看本文章

② 添加ajax响应事件 5

(1) 在strurs.xml添加对应的shili.action 5

(2) 在org.ceshi.ceshi添加shilit方法 5

1. 定义变量 shuju并生成get set方法 5

private String shuju; 5

2. 输出shuju的值是(注意是以  ,  隔开) 5

3. 这里要把 , 去掉 用到split方法 5

4. 这里以将shuju转换成了一个没有,的二维数组ss 6

5. 将数组的信息写入到excle的文件里 6

6. 最后返回 success 7

③ 下载表格按钮 7

finished 8

 

一:准备数据源

  1. 新建文件:objects.json(以json结尾)
  2. 添加数据:(必须以这样的格式添加数据,在[ ]里面添加数据)

{

"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"

}

,。。。。。。

]

}

二:新建网页:Myjsp.jsp

  1. 导入头文件(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>

  1. 写表格

<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>

样式:

  1. 写入两个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>

  1. 给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>

效果:

  1. 添加选中样式
  • 选中的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’);

});

  • 效果:

  1. 下载功能的实现
  • 生成表格按钮 点击事件

$(‘#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(“表格生成完毕”);

};

});

};

  • 添加ajax响应事件
  1. 在strurs.xml添加对应的shili.action

<action name="shilit" class="org.ceshi.ceshi" method="shilit">

    <result type="json" name="success">

       <param name="root">s</param>

    </result>

</action>

  1. org.ceshi.ceshi添加shilit方法
  1. 定义变量 shuju并生成get set方法

private String shuju;

  1. 输出shuju的值是(注意是以  ,  隔开)

Airi Satou,Accountant,Tokyo,5407,2008/11/28,$162700,

  1. 这里要把 , 去掉 用到split方法

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;

}

}

}

例子:

private static void testSplit() {

        String ss = ",aa,bb,cc,dd,,,";

        String[] array = ss.split(",");

        System.out.println(array.length);//结果是5,而不是预想中的8

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

            System.out.println(array[i]);

        }

      //aa bb cc dd

}

 

 

 

  1. 这里以将shuju转换成了一个没有,的二维数组ss
  2. 将数组的信息写入到excle的文件里

操作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();

 

  1. 最后返回 success
  1.  
  • 下载表格按钮

$(“#xiazai”).click(functio1n(){

  window.location.href=”http://IP地址/工程名/excleFile/cj.xls”;

});

finished

 

 

效果:

网页选中这三条数据

点击生成按钮

点击下载按钮

查看数据

猜你喜欢

转载自blog.csdn.net/qq_39860799/article/details/81903729