這裏的Excel是sqlserver數據庫導出的用戶表數據 如今要把這些數據導入到Oracle 而且把用戶的電子簽名導出 放到工程的相應路徑java
package com.pm360.pip.test;c++
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import com.pm360.mda.platform.util.Guid;
import com.pm360.mda.platform.util.Logger;
import com.pm360.mda.platform.util.StringUtil;sql
public class ImportUser
{
/**
* 根據Excel導入數據
* @return retStr 導入結果
* @throws Exception
*/
public static String readExcel() throws Exception
{
Connection conn = null;
String excelPath = "D:/picture/demo.xlsx";
String baseurl = "D:/picture";
String sql = "";
String retStr = "";
try
{
// 讀取EXCEL 文件
File writefile = new File(baseurl);
if(writefile.exists())
writefile.delete();
if (!writefile.exists())
writefile.mkdirs();
excelPath = "d:/users.xls";
File file = new File(excelPath);
if (!file.exists())
{
retStr = "EXCEL文件上傳失敗!";
} else
{
// 鏈接數據源
//conn = DBConn.getConnection("PIP");
//conn.setAutoCommit(false);
// 導入Execle數據
Workbook workbook = Workbook.getWorkbook(new File(excelPath));
for (int i = 0; i < workbook.getSheets().length; i++)
{
Sheet sheet = workbook.getSheet(i);// 獲取第一張Sheet表
if (sheet.getRows() >= 1)
{
Cell titleCell = null;
Cell dataCell = null;
String title = "";
String data = "";
for (int r = 1; r < sheet.getRows(); r++)// 從2行開始取記錄
{
Map<String, Object> listMap = new HashMap<String, Object>();
titleCell = sheet.getCell(0, r);
title = titleCell.getContents();
for (int c = 0; c < sheet.getColumns(); c++)
{
String userId = Guid.getGuid();
dataCell = sheet.getCell(c, r);
title = titleCell.getContents();
data = dataCell.getContents();
listMap.put(title, data);
String[] userArr = data.split(",");
String actual_name = userArr[1];
String user_name = userArr[2];
String password = userArr[3];
String picture = userArr[4].replace("0x","");
System.out.println(picture);
//寫入結構化數據到數據庫
//sql = "INSERT INTO CM_USERS (USER_ID,ACTUAL_NAME, USER_NAME, PASSWORD) VALUES ('"+userId+"','"+actual_name+"','"+user_name+"','"+password+"')";
//DBUtil.executeInsertSql(conn, sql);
//導出電子簽章圖片
if(StringUtil.validate(picture) && !"null".equalsIgnoreCase(picture))
{
saveToImgFile(picture.toString().toUpperCase(),baseurl+"/"+userId+".gif");
}
break;
}
// }
// else
// continue;
}
}
}
//conn.commit();
}
} catch (Exception e)
{
Logger.error(e);
if (conn != null)
conn.rollback();
return retStr;
} finally
{
if (conn != null)
conn.close();
conn = null;
}
return retStr;
}
public static void saveToImgFile(String src,String output){
if(src==null||src.length()==0){
return;
}
try{
FileOutputStream out = new FileOutputStream(new File(output));
byte[] bytes = src.getBytes();
for(int i=0;i<bytes.length;i+=2){
out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
private static int charToInt(byte ch){
int val = 0;
if(ch>=0x30&&ch<=0x39){
val=ch-0x30;
}else if(ch>=0x41&&ch<=0x46){
val=ch-0x41+10;
}
return val;
}
public static void main(String[] args) {
try {
readExcel();
} catch (Exception e) {
e.printStackTrace();
}
}數據庫
}
sqlserver