java读取EXCEL并且发送post请求

本文介绍如何使用Java读取Excel文件内容,并结合HTTP POST请求将数据发送到指定服务器。通过实例代码,理解这一过程的关键步骤和技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取.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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值