JAva excelテーブルのアップロード

1974 ワード

package com.zuma.website.elasticsearch.controller;
import com.zuma.common.utils.rpc.ResultMessage;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by futianwei on 2019/4/3.
 */
@Controller
@RequestMapping("/search-api")
@ResponseBody
public class NewsInfoSearchControllerTest {





    @RequestMapping("/doTestFile")
    public ResultMessage doTestFile(MultipartFile file) {
        ResultMessage res=new ResultMessage();
        res.setMessage("success");
        try{
            //      
            String fileName="";
            if(file==null){
                fileName="    ";
            }else{
                fileName=file.getOriginalFilename();
                if(fileName.endsWith(".xls")){
                    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(file.getInputStream()));
                    HSSFSheet sheet = wb.getSheetAt(0);
                    for(int j=0;j