java读取EXCEL并且发送post请求

来源:互联网 发布:ajax实例java类代码 编辑:程序博客网 时间:2024/06/06 02:04

读取.java

package com.test.applications;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.WorkbookFactory;import org.apache.poi.ss.usermodel.DateUtil;// xmlbeans,Commons Collections,poi-ooxml-schemas除了以上引入的包、还需要加入这些jar包import javax.swing.plaf.synth.SynthEditorPaneUI;import java.io.*;import java.text.SimpleDateFormat;/** * Created by xiapf on 2017/10/19 0019. */public class readFile2 {    public static String[][] readExcel(File file){        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");        String res = ""; // 先声明一下要返回的值        try {            //同时支持Excel 2003、2007            File excelFile = file; //创建文件对象            FileInputStream is = new FileInputStream(excelFile); //文件流            Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的            int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量            //遍历每个Sheet            for (int s = 0; s < sheetCount; s++) {                Sheet sheet = workbook.getSheetAt(s);                int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数                //遍历每一行                for (int r = 0; r < rowCount; r++) {                    Row row = sheet.getRow(r);                    int cellCount = row.getPhysicalNumberOfCells(); //获取总列数                    //遍历每一列                    for (int c = 0; c < cellCount; c++) {                        Cell cell = row.getCell(c);                        int cellType = cell.getCellType();                        String cellValue = null;                        switch(cellType) {                            case Cell.CELL_TYPE_STRING: //文本                                cellValue = cell.getStringCellValue();                                break;                            case Cell.CELL_TYPE_NUMERIC: //数字、日期                                if(DateUtil.isCellDateFormatted(cell)) {                                    cellValue = fmt.format(cell.getDateCellValue()); //日期型                                }                                else {                                    cellValue = String.valueOf(cell.getNumericCellValue()); //数字                                }                                break;                            case Cell.CELL_TYPE_BOOLEAN: //布尔型                                cellValue = String.valueOf(cell.getBooleanCellValue());                                break;                            case Cell.CELL_TYPE_BLANK: //空白                                cellValue = cell.getStringCellValue();                                break;                            case Cell.CELL_TYPE_ERROR: //错误                                cellValue = "错误";                                break;                            case Cell.CELL_TYPE_FORMULA: //公式                                cellValue = "错误";                                break;                            default:                                cellValue = "错误";                        }                        if(c == cellCount-1) { // 每行的最后一个后面加的是HFGF ,区别好多行                            res = res + cellValue + "HFGF";                        }else{  // 行之间的参数为了方便拆分 所以加了FGF标识                            res = res + cellValue + "FGF";                        }                    }                }            }        }        catch (Exception e) {            e.printStackTrace();        }        String[] a = res.split("HFGF");        String[][] b = new String[a.length][];        for (int i=0; i< a.length;i++){            b[i]=a[i].split("FGF");        }        return  b;    }    public static void main(String[] args){        File file = new File("C:\\Users\\Administrator\\Desktop\\phone.xlsx");        String[][] resp = readExcel(file);        String type = "post";        String message="";        postWay res = new postWay();        testPost res1 = new testPost();        String url = "http://192.168.*.*:****/*******";        //System.out.println(resp);        for(int i=1; i < resp.length; i++){            for (int j=0; j < resp[0].length; j++){               if(j==resp[0].length-1){                   message = message + "\"" + resp[0][j] + "\":\"" + resp[i][j] + "\"";               }else{                   message = message + "\"" + resp[0][j] + "\":\"" + resp[i][j] + "\",";               }            }            /*System.out.println("{"+message+"}");*/            String data = "content={"+message+"}&urlAddress=http://127.0.0.1:8080/******";            System.out.println(data);            //发送到sendRequest            //String s = res.transRequest(url,type,data);            //发送的是json格式            //String s = res.transRequest(url,type,"{"+message+"}");            //测试message,此处直接用的是message赋值            //String s = res.transRequest(url,type,message);            //System.out.println(s);            String s1 = res1.transport(url,data);            System.out.println(s1);            message="";        }    }}
post.java

package com.test.applications;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;/** * Created by xiapf on 2017/7/11 0011. */public class testPost {    public String transport(String url, String message) {        StringBuffer sb = new StringBuffer();        try {            URL urls = new URL(url);            HttpURLConnection uc = (HttpURLConnection) urls.openConnection();            uc.setRequestMethod("POST");            uc.setRequestProperty("content-type", "application/x-www-form-urlencoded");            /*uc.setRequestProperty("content-type", "text/plain");*/            uc.setRequestProperty("charset", "UTF-8");            uc.setDoOutput(true);            uc.setDoInput(true);            uc.setReadTimeout(10000);            uc.setConnectTimeout(10000);            OutputStream os = uc.getOutputStream();            DataOutputStream dos = new DataOutputStream(os);            dos.write(message.getBytes("utf-8"));            dos.flush();            os.close();            BufferedReader in = new BufferedReader(new InputStreamReader(uc                    .getInputStream(), "utf-8"));            String readLine = "";            while ((readLine = in.readLine()) != null) {                sb.append(readLine);            }            in.close();        } catch (Exception e) {            System.out.println(e.getMessage() + ":" + e);        }        return sb.toString();    }}