通过需求,要对上传的EXCEL文件进行数据读取并入库。由于EXCEL是由前端直传到阿里云,所以只有一个上传后的文件路径。对于先下载在读取在删除的方式觉得十分耗时且无用,所以试图直接根据URL地址来读取流,生成EXCEL对象并读取数据。代码如下:
public static void main(String[] args) throws IOException { URL httpurl=new URL("http://*******.xlsx"); URLConnection urlConnection = httpurl.openConnection(); InputStream is = urlConnection.getInputStream(); XSSFWorkbook wb = new XSSFWorkbook (is); XSSFSheet sheet = wb.getSheetAt(0); int rowNo = sheet.getLastRowNum(); for (int i = 1; i < rowNo; i++) {s XSSFRow row = sheet.getRow(i); XSSFCell cell1 = row.getCell((short) 1); XSSFCell cell2 = row.getCell((short) 2); XSSFCell cell3 = row.getCell((short) 3); String ce1 = cell1 == null?"空":cell1.g