【Java】下载excel文件并解析excel数据
1、maven依赖
<!-- 下载文件 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.2</version> </dependency> <!-- 操作excel --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
2、代码
import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; public class aa { /** * 下载文件 * * @param url * @param outFilePath */ public void downloadFile(String url, String outFilePath) { HttpUtil.downloadFile(url, outFilePath); } /** * 解析Excel * * @param excelPath excel路径 * @param isReadDefaultSheet true-默认读取第0个sheet表 * @param sheetName 读取指定名称得sheet表(当isReadDefaultSheet=false时生效) * @return [ * {"0":"第0列第0行","1":"第0列第1行"}, * {"0":"第1列第0行","1":"第1列第1行"} * ] */ public ArrayList<JSONObject> analysisExcel(String excelPath, boolean isReadDefaultSheet, String sheetName) { XSSFWorkbook wb = null; try { wb = new XSSFWorkbook(new FileInputStream(excelPath)); } catch (IOException e) { e.printStackTrace(); } XSSFSheet sheet = null; if (isReadDefaultSheet) { sheet = wb.getSheetAt(0); } else { sheet = wb.getSheet(sheetName); } int lastRowNum = sheet.getLastRowNum() + 1; // 总行数 int lastCellNum = sheet.getRow(0).getPhysicalNumberOfCells(); // 总列数 ArrayList<JSONObject> excelDataList = new ArrayList<>(); // 循环行 for (int rowIndex = 0; rowIndex < lastRowNum; rowIndex++) { XSSFRow row = sheet.getRow(rowIndex); JSONObject rowDataJson = new JSONObject(); // 循环列 for (int colIndex = 0; colIndex < lastCellNum; colIndex++) { CellType cellTypeEnum = row.getCell(colIndex).getCellTypeEnum(); switch (cellTypeEnum.name()) { case "_NONE": break; case "NUMERIC": rowDataJson.put(String.valueOf(colIndex), String.valueOf(row.getCell(colIndex).getNumericCellValue())); break; case "STRING": rowDataJson.put(String.valueOf(colIndex), String.valueOf(row.getCell(colIndex).getStringCellValue())); break; case "FORMULA": rowDataJson.put(String.valueOf(colIndex), String.valueOf(row.getCell(colIndex).getCellFormula())); break; case "BLANK": rowDataJson.put(String.valueOf(colIndex), ""); break; case "BOOLEAN": rowDataJson.put(String.valueOf(colIndex), String.valueOf(row.getCell(colIndex).getBooleanCellValue())); break; case "ERROR": break; } } excelDataList.add(rowDataJson); } for (Object a : excelDataList) { System.out.println(a); } return excelDataList; } public static void main(String[] args) throws IOException { // 下载文件 String url = "http://www.chengdu.gov.cn/chengdu/c105449f//2020-04/15/63c83141a30244e5b340265ecad395e7/files/c44606a20b694c22aa6ddf63530e863d.pdf"; new aa().downloadFile(url,"e:/abc/test02.pdf"); // 解析excel String exP = "E:\\aaa.xlsx"; new aa().analysisExcel(exP, true, ""); } }
3、效果
3.1、下载文件
3.2、解析excel
4、备注
get
HttpRquest.get(url).header("XXX", "XXX").execute().writeBody("d:/downloadFile");
post
import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONObject; /** * @Author: * @Date: 2022/1/6 20:52 * @Description: * @Version: v1.0 */ public class aaa { public static void main(String[] args) { String url = "https://xxx.xxx.xxx.xxx/admin/statistics?"; JSONObject jsonObject = new JSONObject(); jsonObject.put("startTime",1641398400000l); jsonObject.put("endTime",1641431562957l); long l = HttpRequest.post(url) .header("Authorization", "e4f9c9db-ae6a-450b-a808-2256800fcba7") .body(jsonObject) .execute().writeBody("E:\\abc\\666.xlsx"); System.out.println(l); } }
如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2020-01-07 【IDEA】+创建一个maven项目并发布部署
2020-01-07 【远程仓库】+maven仓库jar包管理/自制maven包上传至公共仓库
2020-01-07 【模板】+【测试工作文档模板】+3交付测试用例
2020-01-07 【模板】+【测试工作文档模板】+2测分
2020-01-07 【模板】+【测试工作文档模板】+1测试策略&测试计划
2020-01-07 【Java】+操作数据库