从excel中转存sql
1、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.20.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cb.tool</groupId>
<artifactId>analyse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>analyse</name>
<description>analyse tool</description> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
<commons-lang3.version>3.6</commons-lang3.version>
<fastjson.version>1.2.31</fastjson.version>
</properties> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency> <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- start https -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- end https --> <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.3</version>
</dependency> <dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- maven打包插件 end -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>analyse</warName>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build> </project>
2、controller层
@RequestMapping("/sumUpdate")
@ResponseBody
public String updateDealSumInfo() { realTimeDealService.updateDealSumInfo();
return "sumUpdate ok";
}
3、service层
@Override
public void updateDealSumInfo() {
String dirPath = PropConfig.getXls();
File dir = new File(dirPath);
File[] fs = dir.listFiles();
StringBuilder sb=new StringBuilder();
if (fs != null) {
for (File f : fs) {
if (f.isFile()) {
String fName = f.getName().split("\\.")[0]; HSSFWorkbook workbook = null;
FileInputStream excelFileInputStream = null;
try {
excelFileInputStream = new FileInputStream(dirPath + f.getName());
workbook = new HSSFWorkbook(excelFileInputStream);
HSSFSheet sheet1 = workbook.getSheetAt(0);
HashMap<String, Integer> keyMap = new HashMap<>();
getTitleIndex(sheet1, keyMap);
int lastRowNum = sheet1.getLastRowNum();
HashMap<String, String> nameMap=new HashMap();
HashMap<String, String> codeMap=new HashMap();
for (int i = 1; i <=lastRowNum; i++) { // i=1 : from 2th row exclude titile
HSSFRow row = sheet1.getRow(i);
String hosopt_name = getCellStrVal(row.getCell(keyMap.get("hosopt_name")));
if(!nameMap.containsKey(hosopt_name)){
nameMap.put(hosopt_name,"1");
}else {
continue;
}
String pinyin = HanyuPinyinHelper.getFirstLettersUp(hosopt_name);
if(!codeMap.containsKey(pinyin)){
codeMap.put(pinyin,pinyin);
}else {
codeMap.put(pinyin,pinyin+i);
}
pinyin=codeMap.get(pinyin);
sb.append("insert into oi_hos_custom_opt(`hosopt_name`, `spell_code`, `cust_code`) values('")
.append(hosopt_name).append("','").append(pinyin).append("','").append(pinyin).append("');").append("\r\n");
}
} catch (IOException e) {
logger.error("read excel error", e);
} finally {
if (excelFileInputStream != null) {
try {
excelFileInputStream.close();
} catch (IOException e) {
logger.error("excelFileInputStream close error");
}
}
}
}
}
}
System.out.println(sb.toString());
}
private void getTitleIndex(HSSFSheet sheet1, HashMap<String, Integer> keyMap) {
HSSFRow firstRow = sheet1.getRow(0);
short firstCellNum = firstRow.getFirstCellNum();
short lastCellNum = firstRow.getLastCellNum();
for (int i = firstCellNum; i <= lastCellNum; i++) {
String cellStrVal = getCellStrVal(firstRow.getCell(i));
if (cellStrVal.equals("手术名称")) {
keyMap.put("hosopt_name", i);
}
}
}
4、工具类
package cb.tool.analyse.util; import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class HanyuPinyinHelper { /**
* 将文字转为汉语拼音
* @param ChineseLanguage 要转成拼音的中文
*/
public static String toHanyuPinyin(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部小写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
try {
for (int i=0; i<cl_chars.length; i++){
if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
} else {// 如果字符不是中文,则不转换
hanyupinyin += cl_chars[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static String getFirstLettersUp(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);
} public static String getFirstLettersLo(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);
} public static String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(caseType);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
hanyupinyin += cl_chars[i];
} else {// 否则不转换
hanyupinyin += cl_chars[i];//如果是标点符号的话,带着
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static String getPinyinString(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(
cl_chars[i], defaultFormat)[0];
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i];
} else {// 否则不转换
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
}
/**
* 取第一个汉字的第一个字符
* @Title: getFirstLetter
* @Description: TODO
* @return String
* @throws
*/
public static String getFirstLetter(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
String str = String.valueOf(cl_chars[0]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(
cl_chars[0], defaultFormat)[0].substring(0, 1);;
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[0];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[0];
} else {// 否则不转换 }
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static void main(String[] args) {
System.out.println(HanyuPinyinHelper.toHanyuPinyin("中秋节"));
}
}
5、配置文件:
file:
xls: E:\myFiles\data\a\
从excel中转存sql的更多相关文章
- 在Excel中使用SQL语句查询和筛选
本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...
- Excel下用SQL语句实现AVEDEV函数功能
Excel下AVEDEV函数返回一组数据点到其算术平均值的绝对偏差的平均值. AVEDEV 是对一组数据中变化性的度量.最常见的应用就是统计平均分差. 但是如果在Excel中写SQL进行一些复杂的统计 ...
- Excel数据生成Sql语句的方法
选中想要生成的列,套用表格格式,选中表包含标题的选项确定,然后在最右边的一列第二行处,点击函数功能,选择CONCATENATE,在文本里输入想要的结构即可 代码如下 复制代码 ,=CONCATENA ...
- Excel 中使用sql语句查询
将Excel连接Oracle数据库 Excel选项板中"数据"—"自其他来源"下拉菜单中有有个可以连接其它数据库的选项"来自数据连接向导"和 ...
- 将Excel导出为SQL语句
需求说明:公司做项目前进行需求分析,确定表结构后需要建表,如果照着表格去敲,那就太麻烦了,所以想到了自动生成SQL语句. 思路大概就是:解析Excel,拼接SQL语句,输出SQL文件. 第三方jar包 ...
- Excel导入MS SQL SERVER 操作
关于Excel导入到sql操作的相关问题总结: 一.大批量数据导入 方法1.从Excel大批量数据导入时我们可以使用sql里面有一个batch copy的功能 方法2.在sql中建一个table ty ...
- 通过Excel生成批量SQL语句
项目中有时会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Insert,有的是Update等等,少量的数据我们可以采取最原始的办法,也就是在SQL里面用Insert int ...
- 在EXCEL中使用SQL语句查询
SQL语句在数据库使用中十分重要. 在EXCEL中可以不打开工作簿,就获取数据,对多工作簿操作很用,也很快. 对大量数据处理,比循环快很多,但是比词典方法还有点距离(可惜我还没有学会词典). 对数据库 ...
- [转]在Excel中使用SQL语句实现精确查询
本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...
随机推荐
- Window权限维持(一):注册表运行键
在红队行动中在网络中获得最初的立足点是一项耗时的任务.因此,持久性是红队成功运作的关键,这将使团队能够专注于目标,而不会失去与指挥和控制服务器的通信.在Windows登录期间创建将执行任意负载的注册表 ...
- 【Wannafly挑战赛29F】最后之作(Trie树,动态规划,斜率优化)
[Wannafly挑战赛29F]最后之作(Trie树,动态规划,斜率优化) 题面 牛客 题解 首先考虑怎么计算\([l,r]\)这个子串的不同的串的个数. 如果\(l=1\),我们构建\(Trie\) ...
- 2018-9-30-win10-UWP-剪贴板-Clipboard
原文:2018-9-30-win10-UWP-剪贴板-Clipboard title author date CreateTime categories win10 UWP 剪贴板 Clipboard ...
- 调用SqlCommand或SqlDataAdapter的Dispose方法,是否会关闭绑定的SqlConnection?(转载)
1. Does SqlCommand.Dispose close the connection? 问 Can I use this approach efficiently? using(SqlCom ...
- Swagger实例分享(VS+WebApi+Swashbuckle)
Swagger实例分享(VS+WebApi+Swashbuckle) Swagger可以很方便的为发布的WebApi自动生成优雅的文档,不需额外自己编写,只需为项目配置好,是一个很好用的工具,做一个简 ...
- 2019 物易云通java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.物易云通等公司offer,岗位是Java后端开发,因为发展原因最终选择去了物易云通,入职一年时间了,也成为了面 ...
- Promise的三兄弟:all(), race()以及allSettled()
摘要: 玩转Promise. 原文:Promise 中的三兄弟 .all(), .race(), .allSettled() 译者:前端小智 Fundebug经授权转载,版权归原作者所有. 从ES6 ...
- python基础-内置函数 isinstance() 与 issubclass()
面向对象编程之内置函数 isinstance() 与 issubclass() isinstance():判断一个对象是否是另一个类的实例.返回bool值,True表示是,False表示不是. 语法: ...
- sql server 安装出现需要sqlncli.msi文件,错误为 microsoft sql server 2012 native client
在安装sql server 2017 时出现 弹框标题为 microsoft sql server 2012 native client 内容为需要sqlncli.msi文件 去本地目录找本身的那 ...
- [Linux] nginx的try_files指令实现隐藏index.php的重写
1.nginx的try_files指令 ,核心功能是替代rewrite,并且比rewrite更强大的是可以按顺序查找文件是否存在,如果文件都找不到才会执行最后的重定向解决的问题是,如果一个网站的部署是 ...