?? poixlsdb.java
字號(hào):
package com.lingtu.poixls;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 支持xls的2003的xls版本。office2007不支持。
* @author Administrator
*
*/
public class PoiXlsDb {
public static boolean isdb = false;
public static void main(String[] args) {
PoiXlsDb pxd = new PoiXlsDb();
try {
pxd.dealxls();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void dealxls() throws Exception {
long currenttimeStart=System.currentTimeMillis();
int rightnum=0;
int errornum=0;
String brancode="";
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream("d://a.xls"));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
// sheet=0 sheet=1
//test
HSSFSheet sheet = wb.getSheetAt(0);
//HSSFSheet sheet = wb.getSheetAt(1);
int rows = sheet.getPhysicalNumberOfRows();// 行數(shù)
System.out.println("test.xml rows=" + rows);
Statement stmt = null;
PreparedStatement ps=null;
if (isdb) {
String sql = "insert into EMAP_MGR_MARKING_POINT(" +
"mark_code,mark_name,city,point,mark_type, " +
" bran_code,self_equi_stat,mark_addr)"+
" values (?,?,?,db2gse.ST_Point(cast(? as double),cast(? as double), 1003),?,?,?,?)";
Connection conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.clearParameters();
}
try {
String[] value=null;
for (int r = 0; r < rows; r++) {
double dlon=0,dlat=0;
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getLastCellNum();
// 每行單元格數(shù)
value = new String[cells];
//System.out.println("此行單元格數(shù):" + cells);
String num = "";
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);// 單元格
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value[c] += cell.getStringCellValue() + "\t";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value[c] += (long) cell.getNumericCellValue()
+ "\t";
break;
case HSSFCell.CELL_TYPE_STRING:
value[c] += cell.getStringCellValue() + "\t";
break;
default:
value[c] += "\t";
}
if (value[c] != null) {
value[c] = value[c].substring(4).trim();
if (value[c].indexOf(" ") > 0)
value[c] = value[c].substring(0, value[c]
.indexOf(" ") - 1);
}
}else{
if (value[c] == null) {
value[c] = " ";
}
}
// int k = c + 1;
}
// 下面可以將查找到的行內(nèi)容用SQL語(yǔ)句INSERT到sqlserver數(shù)據(jù)庫(kù)
String lin="";
//清楚名稱(chēng)中的回車(chē)字符
if (!value[2].equals("")){
String temp=value[2];
temp=temp.replaceAll("\r\n", "");
// temp=temp.replaceAll(" ", "");
// temp=temp.replaceAll("?", "");
value[2]=temp;
}
//清楚中文中的回車(chē)字符
if (!value[4].equals("")){
String temp=value[4];
temp=temp.replaceAll("\r\n", "");
// temp=temp.replaceAll(" ", "");
// temp=temp.replaceAll("?", "");
value[4]=temp;
}
if (cells==10){
lin ="cells="+cells+":"+r+"->"+ value[0]+"##"+value[1] + "##" + value[2] + "##" + value[3]+ "##" + value[4]+"##"+ value[5] + "##" +
value[6] + "##" + value[7] + "##" + value[8]+ "##" + value[9];
}
else if (cells==11){
lin ="cells="+cells+":"+r+"->"+ value[0]+"##"+value[1] + "##" + value[2] + "##" + value[3]+ "##" + value[4]+"##"+ value[5] + "##" +
value[6] + "##" + value[7] + "##" + value[8]+ "##" + value[9]+"##" + value[10];// +
}else if (cells==12){
lin ="cells="+cells+":"+r+"->"+ value[0]+"##"+value[1] + "##" + value[2] + "##" + value[3]+ "##" + value[4]+"##"+ value[5] + "##" +
value[6] + "##" + value[7] + "##" + value[8]+ "##" + value[9]+"##" + value[10]+"##" +value[11];
if (!value[11].trim().equals("") && r>0){
String strlonlat=value[11].trim();
StringTokenizer st=new StringTokenizer(strlonlat,",");
System.out.println("aaa"+strlonlat);
dlon=Double.parseDouble(st.nextToken());
dlat=Double.parseDouble(st.nextToken());
System.out.println(strlonlat+" "+dlon+" "+dlat);
}
}
//分析用戶名稱(chēng)為空
//value[1]商戶編號(hào)
//value[2]商戶名稱(chēng)
//value[3]適用卡種,這里沒(méi)有用
//value[4]促銷(xiāo)信息
//value[5]商戶地址
//value[7]有限日期
//value[8]城市用戶類(lèi)型
//value[9]城市名稱(chēng)
//value[10]分行名稱(chēng)
//value[11]經(jīng)緯度的小數(shù)部分
//過(guò)濾商戶編號(hào)為空,商戶名稱(chēng)為空,有效日期為空,城市名稱(chēng)為空,分行名稱(chēng)為空
if (value[1].trim().equals("")||value[2].equals("")||value[7].trim().equals("")|| value[9].trim().equals("")|| value[10].trim().equals("")){
errornum++;
}
else{
//判斷商戶編號(hào)不為空,但不是數(shù)字類(lèi)型
String id=value[1];
String s="";
int i = -1 ;
if (!id.equals("")){
//System.out.print(id);
if (id.length()>=2){
s=id.substring(0,2);
}
try{
i = Integer.parseInt(s);
}
catch(NumberFormatException numex){
//如果轉(zhuǎn)換失敗,那么你的處理如下
errornum++;
}
}
//判斷id編號(hào)為數(shù)字類(lèi)型
if (i>=0){
//判斷城市是否滿足。
boolean iscity=iscity2(value[9]);
if (!iscity){
errornum++;
}else{
brancode=getbrancode(value[10]);
if (brancode.equals("")){
errornum++;
}else{
System.out.println(lin);
if (isdb){
//stmt.executeUpdate(sql);
ps.setString(1,"TS"+String.valueOf(r));
ps.setString(2,value[2].trim());
ps.setString(3,value[9].trim()+"市");
ps.setDouble(4,dlon);
ps.setDouble(5,dlat);
ps.setString(6,"10");
ps.setString(7,brancode);
ps.setString(8,"0");
ps.setString(9,value[5].trim());
int insertret=ps.executeUpdate();
if (insertret>0){
rightnum++;
}
}
}
}
}
}
//System.out.println(sql);
}
}
long currenttimeEnd=System.currentTimeMillis();
System.out.println("---------------------------");
//System.out.println("開(kāi)始時(shí)間:"+currenttimeStart);
System.out.println("總行數(shù):" + rows+"\r\n插入數(shù)據(jù):"+rightnum +"\r\n錯(cuò)誤記錄數(shù):" +errornum+"" );
System.out.println("運(yùn)行秒數(shù):"+(currenttimeEnd-currenttimeStart)/1000);
System.out.println("---------------------------");
//System.out.println("結(jié)束時(shí)間:"+currenttimeEnd);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (isdb){
try {
stmt.close();
stmt = null;
} catch (Exception e) {
}
try {
conn.close();
conn = null;
} catch (Exception e) {
}
}
}
}
private Connection conn;
public static Connection getConnection() throws Exception {
Connection conn = null;
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection(
"jdbc:db2://182.119.171.187:60000/bcgis", "bcgis", "bcgis");
return conn;
}
public void deal() {
try {
conn = this.getConnection();
this.TranBranchPy(conn);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加標(biāo)注點(diǎn)
*
* @param poi -
* 標(biāo)注點(diǎn)
* @throws com.lingtu.emap.mgr.DataAccessException
*/
public void TranBranchPy(Connection conn) throws Exception {
String sql = "select bran_code,bran_name from EMAP_MGR_BRANCH ";
String sql2 = "select py,py_code from zh_py where zh=?";
PreparedStatement ps = null;
PreparedStatement ps2 = null;
ArrayList alist = new ArrayList();
// String sql2="update EMAP_MGR_BRANCH set py1=";
ResultSet rs = null;
try {
// ps=dataCon.getcon().prepareStatement(sql);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
String zhstr = "";
String py2 = "";
String py1 = "";
String tmp = "";
String temp = "";
String temp2 = "";
while (rs.next()) {
temp = "";
temp2 = "";
// alist.add(rs.getString("bran_name"));
zhstr = rs.getString("bran_name").trim();
String bran_code = rs.getString("bran_code");
// System.out.println("zhstr="+zhstr);
for (int kkk = 0; kkk < zhstr.length(); kkk++) {
String zh = zhstr.substring(kkk, kkk + 1);
// System.out.println("zh="+zh);
tmp = this.getPy(conn, zh);
// System.out.println("py1="+py1);
temp = temp + tmp.substring(0, 1);
temp2 = temp2 + tmp;
}
py1 = temp;
py2 = temp2;
// System.out.println("zhstr="+zhstr+" py1="+py1+" py2="+py2);
sql2 = "update EMAP_MGR_BRANCH set py1='" + py1 + "'"
+ " ,py2='" + py2 + "' where bran_code='" + bran_code
+ "'";
System.out.println(sql2);
// ps2=conn.prepareStatement(sql2);
// ps2.executeUpdate();
// conn.commit();
}
} catch (SQLException ex) {
// logger.error(ex.getMessage());
throw new Exception("添加入庫(kù)錯(cuò)誤");
} finally {
if (ps != null)
ps.close();
if (ps2 != null)
ps2.close();
if (conn != null)
conn.close();
}
}
public String getPy(Connection conn, String zh) throws Exception {
// String sql="select bran_name from EMAP_MGR_BRANCH ";
String sql = "select py_code from zh_py where zh='" + zh + "'";
PreparedStatement ps = null;
ArrayList alist = new ArrayList();
String py1 = "";
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -