阿里OSS以及POI之excel上传

OSS是一个存储库,可以把文件存储在这里。

POI是,则是阿帕奇提供的一个可以操作mirco office文件的一个API。用于excel 、等文件处理。

03版的后缀是xls,07版以后则是 .xlsx

    <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(); } }

猜你喜欢

转载自www.cnblogs.com/xgzzzy16/p/13195886.html