Java 导入Excel竖版表头

@PostMapping("/importFilePortrait.do")
    @ApiOperation(value = "poi导入文件(竖版模板)")
    public void importFilePortrait(MultipartFile file) 
    {
        employeeService.importFilePortrait(file);
    }

    @PostMapping("/importZipFileContainsExcel.do")
    @ApiOperation(value = "poi导入zip 包含多个excel")
    public void importZipFileContainsExcel(MultipartFile file) 
    {
        employeeService.importZipFileContainsExcel(file);
    }

	@Override
    public void importFilePortrait(MultipartFile file) 
    {
        try 
        {
            //获取上传文件的对象
            Workbook hssfWorkbook = WorkbookFactory.create(file.getInputStream());
            // Workbook hssfWorkbook = new HSSFWorkbook(inputStream);
            Map<String, String> map = readExcel(hssfWorkbook);
            System.out.println(map);
        }
        catch (Exception e) 
        {
            log.error(e.getMessage());
        }
    }

    @Override
    public void importZipFileContainsExcel(MultipartFile file) 
    {
        try 
        {
            InputStream inputStream = file.getInputStream();
            // 如果文件名称包含中文,需要加上Charset.forName("gbk")
            ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("gbk"));
            ZipEntry zipEntry = null;

            while ((zipEntry = zipInputStream.getNextEntry()) != null) 
            {
                if (!zipEntry.isDirectory() && (zipEntry.getName().endsWith(".xlsx") || zipEntry.getName().endsWith(".xls"))) 
                {
                    // Read the Excel file from the Zip entry
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[4096];
                    int length = -1;
                    while ((length = zipInputStream.read(buffer)) != -1) 
                    {
                        outputStream.write(buffer, 0, length);
                    }
                    Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(outputStream.toByteArray()));
                    Map<String, String> map = readExcel(workbook);
                    System.out.println(map);
                    zipInputStream.closeEntry();
                }
            }

            zipInputStream.close();
        } 
        catch (Exception e) 
        {
            log.error(e.getMessage());
        }
    }

	public Map<String, String> readExcel(Workbook  workbook) throws Exception 
    {

        Map<String, String> hashMap = new HashMap<>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++)
        {
            Sheet hssfSheet = workbook.getSheetAt(numSheet);
            if (hssfSheet == null) 
            {
                continue;
            }
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) 
            {
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) 
                {
                    Cell key = hssfRow.getCell(0);
                    Cell cell = hssfRow.getCell(1);
                    //解析文件中各种类型的数据信息
                    String stringCellValue = new DataFormatter().formatCellValue(cell);
                    //key值为去空格的值
                    hashMap.put(key.toString().trim(), stringCellValue);
                }
            }

        }
        workbook.close();
        return hashMap;
    }

引用文章:https://blog.csdn.net/qq_41915325/article/details/130721105

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值