OSS是一個存儲庫,可以把文件存儲在這里。
POI是,則是阿帕奇提供的一個可以操作mirco office文件的一個API。用於excel 、等文件處理。
03版的后綴是xls, HSSF 最大支持65536行
07版以后則是 .xlsx,XSSF行數沒有限制,但容易內存溢出。推薦使用SXSSF,速度快,內存小
<dependencies>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
=========================
package com.xgz.oss;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.*;
import org.junit.Test;
import java.io.*;
public class TestPoi {
//測試poi操作excel,往excel中寫內容
@Test
public void tespoi() throws IOException {
//1創建workbook,xls和xlsx new的對象不同。xlsx new的是XSSFWorkbook,xls new的是HSSFWorkbook
Workbook workbook = new XSSFWorkbook();
//2根據work創建sheet
Sheet sheet = workbook.createSheet("會員列表");
//3根據sheet行rows
Row row = sheet.createRow(0);
//4根據行創建列cell
Cell cell = row.createCell(0);
//5向cell里面設置值
cell.setCellValue("lucy");
//6使用輸出流寫入到文件中
OutputStream outputStream = new FileOutputStream("C:\\excelztest\\lucyxx.xlsx");
//7把workbook內容通過輸出流寫如文件中
workbook.write(outputStream);
//8關閉輸入流
outputStream.close();
}
//測試poi操作excel,讀excel中內容名
@Test
public void testread() throws IOException {
// 1 獲取讀取文件的輸入流
FileInputStream fileInputStream = new FileInputStream("C:\\excelztest\\lucyxx.xlsx");
//2創建workbook,需要把輸入流傳遞進去
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
//3根據workbook獲取sheet
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
//4根據sheet獲取rows
XSSFRow row = sheetAt.getRow(0);
//5根據rows獲取cells
XSSFCell cell = row.getCell(0);
//第一行第二列的值
XSSFCell cell2 = row.getCell(1);
double numericCellValue = cell2.getNumericCellValue();
System.out.println(numericCellValue);
//6根據cell獲取里面的值
String stringCellValue = cell.getStringCellValue();
//7打印讀到的值
System.out.println(stringCellValue);
//8關閉輸入流
fileInputStream.close();
}
}
