1.读取excel文件的代码
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import net.sf.json.JSONObject;
public class POIReadExcelTool{
static JSONObject json;
static String url=null;
static String result;
static String param=null;
public static void main(String[] args) throws Exception {
ArrayList<Integer> list=getResult();
for(int i=0;i<list.size();i++) {
System.out.println( list.get(i));
}
QueryExcel.readandwrite();
}
public static ArrayList<Integer> getResult() throws Exception {
ArrayList<Integer> errors=new ArrayList<Integer>();
List<LoginInfo> list = readXls();
for(LoginInfo info : list){
//System.out.println(info.getUrl());
url=info.getUrl();
param=info.getName()+"&"+info.getPassword();
System.out.println(param+"3");
result=sendPost(url,param);
//解析json代码
json = JSONObject.fromObject(result);
errors.add(Integer.parseInt(json.getString("errno")));
}
return errors;
}
private static String toRunByGET(String url) throws ClientProtocolException, IOException {
String response = null;
DefaultHttpClient httpclient = null;
HttpResponse httpresponse = null;
httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpresponse = httpclient.execute(httpGet);
response = EntityUtils.toString(httpresponse.getEntity());
return response;
}
public static List<LoginInfo> readXls() throws Exception {
InputStream is = new FileInputStream("D:/info.xls");
HSSFWorkbook excel = new HSSFWorkbook(is);
LoginInfo info = null;
List<LoginInfo> list = new ArrayList<LoginInfo>();
System.out.println(excel.getNumberOfSheets()+"数据行数");
// 循环工作表Sheet
for (int numSheet = 0; numSheet < excel.getNumberOfSheets(); numSheet++) {
HSSFSheet sheet = excel.getSheetAt(numSheet);
if (sheet == null)
continue;
// 循环行Row
System.out.println(sheet.getLastRowNum()+"数据行数");
for (int rowNum = 1; rowNum <=sheet.getLastRowNum(); rowNum++) {
HSSFRow row = sheet.getRow(rowNum);
if (row == null)
continue;
info = new LoginInfo();
HSSFCell cell0 = row.getCell(0);
if (cell0 == null)
continue;
info.setId((int)cell0.getNumericCellValue());
HSSFCell cell1 = row.getCell(1);
if (cell1 == null)
continue;
info.setUrl(cell1.getStringCellValue());
HSSFCell cell2 = row.getCell(2);
if (cell2 == null)
continue;
info.setName(cell2.getStringCellValue());
HSSFCell cell3 = row.getCell(3);
if (cell3 == null)
continue;
info.setPassword(cell3.getStringCellValue());
HSSFCell cell4 = row.getCell(4);
if (cell4== null)
continue;
info.setExcepedresult(cell4.getStringCellValue());
HSSFCell cell5 = row.getCell(5);
if (cell5== null)
continue;
info.setReallyresult(cell5.getStringCellValue());
list.add(info);
}
}
return list;
}
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
@SuppressWarnings("unused")
private static String getValue(HSSFCell cell) {
if (cell.getCellType() == CellType.BOOLEAN) {
// 返回布尔类型 值
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
//返回数值类型的值
return String.valueOf(cell.getNumericCellValue());
} else {
//返回字符串类型的值
return cell.getStringCellValue();
}
}
}
写入excel读取结果的代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class QueryExcel {
public static void readandwrite() {
// 读取Excel文件
File file = new File(“D:/info.xls”);
try {
//得到所有数据
List<List> allData=readExcel(file);
//直接将它写到excel中
makeExcel(allData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static List<List> readExcel(File file) throws Exception {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// 只有一个sheet,直接处理
//创建一个Sheet对象
Sheet sheet = wb.getSheet(0);
// 得到所有的行数
int rows = sheet.getRows();
// 所有的数据
List<List<String>> allData = new ArrayList<List<String>>();
// 越过第一行 它是列名称
for (int j = 1; j < rows; j++) {
List<String> oneData = new ArrayList<String>();
// 得到每一行的单元格的数据
Cell[] cells = sheet.getRow(j);
for (int k = 0; k < cells.length; k++) {
oneData.add(cells[k].getContents().trim());
}
// 存储每一条数据
allData.add(oneData);
// 打印出每一条数据
System.out.println(oneData);
}
return allData;
}
public static void makeExcel(List<List<String>> result) throws Exception {
//第一步,创建一个workbook对应一个excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//第二部,在workbook中创建一个sheet对应excel中的sheet
HSSFSheet sheet = workbook.createSheet("测试结果");
//第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
HSSFRow row = sheet.createRow(0);
//第四步,创建单元格,设置表头
HSSFCell cell = row.createCell(0);
cell.setCellValue("id号码");
cell = row.createCell(1);
cell.setCellValue("请求地址");
cell = row.createCell(2);
cell.setCellValue("用户名");
cell = row.createCell(3);
cell.setCellValue("密码");
cell = row.createCell(4);
cell.setCellValue("预期结果");
cell = row.createCell(5);
cell.setCellValue("实际结果");
cell = row.createCell(6);
cell.setCellValue( "通过与否");
cell = row.createCell(7);
//第五步,写入数据
System.out.println(result.size()+"asdas");
for(int i=0;i<result.size();i++) {
List<String> oneData = result.get(i);
HSSFRow row1 = sheet.createRow(i + 1);
for(int j=0;j<oneData.size();j++) {
//创建单元格设值
System.out.println("sxd"+oneData.size());
row1.createCell(j).setCellValue(oneData.get(j));
if(j==5) {
char[] s=POIReadExcelTool.getResult().toString().toCharArray();
row1.createCell(5).setCellValue(POIReadExcelTool.getResult().get(i));
}
/* if(j==6) {
row1.createCell(6).setCellValue(POIReadExcelTool.getResult().get(i));
}*/
}
}
//将文件保存到指定的位置
try {
FileOutputStream fos = new FileOutputStream("E:\\result.xls");
workbook.write(fos);
System.out.println("写入成功");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取xml格式的请求报文
public static String sendByXml() throws DocumentException {
ArrayList list=new ArrayList();
list=readfromxml();
String url=list.get(0);
System.out.println(url.equals("http://u.house.ifeng.com/user/login/validate?"));
String mobile=list.get(1);
System.out.println(mobile.equals("13133814608"));
String pwd=list.get(2);
System.out.println(pwd.equals("933032shixinfa"));
String data=mobile+"&"+pwd;
System.out.println(data);
String resultdata=sendPost(url,data);
System.out.println(resultdata);
return resultdata;
}
public static ArrayList<String> readfromxml() throws DocumentException{
ArrayList<String> listdata=new ArrayList<String>();
SAXReader reader = new SAXReader();
Document document = reader.read(new File("C:\\Users\\20433\\Desktop\\xmllogin.xml"));
//获取文档根节点
Element root = document.getRootElement();
//输出根标签的名字
System.out.println(root.getName());
//获取根节点下面的所有子节点(不包过子节点的子节点)
List<Element> list = root.elements() ;
//遍历List的方法
for (Element e:list){
listdata.add(e.getStringValue());
}
/* System.out.print("+\"-----------------------------------\"");
String url=root.element("url").getStringValue();//首先要知道自己要操作的节点。
System.out.println(url);
String mobile=root.element("mobile").getStringValue();//首先要知道自己要操作的节点。
System.out.println(mobile);
String pwd=root.element("pwd").getStringValue();//首先要知道自己要操作的节点。
System.out.println(pwd);*/
/* @SuppressWarnings("unchecked")
List<Element> contactList = contactElem.elements();
for (Element e:contactList){
System.out.println(e.getName());
}*/
for(int i=0;i<listdata.size();i++) {
System.out.println(listdata.get(i));
}
return listdata;
}
请求的excel的格式
返回的excel格式的报文