maven中Excel 表格导入导出 (使用POI ) 加 拼音 项目一

//excel保存到数据库 (导入)
toolbar工具栏 { id : 'button-import', text : '导入', iconCls : 'icon-redo' }
执行方法

	//绑定一键上传按钮
			$("#button-import").upload({
				action:"${pageContext.request.contextPath}/bcRegion/import.action",
				name:"excelFile"
			});
			

后台:

@Controller
@RequestMapping("bcRegion")
//区域设置
public class BcRegionController {
   @Autowired
    private BcRegionServiceI bcRegionServiceI;

    @RequestMapping(value = "import.action",method = RequestMethod.POST)
    @RequiresRoles(value = {"admin"})
    public String importExcel(MultipartFile excelFile) throws IOException, PinyinException {

        ArrayList<BcRegion> bcRegions = new ArrayList<>();

        try {
            HSSFWorkbook workbook = new HSSFWorkbook(excelFile.getInputStream());
            HSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for(int i=1;i<=lastRowNum;i++){
                HSSFRow row = sheet.getRow(i);
                BcRegion bcRegion = new BcRegion();
                bcRegion.setId(UUID32Gen.gen32UUIDString());
                short lastCellNum = row.getLastCellNum();
                for(int j=0;j<lastCellNum;j++){

                    switch (j){
                        case 1:
                            bcRegion.setProvince(row.getCell(1).getStringCellValue()); //省
                            break;

                        case 2:
                            bcRegion.setCity(row.getCell(2).getStringCellValue()); //市
                            break;

                        case 3:
                            bcRegion.setDistrict(row.getCell(3).getStringCellValue()); //省
                            break;

                        case 4:
                            bcRegion.setPostcode(row.getCell(4).getStringCellValue()); //邮编
                            break;

                    }


                }


                //设置简码和城市简码
                String province = bcRegion.getProvince();
                String city = bcRegion.getCity();
                String district = bcRegion.getDistrict();

                province=province.substring(0,province.length()-1);
                city=city.substring(0,city.length()-1);
                district=district.substring(0,district.length()-1);


                bcRegion.setCitycode(PinyinHelper.convertToPinyinString(city,"", PinyinFormat.WITHOUT_TONE));//城市简码 taiyuan
                bcRegion.setShortcode(PinyinHelper.getShortPinyin(province+city+district));//简码 sxtyqx
                bcRegions.add(bcRegion);



            }

            //excel保存到数据库
            this.bcRegionServiceI.saveAll(bcRegions);


        }catch (Exception e){

            e.printStackTrace();

        }

        return "base/region";
    }

依赖jarb包:


        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
        </dependency>


        <dependency>
            <groupId>com.github.stuxuhai</groupId>
            <artifactId>jpinyin</artifactId>
        </dependency>

要在父工程 设置jar包版本问题
代码:

 <properties>
 <apache.poi.version>4.0.1</apache.poi.version>
        <jpinyin.version>1.1.7</jpinyin.version>
    </properties>



<dependencies>
        <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${apache.poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${apache.poi.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${apache.poi.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.stuxuhai</groupId>
                <artifactId>jpinyin</artifactId>
                <version>${jpinyin.version}</version>
            </dependency>

        </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值