github地址
将指定数据库所有表及表中数据备份到Excel中,Excel名为当前天时间戳,表名为sheet名,字段为标题第一行,数据在对应字段列 数据库配置修改jdbc.properties
再练POI,导出数据库所有表及数据🐷
package myproject
.util
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFWorkbook
;
import org
.apache
.poi
.ss
.usermodel
.*
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFWorkbook
;
import org
.junit
.Test
;
import org
.slf4j
.Logger
;
import org
.slf4j
.LoggerFactory
;
import org
.springframework
.scheduling
.annotation
.Scheduled
;
import java
.io
.*
;
import java
.sql
.*
;
import java
.text
.SimpleDateFormat
;
import java
.util
.Date
;
import java
.util
.*
;
public final class POIDbToExcel {
@Scheduled(cron
= "0 0 0 * * ? *")
@Test
public void runs(){
bakDBToExcel("xlsx","C:/");
}
private static final String SELECT
= "SELECT";
private static final String FROM
= "FROM";
private static Logger logger
= LoggerFactory
.getLogger(POIDbToExcel
.class);
public static void bakDBToExcel(String excelVersion
,String filePath
){
List
<String> tables
= getTables();
if(tables
.isEmpty()){
logger
.info("库里无表,需检查库是否连接正确");
return;
}
Map
<String
, List
<String>> tablesAndColumns
= getTablesColumn(tables
);
boolean outputTitleToExcel
= outPutTablesAndColumns(tablesAndColumns
, excelVersion
, filePath
);
if(!outputTitleToExcel
){
logger
.info("写出字段时出现异常");
return;
}
Map
<String
, Map
<Integer
, List
<String>>> datas
= getDatas(tablesAndColumns
);
if(outPutDatas(datas
,excelVersion
,filePath
)){
System
.out
.println("success");
}else{
System
.out
.println("error");
}
}
public static List
<String> getTables(){
Connection connection
= JdbcConnectionUtil
.getConnection();
ResultSet resultSet
= null
;
List
<String> tables
= new ArrayList<>();
try {
DatabaseMetaData metaData
= connection
.getMetaData();
resultSet
= metaData
.getTables("","","",new String []{"TABLE"});
while(resultSet
.next()){
String table
= resultSet
.getString(3);
tables
.add(table
);
}
} catch (SQLException e
) {
logger
.info("查询表名时出现错误");
} finally {
JdbcConnectionUtil
.close(connection
,null
,resultSet
);
return tables
;
}
}
public static Map
<String
, List
<String>> getTablesColumn(List
<String> tables
) {
Connection connection
= JdbcConnectionUtil
.getConnection();
ResultSet resultSet
= null
;
PreparedStatement preparedStatement
= null
;
Map
<String
, List
<String>> tablesColumn
= new HashMap<>();
try {
for (int i
= 0; i
< tables
.size(); i
++) {
List
<String> columns
= new ArrayList<>();
String sql
= SELECT
+ " " + "*" + " " + FROM
+ " " + tables
.get(i
);
preparedStatement
= connection
.prepareStatement(sql
);
ResultSetMetaData metaData
= preparedStatement
.getMetaData();
int columnCount
= metaData
.getColumnCount();
for (int j
= 1; j
<= columnCount
; j
++) {
columns
.add(metaData
.getColumnName(j
));
}
tablesColumn
.put(tables
.get(i
), columns
);
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
JdbcConnectionUtil
.close(connection
, preparedStatement
, resultSet
);
return tablesColumn
;
}
}
public static boolean outPutTablesAndColumns(Map
<String
, List
<String>> tablesAndColumns
,String excelVersion
,String filePath
) {
Workbook workbooks
= null
;
if ("xlsx".equalsIgnoreCase(excelVersion
)){
workbooks
= new XSSFWorkbook();
} else if ("xls".equalsIgnoreCase(excelVersion
)){
workbooks
= new HSSFWorkbook();
} else {
logger
.info("Excel版本不存在");
return false;
}
Workbook workbook
= workbooks
;
CellStyle cellStyle
= workbook
.createCellStyle();
cellStyle
.setAlignment(CellStyle
.ALIGN_CENTER
);
Font font
= workbook
.createFont();
font
.setBoldweight(Font
.BOLDWEIGHT_BOLD
);
cellStyle
.setFont(font
);
tablesAndColumns
.forEach((tablename
,columns
)->{
Sheet sheet
= workbook
.createSheet(tablename
);
Row row
= sheet
.createRow(0);
for(int i
=0; i
<columns
.size(); i
++){
Cell cell
= row
.createCell(i
);
cell
.setCellValue(columns
.get(i
));
cell
.setCellStyle(cellStyle
);
}
});
Date date
= new Date();
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat("yyyy-MM-dd");
OutputStream outputStream
= null
;
try {
outputStream
= new FileOutputStream(filePath
.endsWith("/")?filePath
+simpleDateFormat
.format(date
)+"."+excelVersion
:filePath
+"/"+simpleDateFormat
.format(date
)+"."+excelVersion
);
workbook
.write(outputStream
);
} catch (IOException e
){
logger
.info("输出流出现异常");
return false;
} finally {
try {
if(outputStream
!=null
){
outputStream
.close();
}
if(workbook
!=null
){
workbook
.close();
workbooks
.close();
}
} catch (IOException e
) {
logger
.info("关闭出输出流时出现异常");
return false;
}
}
return true;
}
public static Map
<String
,Map
<Integer
,List
<String>>> getDatas(Map
<String
, List
<String>> tablesAndColumns
){
Connection connection
= JdbcConnectionUtil
.getConnection();
Map
<String
,Map
<Integer
,List
<String>>> datas
= new HashMap<>();
try {
tablesAndColumns
.forEach((tablename
,columns
)->{
PreparedStatement preparedStatement
= null
;
ResultSet resultSet
= null
;
Map
<Integer
,List
<String>> map
= new HashMap<>();
List
<String> column
= new ArrayList<>();
String sql
= SELECT
+ " ";
for(int i
=0; i
<columns
.size(); i
++){
if( i
==columns
.size()-1){
sql
= sql
+ columns
.get(i
);
}else{
sql
= sql
+ columns
.get(i
) + ",";
}
column
.add(columns
.get(i
));
}
sql
= sql
+ " " + FROM
+ " " +tablename
;
try {
preparedStatement
= connection
.prepareStatement(sql
);
resultSet
= preparedStatement
.executeQuery();
int count
= 1;
while (resultSet
.next()){
List
<String> data
= new ArrayList<>();
for(int i
=0; i
<column
.size(); i
++){
String string
= resultSet
.getString(column
.get(i
));
if(string
== null
){
string
= "";
}
data
.add(string
);
}
map
.put(count
,data
);
count
++;
}
count
= 1;
datas
.put(tablename
,map
);
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
JdbcConnectionUtil
.close(null
,preparedStatement
,resultSet
);
}
});
}catch (Exception e
){
logger
.info("查询各表数据时出现异常");
return null
;
}finally {
JdbcConnectionUtil
.close(connection
,null
,null
);
}
return datas
;
}
public static boolean outPutDatas(Map
<String
, Map
<Integer
, List
<String>>> datas
,String excelVersion
,String filePath
){
Workbook workbook1
= null
;
Date date
= new Date();
SimpleDateFormat simpleDateFormat
= new SimpleDateFormat("yyyy-MM-dd");
InputStream inputStream
= null
;
OutputStream outputStream
= null
;
try {
inputStream
= new FileInputStream(filePath
.endsWith("/") ? filePath
+ simpleDateFormat
.format(date
) + "." + excelVersion
: filePath
+ "/" + simpleDateFormat
.format(date
) + "." + excelVersion
);
if ("xlsx".equalsIgnoreCase(excelVersion
)) {
workbook1
= new XSSFWorkbook(inputStream
);
} else if ("xls".equalsIgnoreCase(excelVersion
)) {
workbook1
= new HSSFWorkbook(inputStream
);
}
Workbook workbook
= workbook1
;
datas
.forEach((tablename
,data
)->{
Sheet sheet
= workbook
.getSheet(tablename
);
data
.forEach((rownum
,data_
)->{
Row row
= sheet
.createRow(rownum
);
for(int i
=0; i
<data_
.size(); i
++){
Cell cell
= row
.createCell(i
);
cell
.setCellValue(data_
.get(i
));
}
});
});
outputStream
= new FileOutputStream(filePath
.endsWith("/") ? filePath
+ simpleDateFormat
.format(date
) + "." + excelVersion
: filePath
+ "/" + simpleDateFormat
.format(date
) + "." + excelVersion
);
workbook
.write(outputStream
);
workbook
.close();
} catch (IOException e
) {
logger
.info("在写出数据时出现异常");
return false;
} finally {
try {
if(outputStream
!=null
){
outputStream
.close();
}
if(workbook1
!=null
){
workbook1
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
return true;
}
}