Java读取Excel文件(遍历文件内容)

本文介绍了一种使用Java读取Excel文件并将数据导入数据库的方法。通过解析Excel的每一行,提取模块名称、经理、QA经理和QA测试员等信息,并调用HTTP接口更新数据库中的模块信息。

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

最近由于需要,需要将excel中的文件内容,导入到数据库,就写了一个Java读取excel文件的方法。

核心代码如下:

package com.*.service.ExcelImport;

/**
 * @Author: 
 * @Description:
 * @Date: Created at 17:09 2019-8-25
 * @Modified By:
 */

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.*.controllers.ApiController;
import com.*.**.service.I**ModuleService;
import com.*.util.DSFClient;
import com.*.util.HttpUtil;
import jxl.*;

public class ReadXLS {
    static I**ModuleService i**ModuleService = DSFClient.get**ModuleService();

    public static void main(String args[]) {
        try {
            Workbook book = Workbook.getWorkbook(new File("C:\\Users\\*\\Desktop\\yuheng.xls"));

            //获得第一个工作簿对象
            Sheet sheet = book.getSheet(0);
            int rows = sheet.getRows();
            //循环遍历
            int flaNum = 1;
            for (int i = 0; i < rows; i++) {

                Cell cell11 = sheet.getCell(0, i);
                String moduleName = cell11.getContents();
                Cell cell12 = sheet.getCell(1, i);
                String manager = cell12.getContents();
                Cell cell13 = sheet.getCell(2, i);
                String qaManager = cell13.getContents();
                Cell cell14 = sheet.getCell(3, i);
                String qaTester = cell14.getContents();
//                i**ModuleService.getMuduleByClusterName();
//                System.out.println(moduleName + "#" + qaManager + "#" + qaTester + "#");
               /* if (!strLis.contains(moduleName)) {
                    continue;
                }*/


                Map<String, String> mapString = new HashMap<>();
                mapString.put("managerNames", manager);
                mapString.put("qaManagerName", qaManager);
                mapString.put("qaPersonName", qaTester);
                mapString.put("moduleName", moduleName);
                String httpGetResult = HttpUtil.sendGet("http://**.*-inc.com/api/moduleUpdate", mapString);
//                ApiController apiController = new ApiController();
//                apiController.updateModule("", qaManager, qaTester, moduleName);
                if (JSONObject.parseObject(httpGetResult).getIntValue("repCode") == 1) {
                    System.out.println("模块名:" + moduleName + "--->" + JSONObject.parseObject(httpGetResult).getString("repData"));
                }
                flaNum++;
            }
            System.out.println("总计:" + flaNum);
            book.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漁陽

彼此共勉,砥砺前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值