xls文檔上傳解析

來源:互聯網
上載者:User

標籤:set   except   itop   一個   int   upload   for   pos   getc   

private File xls;
    public void setXls(File xls) {
        this.xls = xls;
    }
    @Action("AreaAction_upload")
    public String upload() throws Exception {
        
        List<Area> list=new ArrayList<>();
        //建立解析xls工具類
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(xls));
        //指定解析sheet1表格
        HSSFSheet sheet = workbook.getSheet("Sheet1");
        //獲得總行數
        int rowCount = sheet.getLastRowNum();
        //遍曆取得每一個行對象
        for(int i=1;i<=rowCount;i++){
            //獲得行對象
            HSSFRow row = sheet.getRow(i);
            //獲得行中的每一個儲存格
            String id = row.getCell(0).getStringCellValue();//地區編號
            String province = row.getCell(1).getStringCellValue();//省
            String city = row.getCell(2).getStringCellValue();//市
            String district = row.getCell(3).getStringCellValue();//區
            String postcode = row.getCell(4).getStringCellValue();//郵編
            //把從表格的每一行資料封裝到每個area對象中
            Area area2=new Area(id, province, city, district, postcode);
            
            
            province=province.substring(0, province.length()-1);
            city=city.substring(0, city.length()-1);
            district=district.substring(0, district.length()-1);
            //城市全拼
            area2.setCitycode(PinYin4jUtils.hanziToPinyin(city, ""));
            //簡碼
            area2.setShortcode(StringUtils.join(PinYin4jUtils.getHeadByString(province+city+district), ""));
            
            list.add(area2);
        }
        areaService.save(list);
        return "toList";
    }

xls文檔上傳解析

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.