364 lines
9.4 KiB
Plaintext
364 lines
9.4 KiB
Plaintext
package com.bonus.utils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Enumeration;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import com.oreilly.servlet.MultipartRequest;
|
|
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
|
|
|
|
@Controller
|
|
public class ExcelFiledInsertDatabase{
|
|
|
|
@Autowired
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
private String propertyPath;
|
|
|
|
public String getPropertyPath() {
|
|
return propertyPath;
|
|
}
|
|
|
|
public void setPropertyPath(String propertyPath) {
|
|
this.propertyPath = propertyPath;
|
|
}
|
|
|
|
@RequestMapping("/insertData")
|
|
public String mainPage(Model model) {
|
|
|
|
model.addAttribute("tables",getTableNameList());
|
|
|
|
return "/sys/excelInsertDatabase";
|
|
}
|
|
|
|
@RequestMapping(value = "/getColumnListByTitleNumChange")
|
|
@ResponseBody
|
|
public List<String> getColumnListByTitleNumChange(HttpServletRequest request,Integer titleNum,String filepath) {
|
|
List<String> list = new ArrayList<String>();
|
|
try {
|
|
String filePath = request.getSession().getServletContext().getRealPath("/filetemp");
|
|
filepath =filePath +"/"+ filepath;
|
|
list = getExeclColumnList(filepath,titleNum-1);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@RequestMapping(value = "/submitDataToInsert")
|
|
@ResponseBody
|
|
public String submitDataToInsert(HttpServletRequest request) {
|
|
String res = "0";
|
|
try {
|
|
res = insterData(request);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return res;
|
|
}
|
|
|
|
@RequestMapping(value = "/uploadexcel")
|
|
@ResponseBody
|
|
public List<String> uploadexcel(HttpServletRequest request,String excelPath) {
|
|
List<String> list = new ArrayList<String>();
|
|
try {
|
|
list = uploadexcel(request);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@RequestMapping(value = "/getFiledListByTableName")
|
|
@ResponseBody
|
|
public List<Map<String, Object>> getFiledListByTableName1(String tableName) {
|
|
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
|
try {
|
|
list = getFiledListByTableName(tableName);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return list;
|
|
}
|
|
|
|
|
|
|
|
public String getDataBaseName(String propertyPath){
|
|
Properties prop=new Properties();
|
|
InputStream ips=ExcelFiledInsertDatabase.class.getClassLoader()
|
|
.getResourceAsStream(propertyPath);
|
|
String[] params = new String[2];
|
|
try {
|
|
|
|
prop.load(ips);
|
|
|
|
String property = prop.getProperty("jdbc.mysql.url");
|
|
|
|
params = property.split("\\?");
|
|
|
|
System.out.println(params);
|
|
|
|
} catch (Exception e){
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return params[0].substring(params[0].lastIndexOf("/")+1);
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getFiledListByTableName(String tableName) {
|
|
|
|
String sql = "show full columns from "+tableName;
|
|
|
|
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
|
|
|
|
return list;
|
|
}
|
|
|
|
public List<String> getTableNameList() {
|
|
|
|
String dataBaseName = getDataBaseName(propertyPath);
|
|
|
|
String sql = "select table_name from information_schema.tables where table_schema='"+dataBaseName+"'";
|
|
|
|
|
|
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
|
|
|
|
List<String> l = new ArrayList<String>();
|
|
|
|
for(int i=0;i<list.size();i++){
|
|
l.add(list.get(i).get("table_name").toString());
|
|
}
|
|
|
|
return l;
|
|
}
|
|
|
|
|
|
public List<String> uploadexcel(HttpServletRequest request) {
|
|
|
|
Integer titleNum = Integer.parseInt(request.getParameter("titleNum").toString());
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
String fileName = "";
|
|
|
|
String filePath = request.getSession().getServletContext().getRealPath("/filetemp");
|
|
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
// 打印上传路径信息
|
|
int maxPostSize = 200 * 1024 * 1024;
|
|
MultipartRequest multi = null;
|
|
DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();
|
|
try {
|
|
multi = new MultipartRequest(request, filePath, maxPostSize, "UTF-8", dfp);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
// 输出反馈信息
|
|
Enumeration<?> files = multi.getFileNames();
|
|
while (files.hasMoreElements()) {
|
|
String name = (String) files.nextElement();
|
|
File f = multi.getFile(name);
|
|
if (f != null) {
|
|
long newName=System.currentTimeMillis();
|
|
fileName = multi.getFilesystemName(name);
|
|
int begin = fileName.indexOf(".");
|
|
int last = fileName.length();
|
|
String suffix = fileName.substring(begin,last);
|
|
File oldfile = new File(filePath + "/" + fileName);
|
|
File newfile = new File(filePath + "/" + newName + suffix);
|
|
oldfile.renameTo(newfile);
|
|
filePath = filePath + "/" + newName + suffix;
|
|
|
|
list = getExeclColumnList(filePath,titleNum-1);
|
|
|
|
list.add(newName + suffix);
|
|
//tempFile.delete();
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public List<String> getExeclColumnList(String filePath,Integer titleIndex) {
|
|
List<String> info = new ArrayList<String>();
|
|
InputStream is;
|
|
try {
|
|
is = new FileInputStream(filePath);
|
|
Workbook wk = WorkbookFactory.create(is);
|
|
|
|
Sheet st = wk.getSheetAt(0);
|
|
|
|
if(st.getRow(titleIndex) == null){
|
|
return info;
|
|
}
|
|
int coloumNum=st.getRow(titleIndex).getPhysicalNumberOfCells();
|
|
|
|
Row row = null;
|
|
Cell cell = null;
|
|
row = st.getRow(titleIndex);
|
|
if(row != null) {
|
|
for(int j = 0;j<coloumNum;j++) {
|
|
cell = row.getCell(j);
|
|
if(cell != null) {
|
|
info.add(cell.toString());
|
|
}else {
|
|
info.add(null);
|
|
}
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return info;
|
|
}
|
|
|
|
public String insterData(HttpServletRequest request) {
|
|
|
|
String result = "0";
|
|
|
|
String filePath = request.getSession().getServletContext().getRealPath("/filetemp");
|
|
|
|
String tableName = request.getParameter("tableName");
|
|
String filepath = request.getParameter("filepath");
|
|
|
|
String fieldStr = request.getParameter("fieldStr");
|
|
String columnStr = request.getParameter("columnStr");
|
|
Integer start = Integer.parseInt(request.getParameter("start"));
|
|
String parameter = request.getParameter("end");
|
|
Integer end = null;
|
|
|
|
filepath=filePath+"/"+filepath;
|
|
|
|
if(!("".equals(parameter) || parameter ==null || "null".equals(parameter) || "undefined".equals(parameter)) ){
|
|
end = Integer.parseInt(parameter);
|
|
}
|
|
|
|
List<List<String>> columnStrs = extractExcelFileInfo(filepath,columnStr,start,end);
|
|
//字段值
|
|
String vals = "";
|
|
String temp = "";
|
|
columnStrs.size();
|
|
|
|
for(List<String> columnDatas:columnStrs){
|
|
temp = "(";
|
|
for(int i = 0; i < columnDatas.size(); i++){
|
|
|
|
if(i == 0){
|
|
if(columnDatas.get(i) == null || "".equals(columnDatas.get(i)) ){
|
|
temp += "null";
|
|
}else{
|
|
temp += "'"+columnDatas.get(i)+"'";
|
|
}
|
|
}else{
|
|
if(columnDatas.get(i) == null || "".equals(columnDatas.get(i)) ){
|
|
temp += ",null";
|
|
}else{
|
|
temp += ",'"+columnDatas.get(i)+"'";
|
|
}
|
|
}
|
|
}
|
|
|
|
temp += ")";
|
|
|
|
if("".equals(vals)){
|
|
vals = temp;
|
|
}else{
|
|
vals += "," + temp;
|
|
}
|
|
}
|
|
|
|
String sql = " insert into "+ tableName +"("+fieldStr+") values"+vals;
|
|
|
|
System.out.println(sql);
|
|
try {
|
|
jdbcTemplate.execute(sql);
|
|
result = "1,"+columnStrs.size();;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
result = "2";
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static List<List<String>> extractExcelFileInfo(String filePath,String columnStr,Integer start,Integer end) {
|
|
|
|
List<List<String>> info =new ArrayList<List<String>>();
|
|
|
|
List<String> list = null;
|
|
|
|
InputStream is;
|
|
|
|
try {
|
|
String[] indexArr = columnStr.split(",");
|
|
|
|
is = new FileInputStream(filePath);
|
|
|
|
Workbook wk = WorkbookFactory.create(is);
|
|
|
|
Sheet st = wk.getSheetAt(0);
|
|
|
|
int rows = st.getPhysicalNumberOfRows();//行数
|
|
|
|
Row row = null;
|
|
|
|
Cell cell = null;
|
|
|
|
Integer len = indexArr.length;
|
|
|
|
for(int k = start;k < rows; k++){
|
|
row = st.getRow(k);
|
|
if(row != null){
|
|
list = new ArrayList<String>();
|
|
for(int i = 0; i<len; i++) {
|
|
|
|
cell = row.getCell(Integer.parseInt(indexArr[i]));
|
|
|
|
if(cell != null) {
|
|
list.add(cell.toString());
|
|
}else {
|
|
list.add(null);
|
|
}
|
|
}
|
|
info.add(list);
|
|
}else{
|
|
break;
|
|
}
|
|
if(end != null && end == k){
|
|
break;
|
|
}
|
|
}
|
|
} catch (FileNotFoundException fe) {
|
|
fe.printStackTrace();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return info;
|
|
}
|
|
|
|
} |