实现秒级execl大批量导入数据到mysql中

来源:互联网 发布:货车租赁软件 编辑:程序博客网 时间:2024/05/18 13:04
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
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 com.mysql.jdbc.Statement;


/**
 * 
* @ProjectName eims
* @ClassName ImportDataByExcel 
* @Description TODO(从excel批量导入数据到数据库中) 
* @author makang
* @date 2016-7-28 下午3:15:14 
* @version V1.0
 */
public class ImportDataByExcel {


/**

* @Title createCsvFile 
* @Description TODO(生成csv文件) 
* @param excelPath  用户上传excel文件路径
* @param csvPath    生成csv文件存放路径
* @param cellCount  每行多少个cell
* @param titles     excel表头名称
* @return
*/
public static String createCsvFile(String excelPath,String csvPath,int cellCount,List<String> titles){
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");//创建CSVFormat ,每行记录间隔符使用换行【\n】
CSVPrinter csvFilePrinter = null;
BufferedWriter fileWriter = null;
try {
fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(csvPath)),"gbk"));
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
csvFilePrinter.printRecord(titles);

File excelFile = new File(excelPath);
FileInputStream fis = new FileInputStream(excelFile);
Workbook workBook = WorkbookFactory.create(fis); 
Sheet sheet = workBook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
for(int i=1;i<rows;i++){
List<String> record = new ArrayList<String>();
Row row = sheet.getRow(i);  
if(null!=row){
for(int j=0;j<cellCount;j++){
Cell cell = row.getCell(j); 
if(cell!=null){  
cell.setCellType(Cell.CELL_TYPE_STRING);  
record.add(cell.getStringCellValue().contains(",")?cell.getStringCellValue().replaceAll(",", "\\\\,"):cell.getStringCellValue());
}else{  
record.add("");

}
}
csvFilePrinter.printRecord(record);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
            try {
                fileWriter.flush();
                fileWriter.close();
                csvFilePrinter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
return csvPath;
}

/**

* @Title jdbcInsert 
* @Description TODO(将csv文件通过jdbc的方式插入数据库) 
* @param csvFilePath  生成csv文件路径
* @param tableName    导入的表名
* @param fieldsName   导入字段名
*/
public static void jdbcInsert(String csvFilePath,String tableName,String fieldsName){
Connection conn = null;  
PreparedStatement pstmt = null;  
try {
   conn = JDBCUtil.getConnection();  
   String sql = "load data local infile '"+csvFilePath+"'"  
           + " into table "+tableName+"  character set gbk" 
           + " fields terminated by ',' optionally enclosed by '\"' ignore 1 lines ("+fieldsName+")";  
   pstmt = conn.prepareStatement(sql);  
   if (pstmt.isWrapperFor(Statement.class)) {  
       PreparedStatement mysqlStatement = pstmt.unwrap(PreparedStatement.class);  
       mysqlStatement.executeUpdate();  
   }  
} catch (SQLException e) {  
   e.printStackTrace();  
} finally {  
   try {  
       if(conn != null){  
           conn.close();  
       }  
       if(pstmt != null){  
           pstmt.close();  
       }  
   } catch (Exception e2) {  
       e2.printStackTrace();  
   }  
}  
}

public static void main(String[] args) {
long starTime=System.currentTimeMillis();
List<String> titles = new ArrayList<String>();
titles.add("专家姓名");
titles.add("专家性别");
titles.add("专家类型");
titles.add("专家类型(其他)");
titles.add("主学科方向A");
titles.add("主学科方向B");
titles.add("主学科方向C");
titles.add("学科关键词");
titles.add("学科分组");
titles.add("工作单位");
titles.add("办公电话");
titles.add("手机号码");
titles.add("电子邮件");
titles.add("简介");
titles.add("大前年年参评的次数");
titles.add("前年年参评的次数");
titles.add("去年参评的次数");
String csvPath = createCsvFile("d:/import/11111.xls","d:/import/expert.csv",17,titles);
long  endTime=System.currentTimeMillis(); 
long  Time=endTime-starTime; 
System.out.println("***********createCsvFile执行时间****"+Time+"***************");


starTime=System.currentTimeMillis();
jdbcInsert(csvPath,"expert","name,sex,type,otherType,directionA,directionB,directionC,subjectKeywords,grop,workplace,telephone,mobile,email,introduction,yearbl,yearlast,thisyear");
endTime=System.currentTimeMillis(); 
   Time=endTime-starTime; 
System.out.println("***********jdbcInsert执行时间****"+Time+"***************");


//jdbcInsert("d:/import/33333.csv","test01","name,sex,type");
}
0 0
原创粉丝点击