JAVA實(shí)現(xiàn)excel轉(zhuǎn)mysql建表sql

ReadExcel4SQL.java

package rtool;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.io.FileUtils;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel4SQL {

//資源絕對路徑

? ? public static StringxlsPath="F:\\文檔\\實(shí)體類\\黑名單.xls";

? ? //資源的目錄

? ? public static StringdirPath="F:\\文檔\\實(shí)體類\\";

? ? //輸出路徑

? ? public static StringjavaPath="";

? ? //字段名列

? ? public static int attributesIndex=0;

? ? //類型列

? ? public static int typesIndex=2;

? ? //注釋列

? ? public static int[]marksIndex={1,4,3};

? ? //是否為空注釋

? ? public static int marksEmptyIndex=3;

? ? public static void main(String[] args)throws IOException, InvalidFormatException {

// TODO Auto-generated method stub

? ? ? ? File file =new File(dirPath);

? ? ? ? ArrayList files =getListFiles(file);

? ? ? ? for(File f:files){

createSQL( f);

? ? ? ? }

}

public static void createSQL(File file)throws IOException, InvalidFormatException {

String fileName = file.getName();

? ? ? ? String tableName ="demo";

? ? ? ? File file1 =null;

? ? ? ? FileOutputStream fop =null;

? ? ? ? List types =new ArrayList();

? ? ? ? List attributes =new ArrayList();

? ? ? ? List marks =new ArrayList();

? ? ? ? List marksEmpty =new ArrayList();

? ? ? ? if (fileName.endsWith("xlsx")){

//?????????????????????????????????

? ? ? ? ? ? XSSFWorkbook workbooks =new XSSFWorkbook(FileUtils.openInputStream(file));

? ? ? ? ? ? XSSFSheet xssfSheet = workbooks.getSheetAt(0);

? ? ? ? ? ? int totalRows = xssfSheet.getPhysicalNumberOfRows();

? ? ? ? ? ? XSSFRow row = xssfSheet.getRow(0);

? ? ? ? ? ? tableName = row.getCell(0).getStringCellValue();

? ? ? ? ? ? System.out.println(tableName+"total:"+totalRows);

? ? ? ? ? ? for(int i=1;i

XSSFRow row2 = xssfSheet.getRow(i);

? ? ? ? ? ? ? ? attributes.add(row2.getCell(attributesIndex).getStringCellValue());

? ? ? ? ? ? ? ? types.add(row2.getCell(typesIndex).getStringCellValue());

? ? ? ? ? ? ? ? String marksDtr="";

? ? ? ? ? ? ? ? for(int j=marksIndex.length-1;j>=0;j--){

marksDtr+= row2.getCell(marksIndex[j]).getStringCellValue();

? ? ? ? ? ? ? ? }

marks.add(marksDtr);

? ? ? ? ? ? }

}else if(fileName.endsWith("xls")) {

HSSFWorkbook workbook =new HSSFWorkbook(FileUtils.openInputStream(file));

? ? ? ? ? ? //讀取默認(rèn)第一個(gè)工作表sheet

? ? ? ? ? ? HSSFSheet sheet = workbook.getSheetAt(0);

? ? ? ? ? ? int firstRowNum =0;

? ? ? ? ? ? //獲取sheet中最后一行行號

? ? ? ? ? ? int lastRowNum = sheet.getLastRowNum();

? ? ? ? ? ? HSSFRow row = sheet.getRow(firstRowNum);

? ? ? ? ? ? tableName = row.getCell(0).getStringCellValue();

? ? ? ? ? ? for (int i =1; i <=lastRowNum-1; i++){

HSSFRow row1 = sheet.getRow(i);

? ? ? ? ? ? ? ? attributes.add(row1.getCell(attributesIndex).getStringCellValue());

? ? ? ? ? ? ? ? types.add(row1.getCell(typesIndex).getStringCellValue());

? ? ? ? ? ? ? ? String marksDtr="";

? ? ? ? ? ? ? ? for(int j=marksIndex.length-1,k=0;j>=0;j--,k++){

marksDtr+= row1.getCell(marksIndex[k]).getStringCellValue().equals("")?"":row1.getCell(marksIndex[k]).getStringCellValue()+(j!=0?"--":"");

? ? ? ? ? ? ? ? }

marks.add(marksDtr);

? ? ? ? ? ? ? ? marksEmpty.add(row1.getCell(marksEmptyIndex).getStringCellValue());

? ? ? ? ? ? }

}else

? ? ? ? ? ? {

return;

? ? ? ? ? ? }

StringBuffer sb =new StringBuffer();

? ? ? ? try {

//? ? ? ? ? ? attributes= createUtil.attributesFilter(attributes);

//? ? ? ? ? ? types= createUtil.typesFilter(types);

? ? ? ? ? ? marksEmpty=createSQL.marksEmptyFilter(marksEmpty);

? ? ? ? ? ? types= createSQL.typesFilter(types);

? ? ? ? ? ? attributes = createSQL.attributesFilter(attributes);

? ? ? ? ? ? sb.append("CREATE TABLE " + tableName +"(\n\nID int not null auto_increment primary key,");

? ? ? ? ? ? sb.append(createSQL.appendInit(types, attributes,marksEmpty, marks));

? ? ? ? ? ? //sb.append(createUtil.getSet(types, attributes, marks));

? ? ? ? ? ? sb.append(");\n");

? ? ? ? }catch (Exception e)

{

System.out.println(e.toString());

? ? ? ? }

file1 =new File(file.getParent()+"http://createTable.sql");

? ? ? ? if (!file1.exists()) {

file1.createNewFile();

? ? ? ? ? ? fop =new FileOutputStream(file1);

? ? ? ? }else {

fop =new FileOutputStream(file1,true);

? ? ? ? }

byte[] contentInBytes = sb.toString().getBytes();

? ? ? ? fop.write(contentInBytes);

? ? ? ? fop.flush();

? ? ? ? fop.close();

? ? ? ? System.out.println("Done");

? ? }

public static ArrayListgetListFiles(Object obj) {

File directory =null;

? ? ? ? if (objinstanceof File) {

directory = (File) obj;

? ? ? ? }else {

directory =new File(obj.toString());

? ? ? ? }

ArrayList files =new ArrayList();

? ? ? ? if (directory.isFile()) {

files.add(directory);

? ? ? ? ? ? return files;

? ? ? ? }else if (directory.isDirectory()) {

File[] fileArr = directory.listFiles();

? ? ? ? ? ? for (int i =0; i < fileArr.length; i++) {

File fileOne = fileArr[i];

? ? ? ? ? ? ? ? files.addAll(getListFiles(fileOne));

? ? ? ? ? ? }

}

return files;

? ? }

}

createSQL.java

package rtool;

import java.util.List;

public class createSQL {

public static StringappendInit(List types,List attributes,List marksEmpty,List mark){

StringBuffer sb =new StringBuffer();

? ? ? ? if(types.size()==attributes.size()&&types.size()==mark.size()){

for(int i=0;i

sb.append("? ? /**\n");

? ? ? ? ? ? ? ? sb.append(" *"+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" */\n");

? ? ? ? ? ? ? ? sb.append(attributes.get(i)+"? "+types.get(i)+"? "+marksEmpty.get(i)+" COMMENT '"+mark.get(i) +"',\n\n");

? ? ? ? ? ? }

}

return (sb.deleteCharAt(sb.lastIndexOf(","))).toString();

? ? }

public static StringgetSet(List types,List attributes,List mark){

StringBuffer sb =new StringBuffer();

? ? ? ? if(types.size()==attributes.size()){

for(int i=0;i

String attribute = attributes.get(i);

? ? ? ? ? ? ? ? String c = String.valueOf(attribute.charAt(0));

? ? ? ? ? ? ? ? c = c.toUpperCase();

? ? ? ? ? ? ? ? attribute = c + attribute.substring(1);

? ? ? ? ? ? ? ? sb.append("? ? /**\n");

? ? ? ? ? ? ? ? sb.append(" * 設(shè)置"+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" * @param "+attributes.get(i)+" "+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" */\n");

? ? ? ? ? ? ? ? sb.append(" public void set"+attribute+"("+types.get(i)+" "+attributes.get(i)+"){\n");

? ? ? ? ? ? ? ? sb.append("? ? this."+attributes.get(i)+" = "+attributes.get(i)+";\n");

? ? ? ? ? ? ? ? sb.append(" }\n\n");

? ? ? ? ? ? ? ? sb.append("? ? /**\n");

? ? ? ? ? ? ? ? sb.append(" *獲得"+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" * @return "+attributes.get(i)+" "+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" */\n");

? ? ? ? ? ? ? ? sb.append(" public "+types.get(i)+" get"+attribute+"(){\n");

? ? ? ? ? ? ? ? sb.append("? ? return "+attributes.get(i)+";\n");

? ? ? ? ? ? ? ? sb.append(" }\n\n");

? ? ? ? ? ? }

}

return sb.toString();

? ? }

//? ? .toLowerCase();//轉(zhuǎn)成小寫

//.toUpperCase();//轉(zhuǎn)成大寫

//? ? public static List? attributesFilter(List attributes)

//? ? {

//? ? ? ? for(int i=0;i< attributes.size();i++)

//? ? ? ? {

//? ? ? ? ? ? String str[]=attributes.get(i).toLowerCase().split("_");

//? ? ? ? ? ? String Str="";

//? ? ? ? ? ? Str +=str[0];

//? ? ? ? ? ? for(int j=1;j< str.length;j++)

//? ? ? ? ? ? {

//? ? ? ? ? ? ? ? Str+=str[j].substring(0,1).toUpperCase();

//? ? ? ? ? ? ? ? Str+=str[j].substring(1);

//? ? ? ? ? ? }

//? ? ? ? ? ? attributes.set(i,Str);

//

//? ? ? ? }

//? ? ? ? return attributes;

//? ? }

? ? public static ListtypesFilter(List types)

{

for(int i=0;i< types.size();i++)

{

if( types.get(i).toLowerCase().contains("int"))

{

types.set(i,"Integer");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("double")){

types.set(i,"Double");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("string")){

types.set(i,"VARCHAR(20)");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("float")){

types.set(i,"Float");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("bool")){

types.set(i,"char");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("date")){

types.set(i,"DATETIME");

? ? ? ? ? ? }else if(types.get(i).toLowerCase().contains("char")){

types.set(i,"char");

? ? ? ? ? ? }

}

return types;

? ? }

public static ListattributesFilter(List attributes)

{

for(int i=0;i< attributes.size();i++)

{

if( attributes.get(i).toUpperCase().contains("DESCRIBE"))

{

attributes.set(i,attributes.get(i)+"_");

? ? ? ? ? ? }

}

return attributes;

? ? }

public static ListmarksEmptyFilter(List m)

{

for(int i=0;i< m.size();i++)

{

if( m.get(i).toLowerCase().contains("y")||m.get(i).toLowerCase().contains("是"))

{

m.set(i,"not null");

? ? ? ? ? ? }else

? ? ? ? ? ? {

m.set(i,"");

? ? ? ? ? ? }

}

return m;

? ? }

public static StringappendCreateTable(List types,List attributes,List mark){

StringBuffer sb =new StringBuffer();

? ? ? ? if(types.size()==attributes.size()&&types.size()==mark.size()){

for(int i=0;i

sb.append("? ? /**\n");

? ? ? ? ? ? ? ? sb.append(" *"+mark.get(i)+"\n");

? ? ? ? ? ? ? ? sb.append(" */\n");

? ? ? ? ? ? ? ? sb.append(" private "+types.get(i)+" "+attributes.get(i)+";\n\n");

? ? ? ? ? ? }

}

return sb.toString();

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容