一键上传服务器读入并写入数据库,实现批量操作
程序员文章站
2024-02-22 21:43:07
...
使用插件jquery.ocupload-1.1.2.js
编写Js脚本
<input type="file" id="excel_upload_btn" value="上传文件"/>
<script type="text/javascript">
$(function () {
$("#excel_upload_btn").upload({
action:"abc",
name:"excelFile"
});
});
</script>
使用Apache POI解析Excel
Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
Cotroller层
@Controller
@RequestMapping("bcRegion")
public class BcRegionController {
@Autowired
private BcRegionServiceI bcRegionServiceI;
@RequestMapping("import.action")
@ResponseBody
public ResponseResult importExcelFile(MultipartFile excelFile) throws Exception {
LinkedList<BcRegion> bcRegions = new LinkedList<>();
//读取文件
HSSFWorkbook xlsFile = new HSSFWorkbook(excelFile.getInputStream());
//获取工作区
HSSFSheet sheet1 = xlsFile.getSheetAt(0);
//对行扫描,
int rowNum = sheet1.getLastRowNum();
//第一行舍弃,默认为表头
for (int i = 1; i < rowNum; i++) {
HSSFRow row = sheet1.getRow(i);
BcRegion bcRegion = new BcRegion();
//获取最后一列的索引
short lastCellNum = row.getLastCellNum();
for (int j = 0; j <= lastCellNum; j++) {
String province = row.getCell(j).getStringCellValue());
String city = row.getCell(j).getStringCellValue());
String district = row.getCell(j).getStringCellValue());
String postcode = row.getCell(j).getStringCellValue());
bcRegion.setProvince(province);
bcRegion.setCity(city);
bcRegion.setDistrict(district);
bcRegion.setPostcode(postcode);
}
//设置简码和城市简码
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);
}
//保存到数据库
bcRegionServiceI.importExcelFile(bcRegions);
//关闭读写
xlsFile.close();
//前端所需的数据,可以不写
return new ResponseResult(true,null);
}
}
BcRegion为一个JavaBean
拓展知识:jpinyin生成简码
用于汉字转中文
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>
<version>1.1.7</version>
</dependency>
API使用案例
try {
String str = "你好世界";
//设置声调表示格式
System.out.println(PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK)); // nǐ,hǎo,shì,jiè
//数字表示声调
System.out.println(PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER)); // ni3,hao3,shi4,jie4
//无声调
System.out.println( PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE)); // ni,hao,shi,jie
//获取拼音首字母
System.out.println(PinyinHelper.getShortPinyin(str)); // nhsj
//判断是否多音字
System.out.println(PinyinHelper.hasMultiPinyin('啊'));//true
} catch (PinyinException e) {
e.printStackTrace();
}
总结:
使用jquery.ocupload-1.1.2.js编写upload事件,后端使用MultipartFile接收参数,利用jpinyin生成简码字段
注意: ResponseResult importExcelFile(MultipartFile excelFile)解析的方式固定了文件为xls
province | city | district | postcode |
北京市 | 北京市 | 延庆县 | 110229 |
北京市 | 北京市 | 门头沟区 | 110109 |
河北省 | 石家庄市 | 桥西区 | 130104 |
河北省 | 石家庄市 | 赵县 | 130133 |
山西省 | 太原市 | 尖草坪区 | 140108 |
河北省 | 石家庄市 | 深泽县 | 130128 |
河北省 | 石家庄市 | 栾城县 | 130124 |
上一篇: replace批量替换关键词
下一篇: 初探Java类加载机制