1. 程式人生 > >通過阿里雲的上傳路徑獲取EXCEL檔案進行資料讀取

通過阿里雲的上傳路徑獲取EXCEL檔案進行資料讀取

通過需求,要對上傳的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.getStringCellValue(); String ce2 = cell2 == null?"空":cell2.getStringCellValue(); String ce3 = cell3 == null?"空":cell3.getStringCellValue(); System.out.println(ce1 + "\t" + ce2 + "\t" + ce3); } }

期間出現的問題有:

<dependency>
   <groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency>
URL httpurl=new URL("http://media-zoomdu.oss-cn-shanghai.aliyuncs.com/trip-file/2640/55443249742470250302167.xlsx");
URLConnection urlConnection = httpurl.openConnection();
InputStream is = urlConnection.getInputStream();
XSSFWorkbook wb = new XSSFWorkbook (is);
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.8</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml-schemas</artifactId>
   <version>3.8</version>
</dependency>
<dependency>
   <groupId>org.apache.xmlbeans</groupId>
   <artifactId>xmlbeans</artifactId>
   <version>2.6.0</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.8</version>
</dependency>
<dependency>
   <groupId>commons-collections</groupId>
   <artifactId>commons-collections</artifactId>
   <version>3.2.1</version>
</dependency>
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-collections4</artifactId>
   <version>4.1</version>
</dependency>