java控制台网页_网页数据获取小案例(java控制台程序)

1 importjava.io.FileOutputStream;2 importjava.text.SimpleDateFormat;3 importjava.util.ArrayList;4 importjava.util.Date;5 importjava.util.List;6

7 importorg.apache.poi.hssf.usermodel.HSSFCell;8 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;9 importorg.apache.poi.hssf.usermodel.HSSFRow;10 importorg.apache.poi.hssf.usermodel.HSSFSheet;11 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;12

13 /**

14 * 程序入口15 *@authorDSH16 *17 */

18 public classOutput {19

20 @SuppressWarnings({ "deprecation", "unchecked", "rawtypes"})21 public static void main(String[] args) throwsException {22

23 //创建一个webbook,对应一个Excel文件

24 HSSFWorkbook wb = newHSSFWorkbook();25 //在webbook中添加一个sheet,对应Excel文件中的sheet

26 HSSFSheet sheet = wb.createSheet("POI数据");27 //在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

28 HSSFRow row = sheet.createRow((int) 0);29 //创建单元格,并设置值表头 设置表头居中

30 HSSFCellStyle style =wb.createCellStyle();31 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式32

33 //设置表头

34 HSSFCell cell = row.createCell((short) 0);35 cell.setCellValue("名称");36 cell.setCellStyle(style);37 cell = row.createCell((short) 1);38 cell.setCellValue("地址");39 cell.setCellStyle(style);40 cell = row.createCell((short) 2);41 cell.setCellValue("电话号码");42 cell.setCellStyle(style);43 cell = row.createCell((short) 3);44 cell.setCellValue("所属分类");45 cell.setCellStyle(style);46 cell = row.createCell((short) 4);47 cell.setCellValue("所在省份");48 cell.setCellStyle(style);49 cell = row.createCell((short) 5);50 cell.setCellValue("所在区县");51 cell.setCellStyle(style);52 cell = row.createCell((short) 6);53 cell.setCellValue("详细地址");54 cell.setCellStyle(style);55 cell = row.createCell((short) 7);56 cell.setCellValue("所属标签");57 cell.setCellStyle(style);58 cell = row.createCell((short) 8);59 cell.setCellValue("大地坐标——X");60 cell.setCellStyle(style);61 cell = row.createCell((short) 9);62 cell.setCellValue("大地坐标——Y");63 cell.setCellStyle(style);64 cell = row.createCell((short) 10);65 cell.setCellValue("火星坐标——X");66 cell.setCellStyle(style);67 cell = row.createCell((short) 11);68 cell.setCellValue("火星坐标——Y");69 cell.setCellStyle(style);70 cell = row.createCell((short) 12);71 cell.setCellValue("百度坐标——X");72 cell.setCellStyle(style);73 cell = row.createCell((short) 13);74 cell.setCellValue("百度坐标——Y");75 cell.setCellStyle(style);76 cell = row.createCell((short) 14);77 cell.setCellValue("条目");78 cell.setCellStyle(style);79

80

81 List list = null;82 List listAll = new ArrayList();//保存全部数据

83 for (int i = 1; i <= 2; i++) {//从第一页开始,,,,第二页截止(包含第二页)84 HtmlParseUtil htmlParseUtil = newHtmlParseUtil();85 //获取详细页

86 list = htmlParseUtil.getContents("http://www.poi86.com/poi/district/1550/"+i+".html",i);87 listAll.addAll(list);//将所有数据放到一个list集合中

88 }89

90 for (int i = 0; i < listAll.size(); i++) {91 row = sheet.createRow((int) i + 1);92

93 Content con =(Content) listAll.get(i);94 //创建单元格,并设置值

95 row.createCell((short) 0).setCellValue(con.getName());96 row.createCell((short) 1).setCellValue(con.getAddress());97 row.createCell((short) 2).setCellValue(con.getPhone());98 row.createCell((short) 3).setCellValue(con.getType());99 row.createCell((short) 4).setCellValue(con.getProvince());100 row.createCell((short) 5).setCellValue(con.getArea());101 row.createCell((short) 6).setCellValue(con.getAddress_detail());102 row.createCell((short) 7).setCellValue(con.getLabel());103 row.createCell((short) 8).setCellValue(con.getDd_x());104 row.createCell((short) 9).setCellValue(con.getDd_y());105 row.createCell((short) 10).setCellValue(con.getHx_x());106 row.createCell((short) 11).setCellValue(con.getHx_y());107 row.createCell((short) 12).setCellValue(con.getBd_x());108 row.createCell((short) 13).setCellValue(con.getBd_y());109 row.createCell((short) 14).setCellValue(con.getRecord());110 }111 //将文件存到指定位置

112 try{113 Date currentTime = newDate();114 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");115 String date =formatter.format(currentTime);116

117 FileOutputStream fout = new FileOutputStream("F:/content" + date + ".xls");118 wb.write(fout);119 fout.close();120 } catch(Exception e) {121 e.printStackTrace();122 }123 }124 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值