java Excel解析

package com.duia.web.controller.bang;

import com.duia.common.bean.MyPageModel;
import com.duia.common.bean.ParamUtil;
import com.duia.common.bean.ResponseJsonModel;
import com.duia.common.bean.annotation.PermissionAnn;
import com.duia.common.constant.HttpConstant;
import com.duia.model.bang.duiba.DuibaStar;
import com.duia.model.mars.app.StarConfig;
import com.duia.model.mars.system.SysDict;
import com.duia.service.bang.duiba.spi.IDuibaStarService;
import com.duia.service.mars.app.spi.IStarConfigService;
import com.duia.service.mars.system.spi.ISysDictService;
import com.duia.web.controller.BaseController;
import com.duia.web.controller.learn.SpokenController;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
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.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by xiaochao on 2016/12/21.
 */
@Controller
@RequestMapping("/duibaStar")
public class DuibaStarController extends BaseController{
    private static Logger logger = Logger.getLogger(SpokenController.class);

    public List<String> uploadExcel(HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        List<String> list = new ArrayList<String>();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile excel = entity.getValue();
            try {
                String type = excel.getOriginalFilename().substring(excel.getOriginalFilename().lastIndexOf("."), excel.getOriginalFilename().length());
                Workbook wb = null;
                if (type.equals(".xls")) {//2003
                    wb = new HSSFWorkbook(excel.getInputStream());
                }
                if (type.equals(".xlsx")) {//2007
                    wb = new XSSFWorkbook(excel.getInputStream());
                }
                return read(wb);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    /**
     * 需要多列的值则添加 case  1 ; case 2 ...
     * @param wb
     * @return
     * @throws Exception
     */
    private  List<String> read(Workbook wb) throws Exception {
        List<String> emailList = new LinkedList<String>();
        try {
            Sheet sheet = wb.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();
            for (int r = 0; r < rows; r++) {
                // 定义 row
                Row row = sheet.getRow(r);
                if (row != null) {
                    int cells = row.getPhysicalNumberOfCells();
                    for (short c = 0; c < cells; c++) {
                        Cell cell = row.getCell(c);
                        cell.setCellType(cell.CELL_TYPE_STRING);
                        switch (c){
                            case 0:
                                emailList.add(cell.getStringCellValue());
                                break;

                            default:
                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return emailList;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值