讀取Excel中的十六進制轉化爲圖片 存到硬盤

這裏的Excel是sqlserver數據庫導出的用戶表數據 現在要把這些數據導入到Oracle 並且把用戶的電子簽名導出 放到工程的相應路徑

 

 

package com.pm360.pip.test;

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;

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();
    }
    
   }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章