将byte数组转成poi需要的workbook

将byte数组转成poi需要的workbook

最近在公司遇到一个需求,要将前端传过来的byte数组生成excel,然后将数据生成一个List> 数据给下方调用者。
废话少说,直接上代码:

public static List> getExcelListMap(byte[] bytes) {
        InputStream inputStream = new ByteArrayInputStream(bytes);
        return getExcelListMap(inputStream);
    }
public static List> getExcelListMap(InputStream inputStream) {
        try {
            Workbook wb = WorkbookFactory.create(inputStream);
            Sheet sheet = wb.getSheetAt(0);
            int lastRowNumber = sheet.getLastRowNum();
            Row myRow = null;
            List> list = Lists.newArrayList();
            Map map = Maps.newHashMap();
            for (int i = 0; i <= lastRowNumber; i++) {
                if (sheet != null) {
                    Row row = sheet.getRow(i);
                    myRow = sheet.getRow(0);
                    Map tempMap = new HashMap();
                    if (row != null) {
                        int tempNum = row.getLastCellNum();
                        int nullCount = 0;
                        for (int k = 0; k < tempNum; k++) {
                            if (i == 0) {
                                map.put(String.valueOf(k), checkNull(getCellValue(row, k)));
                            } else {
                                String cellValue = getCellValue(row, k);
                                if (cellValue == null || "".equals(cellValue) || "null".equals(cellValue)) {
                                    nullCount++;
                                }
                                tempMap.put(map.get(String.valueOf(k)), checkNull(getCellValue(row, k)));
                            }
                        }
                        if (tempMap.size() > 0 && nullCount != tempNum) {
                            list.add(tempMap);
                        }
                    }
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
private static String getCellValue(Row row, int point) {
        String reString = "";
        try {
            Cell cell = row.getCell((short) point);
            if (cell.getCellType() == 1)
                reString = cell.getStringCellValue();
            else if (cell.getCellType() == 0) {
                reString = Double.toString(cell.getNumericCellValue());
                BigDecimal bd = new BigDecimal(reString);
                reString = bd.toPlainString();
            } else {
                reString = "";
            }
            System.out.println(cell.getCellType() + ":" + cell.getCellFormula());
        } catch (Exception localException) {
        }
        return checkNull(reString);
    }
     private static String checkNull(String str) {
        return StringUtils.stripToEmpty(str);
    }

反手就是一个测试,看看最后生成的结果是否符合需求:

 public static void main(String[] args) throws Exception{
        File file = new File("E:/student1.xlsx");
        InputStream inputStream = new FileInputStream(file);
        List> excelListMap = getExcelListMap(inputStream);
        System.out.println(excelListMap);
    }
    /*
    [{college=中国人民大学, major=计算机信息科学, sno=, phone=128182121, className=一班, name姓名=玛纳斯}, {college=武汉大学, major=哲学, sno=, phone=222424.0, className=二班, name姓名=托马斯}]
    */

将byte数组转成poi需要的workbook_第1张图片

你可能感兴趣的:(poi,excel)