java 調用jdbc 實現excel和csv的導入和導出,jdbccsv
package com.gpdi.mdata.web.manage.database.data;
public class DBQueryData {
private String url;
private String dbName;
private String user;
private String passwd;
private String tableName;
private String where;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getWhere() {
return where;
}
public void setWhere(String where) {
this.where = where;
}
@Override
public String toString() {
return "DBQueryData{" +
"url='" + url + '\'' +
", dbName='" + dbName + '\'' +
", user='" + user + '\'' +
", passwd='" + passwd + '\'' +
", tablename='" + tableName + '\'' +
", where='" + where + '\'' +
'}';
}
}
package com.gpdi.mdata.web.manage.database.utils;
import com.gpdi.mdata.web.manage.database.data.DBQueryData;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionManager {
private static Connection conn=null;
public ConnectionManager(){};
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(DBQueryData dbQueryData){
try {
if(conn==null||conn.isClosed()) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://"+dbQueryData.getUrl()+"/"+dbQueryData.getDbName();
String user = dbQueryData.getUser();
String password = dbQueryData.getPasswd();
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
System.out.println("數據庫鏈接成功");
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("數據庫鏈接失敗");
}
return conn;
}
public static void main(String[] args) {
}
}
- 進行數據庫查詢,得到結果以對象方式存諸以便方便調用
package com.gpdi.mdata.web.manage.database.utils;
import com.gpdi.mdata.web.manage.database.data.DBList;
import com.gpdi.mdata.web.manage.database.data.DBQueryData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.sql.*;
import java.util.*;
//jdbc鏈接數據庫,獲取表名,字段名和數據
public class DBhepler {
private Connection conn=null;
private PreparedStatement ps=null;
public DBList search(DBQueryData dbQueryData) throws Exception {
DBList dbList=new DBList();
String sql="";
conn = ConnectionManager.getConnection(dbQueryData);
if(dbQueryData.getWhere()==null||dbQueryData.getWhere().equals("")){
sql="select * from "+dbQueryData.getTableName();
}
else {
sql="select * from "+dbQueryData.getTableName()+" where="+dbQueryData.getWhere();
}
// 獲取所有表名
//Statement statement = conn.createStatement();
PreparedStatement pst =conn.prepareStatement(sql);
ResultSet resultSet=pst.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
//定義一個list去接受字段名
List<String> list=new ArrayList<>();
for (int i = 0; i < metaData.getColumnCount(); i++) {
// resultSet數據下標從1開始
String columnName = metaData.getColumnName(i + 1);
int type = metaData.getColumnType(i + 1);
if (Types.INTEGER == type) {
// int
} else if (Types.VARCHAR == type) {
// String
}
list.add(columnName);
System.out.print(columnName + "\t");
}
dbList.setRowTitle(list);
List<List<String>> valueList=new ArrayList<>();
int j=0;
System.out.println();
// 獲取數據
while (resultSet.next()) {
List<String> colList=new ArrayList<>();
for (int i = 0; i < metaData.getColumnCount(); i++) {
// resultSet數據下標從1開始
//System.out.print(resultSet.getString(i + 1) + "\t");
colList.add(resultSet.getString(i + 1));
}
valueList.add(colList);
j++;
}
dbList.setColCount(metaData.getColumnCount());
dbList.setRowCount(j);
dbList.setValueList(valueList);
pst.close();
conn.close();
//pst.close();
// conn.close();
return dbList;
}
@RequestMapping
public Map<String, List<String>> SQLInsert(String [] zdName,List<List<Object>> list,DBQueryData dbQueryData){
Map<String, List<String>> resultMap=new HashMap<>();
List<String>countList=new ArrayList<>();
List<String> suList=new ArrayList();
List<String> fiList=new ArrayList();
//sql
int filed=0;
int success=0;
for(int li=0;li<list.size()-1;li++){
//sql語句前部分
StringBuffer sql=new StringBuffer();
sql.append("insert into file_1 (");
for (int zd=0;zd<zdName.length-1;zd++){
//zdName[zd]=zdList.get(zd).toString();
sql.append(zdName[zd]+",");
}
sql.append(zdName[zdName.length-1]+") ");
//sql後部分
List valueList=list.get(li);
StringBuffer sqlEnd=new StringBuffer();
sqlEnd.append(" values(");
for (int v=0;v<valueList.size()-1;v++){
sqlEnd.append("'"+valueList.get(v)+"'"+",");
}
sqlEnd.append("'"+valueList.get(valueList.size()-1)+"'"+");");
sql.append(sqlEnd);
System.err.println("sql:"+sql);
conn= ConnectionManager.getConnection(dbQueryData);
try {
ps=conn.prepareStatement(sql.toString());
//ps.addBatch(insertSql);
ps.executeUpdate();
success++;
suList.add(sql.toString());
System.err.println("導入成功"+success);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
filed++;
System.err.println("導入失敗"+filed);
System.err.println("失敗sql:"+sql);
fiList.add(sql.toString());
//return null;
}finally {
System.err.println("導入完成");
}
}
try {
conn.close();
System.err.println("關閉數據庫成功");
} catch (SQLException e) {
System.err.println("關閉數據庫失敗");
}
countList.add(String.valueOf(success));
countList.add(String.valueOf(filed));
resultMap.put("count",countList);
resultMap.put("success",suList);
resultMap.put("filed",fiList);
return resultMap;
}
}
@RequestMapping
public static void exportToExcel(HttpServletRequest request,
HttpServletResponse response,DBQueryData dbQueryData) {
try {
System.err.println(dbQueryData);
String name =dbQueryData.getTableName();
DBhepler dBhepler=new DBhepler();
DBList dbList=dBhepler.search(dbQueryData);
Workbook workbook = createWorkbook(dbList, name);
String attachmentName = name + ".xls";
String ua = request.getHeader("User-Agent");
boolean isChrome = (ua != null && ua.contains("Chrome"));
try {
if (isChrome) {
attachmentName = java.net.URLEncoder.encode(attachmentName, "UTF-8");
}
else {
attachmentName = new String(attachmentName.getBytes("GB2312"), "iso8859-1");
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-disposition", "attachment; filename=" + attachmentName);
response.setContentType("application/msexcel");
ServletOutputStream os = response.getOutputStream();
workbook.write(os);
os.close();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Workbook createWorkbook(DBList dbList, String name) throws Exception {
String sheetName = name;
Workbook workbook = new HSSFWorkbook();
Sheet sheet = null;
int a = dbList.getRowCount();
if (a > 65535) {
int length = a / 65535;
for (int i = 0; i <= length; i++) {
if (sheet != null) {
sheet = null;
}
sheet = workbook.createSheet(sheetName + i);
int colCount = dbList.getColCount();
Row titleRow = sheet.createRow(0);
List<String> RowList=dbList.getRowTitle();
for(int j=0;j<RowList.size();j++){
Cell cell = titleRow.createCell(j);
cell.setCellValue(RowList.get(j));
}
if ((i + 1) * 65535 < a) {
//for (int r = i * 65535; r < (i + 1) * 65535; r++) {
List<List<String>> valueList= dbList.getValueList();
for(int v=0;v<valueList.size();v++){
Row row = sheet.createRow(v+ 1);
List<String> lists=valueList.get(v);
for (int l=0;l<lists.size();l++){
Cell cell=row.createCell(l);
cell.setCellValue(lists.get(l));
}
}
//}
}
else {
//for (int r = i * 65535; r < a; r++) {
List<List<String>> valueList= dbList.getValueList();
for(int v=0;v<valueList.size();v++){
Row row = sheet.createRow(v+ 1);
List<String> lists=valueList.get(v);
for (int l=0;l<lists.size();l++){
Cell cell=row.createCell(l);
cell.setCellValue(lists.get(l));
}
}
// }
}
}
}
else {
sheet = workbook.createSheet(name);
Row titleRow = sheet.createRow(0);
List<String> RowList=dbList.getRowTitle();
for(int j=0;j<RowList.size();j++){
Cell cell = titleRow.createCell(j);
cell.setCellValue(RowList.get(j));
}
List<List<String>> valueList= dbList.getValueList();
for(int v=0;v<valueList.size();v++) {
Row row = sheet.createRow(v + 1);
List<String> lists = valueList.get(v);
for (int l = 0; l < lists.size(); l++) {
Cell cell = row.createCell(l);
cell.setCellValue(lists.get(l));
}
}
}
return workbook;
}
package com.gpdi.mdata.web.manage.database.utils;
import com.gpdi.mdata.web.manage.database.data.DBQueryData;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ReadExcel {
public static void readExcel(InputStream file,String fileName,DBQueryData dbQueryData)throws IOException {
String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
.substring(fileName.lastIndexOf(".") + 1);
if ("xls".equals(extension)) {
read2003Excel(file,dbQueryData);
} else if ("xlsx".equals(extension)) {
read2007Excel(file);
} else {
//throw new IOException("不支持的文件類型");
read2003Excel(file,dbQueryData);
}
}
public static Map<String, List<String>> read2003Excel(InputStream file,DBQueryData dbQueryData)
{
DBhepler dBhepler=new DBhepler();
Map<String, List<String>> resultMap =new HashMap<>();
List<List<Object>> list = new ArrayList<>();
long Start;
try (HSSFWorkbook hwb = new HSSFWorkbook(file)) {
System.err.println("sheet:" + hwb.getNumberOfSheets());
Start = System.currentTimeMillis();
HSSFSheet sheet = hwb.getSheetAt(0);
Object value = null;
HSSFRow row = null;
HSSFCell cell = null;
int counter = 0;
System.err.println("s"+sheet.getLastRowNum());
//首行的內容,相當字段名
Object firstValue=getFirstRow(sheet);
String []ss=firstValue.toString().split(",");
for (int l=0;l<ss.length;l++){
System.err.println("ss:"+ss[l]);
}
//每行的每個單元格內容
for (int i = 1; i <=sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new ArrayList<>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
Long ns=System.currentTimeMillis();
if (cell == null) {
continue;
}
Long ne=System.currentTimeMillis();
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
Long Ss=System.currentTimeMillis();
value = cell.getStringCellValue();
Long Se=System.currentTimeMillis();
//System.out.println( "Strting開始:"+Ss+"結束:"+Se);
break;
case XSSFCell.CELL_TYPE_NUMERIC:
Long nns=System.currentTimeMillis();
System.out.println(i + "行" + j
+ " 列 is Number type ; DateFormt:"
+ cell.getCellStyle().getDataFormatString());
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
Long nne=System.currentTimeMillis();
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
System.err.println(i);
}
long end=System.currentTimeMillis();
System.err.println("總的開始:" + Start + "結束:" + end);
resultMap= dBhepler.SQLInsert(ss, list, dbQueryData);}
catch (Exception e){
}
return resultMap;
}
private static List<List<Object>> read2007Excel(InputStream file)
throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
// 構造 XSSFWorkbook 對象,strPath 傳入文件路徑
XSSFWorkbook xwb = new XSSFWorkbook(file);
// 讀取第一章表格內容
XSSFSheet sheet = xwb.getSheetAt(0);
Object value = null;
XSSFRow row = null;
XSSFCell cell = null;
int counter = 0;
for (int i = sheet.getFirstRowNum(); counter < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
//System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
System.out.println(i + "行" + j
+ " 列 is Number type ; DateFormt:"
+ cell.getCellStyle().getDataFormatString());
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
public static void main(String[] args) {
}
private static Object getFirstRow(HSSFSheet sheet){
Object value = "";
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.getRow(0);
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
//System.out.println(0 + "行" + j + " 列 is String type");
value = value+cell.getStringCellValue()+",";
break;
case XSSFCell.CELL_TYPE_NUMERIC:
System.out.println(0+ "行" + j
+ " 列 is Number type ; DateFormt:"
+ cell.getCellStyle().getDataFormatString());
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue())+",";
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = value+nf.format(cell.getNumericCellValue())+",";
} else {
value =value+ sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()))+",";
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(0 + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue()+",";
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(0 + "行" + j + " 列 is Blank type");
value =value+ ""+",";
break;
default:
System.out.println(0 + "行" + j + " 列 is default type");
value = value+cell.toString()+",";
}
if (value == null || "".equals(value)) {
continue;
}
}
//System.err.println("value:"+value);
return value;
}
}
@RequestMapping
public Map<String, List<String>> upload(HttpServletRequest request,DBQueryData dbQueryData) throws IOException {
System.err.println("進來了。。。。");
Map<String, List<String>> resultMap=new HashMap<>();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 得到上傳的文件
MultipartFile mFile = multipartRequest.getFile("file");
// 得到上傳服務器的路徑
String path = request.getSession().getServletContext()
.getRealPath("/WEB-INF/upload/");
File file = new File(path);
//判斷上傳文件的保存目錄是否存在
if (!file.exists() && !file.isDirectory()) {
System.out.println(path+"目錄不存在,需要創建");
//創建目錄
file.mkdir();
}
// 得到上傳的文件的文件名
String filename = mFile.getOriginalFilename();
InputStream inputStream = mFile.getInputStream();
String extension = filename.lastIndexOf(".") == -1 ? "" : filename
.substring(filename.lastIndexOf(".") + 1);
if ("xls".equals(extension)) {
//ReadExcel readExcel=new ReadExcel();
resultMap= ReadExcel.read2003Excel(inputStream, dbQueryData);
//inputStream.close();
//return resultMap;
// ReadExcel.readExcel(inputStream, filename, dbQueryData);
} else if ("csv".equals(extension)) {
resultMap=CSVUtils.importCsv(inputStream,dbQueryData);
} else {
//throw new IOException("不支持的文件類型");
//read2003Excel(file,dbQueryData);
// return resultMap;
}
// 文件流寫到服務器端
// FileOutputStream outputStream = new FileOutputStream(path);
//outputStream.write(b, 0, length);
//inputStream.close();
//outputStream.close();
inputStream.close();
return resultMap;
}
package com.gpdi.mdata.web.manage.database.utils;
import com.gpdi.mdata.web.manage.database.data.DBList;
import com.gpdi.mdata.web.manage.database.data.DBQueryData;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
public class CSVUtils {
public static String TAB = "\r\n";
static{
Properties prop = System.getProperties();
String os = prop.getProperty("os.name").toLowerCase();
if(os.startsWith("win")){
TAB = "\r\n";
}else if(os.startsWith("linux") || os.startsWith("unix")){
TAB = "\n";
}else if(os.startsWith("mac")){
TAB = "\r";
}
}
public static void exportCsv(HttpServletResponse response,DBQueryData dbQueryData) throwsException {
String fileName=dbQueryData.getTableName()+".csv";
DBhepler dBhepler=new DBhepler();
DBList dbList=dBhepler.search(dbQueryData);
List<String> titles=dbList.getRowTitle();
List<List<String>> data=dbList.getValueList();
StringBuffer sb = new StringBuffer();
OutputStream outputStream = null;
for (int i = 0; i < titles.size(); i++) {
if(i != titles.size() - 1){
sb.append(titles.get(i)).append(",");
}else{
sb.append(titles.get(i)).append(TAB);
}
}
for (int i = 0; i < data.size(); i++) {
List<String> row = data.get(i);
for(int j = 0 ; j < row.size(); j++ ){
if(j != row.size() - 1){
sb.append(row.get(j)).append(",");
}else{
sb.append(row.get(j)).append(TAB);
}
}
}
try {
response.addHeader("Content-Disposition","attachment;fileName=" + newString(fileName.getBytes("UTF-8"),"iso8859-1"));
//response.addHeader("Content-Length", "" + sb.length());
//response.setContentType("application/csv;charset=UTF-8");
response.setContentType("multipart/form-data");//設置文件ContentType類型,這樣設置,會自動判斷下載文件類型
outputStream = response.getOutputStream();
outputStream.write(new byte []{( byte ) 0xEF ,( byte ) 0xBB ,( byte ) 0xBF });//加上bom頭,才不會中文亂碼
outputStream.write(sb.toString().getBytes("UTF-8"));
outputStream.flush();
} catch (IOException e) {
}finally{
try {
outputStream.close();
} catch (IOException e) {
}
}
}
public static List<String> importCsv(File file){
List<String> data = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while((line = br.readLine()) != null){
data.add(line);
}
} catch (Exception e) {
}finally{
try {
br.close();
} catch (IOException e) {
}
}
return data;
}
public static Map<String, List<String>> importCsv(InputStream inputStream,DBQueryData dbQueryData){
List<String> data = new ArrayList<String>();
Map<String, List<String>> resultMap=new HashMap<>();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream,"Utf-8"));
String line = "";
while((line = br.readLine()) != null){
data.add(line);
}
} catch (Exception e) {
}finally{
try {
br.close();
inputStream.close();
} catch (IOException e) {
}
}
List<List<Object>> result=new ArrayList<>();
DBhepler dBhepler=new DBhepler();
if(data.size()>0) {
for (int i = 0; i < data.size(); i++) {
List<Object> valueList = new ArrayList();
if (i != 0) {
String cont[] = data.get(i).replace(""", "").split(",");
for (int j = 0; j < cont.length; j++) {
valueList.add(cont[j]);
}
result.add(valueList);
}
System.err.println("csv:" + data.get(i).replace(""",""));
}
String zdName[] = data.get(0).replace(""", "").split(",");
resultMap=dBhepler.SQLInsert(zdName, result, dbQueryData);
}
else {
return resultMap;
}
return resultMap;
}
}