資源簡介
運用jdbc連接數據庫,將數據庫的表導出到excel中,將excel文件導入到數據庫中,支持中文,可以進行導入前的數據修改。

代碼片段和文件信息
package?excel;
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.ResultSetmetaData;
import?java.sql.SQLException;
import?java.sql.Statement;
import?java.io.*;
import?java.util.ArrayList;
import?java.util.List;
import?jxl.Workbook;
import?jxl.write.Label;
import?jxl.write.WritableSheet;
import?jxl.write.WritableWorkbook;
public?class?BDToExcel{
private?static?Connection?conn=null;
private?static?Statement?stat=null;
private?static?ResultSet?rs=null;
private?PreparedStatement?prep=null;
private?WritableWorkbook?workbook;
????private?WritableSheet?sheet;??
??
public??BDToExcel(){
}
/* ?*?建立數據庫連接資源
?*/
public?void?Connect(){
try?{
Class.forName(“com.mysql.jdbc.Driver“).newInstance();
String?url?=“jdbc:mysql://127.0.0.1:3306/testdb?autoReconnect=true&user=root&password=1234567&useUnicode=true&characterEncoding=utf-8“;
conn=DriverManager.getConnection(url);
stat=conn.createStatement();
System.out.println(“數據庫連接成功?!埃?
}?catch?(Exception?e)?{
e.printStackTrace();
System.out.println(“數據庫連接失敗。“);
}
}
/* ?*?關閉數據庫連接資源
?*?close()
?*/
public?void?close(){
try?{
if(rs!=null)?{
rs.close();
rs=null;
}
if(stat!=null)?{
stat.close();
stat=null;
}
if(prep!=null){?
prep.close();
prep=null;
}
if(conn!=null)?{
conn.close();
conn=null;
}
}?catch?(SQLException?e)?{
e.printStackTrace();
}
}
//*workbook?????*?生成Excel文件??????*?@param?path?????????文件路徑?
??public?void?createExcelFile(String?path){
???????try{??
???????????OutputStream?os=new?FileOutputStream(path);???
???????????workbook=Workbook.createWorkbook(os);???
???????}catch(Exception?e){??
???????????e.printStackTrace();??
??????? }??
??}??
??//*sheet*?????*?生成sheet表?????*?@param?intsheet?????工作表次序??????*?@param?sheetName????工作表名稱
??public?void?createSheetName(int?intsheetString?sheetName){
???????try{??
???????????sheet?=?workbook.createSheet(sheetName?intsheet);?//添加工作表??
???????}catch(Exception?e){??
???????????e.printStackTrace();??
??????? }??
??}??
??// 保存Excel文件
??public?void?WorkBookWrite(){
???????try{??
???????????workbook.write();???
???????????workbook.close();??
???????}catch(Exception?e){??
???????????e.printStackTrace();??
???????}??
??}?
??
??// 寫
??public?void?write?(Listject[]>?listData){
?? Label?label?=?null;
object[]?obj?=?null;?
?? for?(int?i=0;i obj?=?listData.get(i);
//?列
for?(int?j=0;j //?Label構造器中有三個參數,第一個為列,第二個為行,第三個則為單元格填充的內容
label?=?new?Label(j?i?(obj[j]==null)?““:obj[j].toString());
//?將被寫入數據的單元格添加到工作表
try{
sheet.addCell(label);
}catch(Exception?e){
????? System.out.println(“錯誤“+e.toString());
}
}
System.out.println(“共:“+listData.size()+“行,“+“
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5184??2014-04-16?15:03??數據庫與excel導入導出\BDToExcel.java
?????文件???????6523??2014-04-15?21:17??數據庫與excel導入導出\ExcelTest.java
?????文件????????676??2014-04-16?09:58??數據庫與excel導入導出\Test(源文件)\.classpath
?????文件????????285??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.myme
?????文件???????1410??2014-04-15?08:56??數據庫與excel導入導出\Test(源文件)\.project
?????文件????????500??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.settings\.jsdtscope
?????文件????????330??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.settings\org.eclipse.jdt.core.prefs
?????文件?????????49??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件??????????6??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????文件???????5184??2014-04-16?15:03??數據庫與excel導入導出\Test(源文件)\src\excel\BDToExcel.java
?????文件???????6523??2014-04-15?21:17??數據庫與excel導入導出\Test(源文件)\src\excel\ExcelTest.java
?????文件????????724??2014-04-15?08:55??數據庫與excel導入導出\Test(源文件)\WebRoot\index.jsp
?????文件?????????36??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\WebRoot\me
?????文件????????371??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\web.xm
?????文件???????5529??2014-04-16?15:03??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\classes\excel\BDToExcel.class
?????文件???????4920??2014-04-16?09:58??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\classes\excel\ExcelTest.class
?????文件?????704543??2014-04-16?09:58??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\lib\jxl.jar
?????文件?????709922??2014-03-27?10:06??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\lib\mysql-connector-java-5.1.7-bin.jar
?????文件????1793021??2014-04-15?08:28??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\lib\poi-3.8-beta5-20111217.jar
?????文件?????583286??2014-04-15?15:55??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\lib\sqljdbc.jar
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\classes\excel
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\classes
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF\lib
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\src\excel
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot\me
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot\WEB-INF
?????目錄??????????0??2014-04-14?20:27??數據庫與excel導入導出\Test(源文件)\.myeclipse
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\.settings
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\src
?????目錄??????????0??2014-04-16?15:09??數據庫與excel導入導出\Test(源文件)\WebRoot
............此處省略5個文件信息
- 上一篇:水晶易表教程經典
- 下一篇:ST16型示波器故障分析與檢修
評論
共有 條評論