• exl导入数据库


    java] view plaincopy
     
    1. /** 
    2. * @Description: 得到Excel文档,把文档中的数据批量导入到数据库中 
    3. * 1、找到上传的数据,2、把数据放到List集合中,3、把List集合中的数据更新到数据库 
    4. * @return void 
    5. * @throws 
    6. */  
    7. public String cmdImpQxsj() {  
    8. String sysGuid = getWorkDTO().getAsString("sysGuid");  
    9. String webPath = WebConfig.getInstance().getContext_path();  
    10. String filePath = this.getFilePath(sysGuid);  
    11. String longPath = webPath+filePath;  
    12.   
    13. HSSFWorkbook workbook = POIExcelUtil.getExistHSSFWorkbook(new File(longPath));  
    14. List<List<Object>> list = new ArrayList<List<Object>>();  
    15. if (workbook != null) {  
    16. HSSFSheet sheet = workbook.getSheetAt(0);  
    17. if (sheet != null) {  
    18. int rownum = sheet.getLastRowNum();  
    19. int columnnum = sheet.getRow(0).getLastCellNum();  
    20. for(int i=3;i<rownum;i++){  
    21. List<Object> row = new ArrayList<Object>();  
    22. Object value = "";  
    23. for (int j = 1; j < columnnum-1; j++) {  
    24. Cell cell = sheet.getRow(i).getCell(j);  
    25. if (cell == null) {  
    26. row.add("");  
    27. else if (j==2 || j==3 || j == 5  
    28. || j==6 || j==7 || j == 9 || j==10  
    29. || j==11 || j == 12 || j==13 || j == 15  
    30. || j==16 || j==18 || j == 19 || j==20) {  
    31. if(cell.getStringCellValue() != null || !"".equals(cell.getStringCellValue())){  
    32. value = cell.getStringCellValue();  
    33. row.add(value);  
    34. }  
    35. }else if(j == 1){  
    36. if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){  
    37. int d = (int)cell.getNumericCellValue();  
    38. String numb = d+"";  
    39. row.add(numb);  
    40. }else if(cell.getStringCellValue() != null || !"".equals(cell.getStringCellValue())){  
    41. value = cell.getStringCellValue();  
    42. row.add(value);  
    43. }  
    44. }else if (j == 4 || j == 14 || j == 17){  
    45. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
    46. value = cell.getDateCellValue();  
    47. else {  
    48. value = cell.getStringCellValue();  
    49. }  
    50. if (value == null || "".equals(value.toString())) {  
    51. row.add("");  
    52. else {  
    53. SimpleDateFormat sdf = new SimpleDateFormat(  
    54. "yyyy-MM-dd HH:mm");  
    55. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
    56. try {  
    57. value = sdf.format(value);  
    58. catch (Exception e) {  
    59. value = "";  
    60. }  
    61. }  
    62. row.add(value);  
    63. }  
    64. }else if (j == 8){  
    65. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
    66. value = cell.getDateCellValue();  
    67. else {  
    68. value = cell.getStringCellValue();  
    69. }  
    70. if (value == null || "".equals(value.toString())) {  
    71. row.add("");  
    72. else {  
    73. SimpleDateFormat sdf = new SimpleDateFormat(  
    74. "yyyy-MM-dd HH:mm:ss");  
    75. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
    76. try {  
    77. value = sdf.format(value);  
    78. catch (Exception e) {  
    79. value = "";  
    80. }  
    81. }  
    82. row.add(value);  
    83. }  
    84. }  
    85. }  
    86.   
    87. list.add(row);  
    88. }  
    89.   
    90. try {  
    91. impToDB(list);  
    92. getWorkDTO().put("jsonString", "{success:true}");  
    93. catch (SQLException e) {  
    94. getWorkDTO().put("jsonString", "{failure:true}");  
    95. e.printStackTrace();  
    96. return ICnsExResult.RUSULT_ERROR;  
    97. }  
    98.   
    99. }  
    100. }  
    101. return ICnsExResult.RUSULT_SUCCESS;  
    102.   
    103. }  
    104.   
    105. /** 
    106. * @throws SQLException 
    107. * @Description: 把Excel数据导入到数据库 
    108. * @return void 
    109. * @throws 
    110. */  
    111. @SuppressWarnings("static-access")  
    112. public void impToDB(List<?> list) throws SQLException{  
    113. String sql = "";  
    114. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss");  
    115. String cretDate = format.format(new Date());//导入时间  
    116. String jobID = getSessionDTO().getAsString(SessionDTO.LOGIN_USR_JOBID);//导入上传人工号  
    117.   
    118. sql = "insert into us_app.tb_ope_qxsc(guid,qxgd,qxmc,gddqzt," +  
    119. "dqztxgsj,clqk,gdsqr,sqrzb,fxsj,zy,jzmc,qxlb,sbmc,jxr," +  
    120. "jxyssj,wxr,xqr,xqsj,tjpzr,bz,gzpbh,scsj,scgh) " +  
    121. "values(sys_guid(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";  
    122. Connection con = this.baseDAO.toGetSession().connection();  
    123. PreparedStatement ps = con.prepareStatement(sql);  
    124. final int batchSize = 60; //设置批处理数据的条数  
    125. int count = 0;  
    126. for(int p=0;p<list.size();p++){  
    127. List<?> singleList = (List<?>)list.get(p);  
    128. /*try { 
    129. fileDate = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss").parse(cretDate); 
    130. date1 = sdf.parse((String) singleList.get(3)); 
    131. date2 = fmat.parse((String) singleList.get(7)); 
    132. date3 = sdf.parse((String) singleList.get(13)); 
    133. date4 = sdf.parse((String) singleList.get(16)); 
    134. } catch (ParseException e) { 
    135. e.printStackTrace(); 
    136. } */  
    137.   
    138. ps.setString(1, (String) singleList.get(0));  
    139. ps.setString(2, (String) singleList.get(1));  
    140. ps.setString(3, (String) singleList.get(2));  
    141. if("".equals(singleList.get(3))){  
    142. ps.setTimestamp(4, null);  
    143. }else{  
    144. ps.setTimestamp(4, returnDate((String) singleList.get(3)));  
    145. }  
    146. ps.setString(5, (String) singleList.get(4));  
    147. ps.setString(6, (String) singleList.get(5));  
    148. ps.setString(7, (String) singleList.get(6));  
    149.   
    150. if("".equals(singleList.get(7))){  
    151. ps.setTimestamp(8, null);  
    152. }else{  
    153. ps.setTimestamp(8, returnDate2((String) singleList.get(7)));  
    154. }  
    155. ps.setString(9, (String) singleList.get(8));  
    156. ps.setString(10, (String) singleList.get(9));  
    157. ps.setString(11, (String) singleList.get(10));  
    158. ps.setString(12, (String) singleList.get(11));  
    159. ps.setString(13, (String) singleList.get(12));  
    160. if("".equals(singleList.get(13))){  
    161. ps.setTimestamp(14, null);  
    162. }else{  
    163. ps.setTimestamp(14, returnDate((String) singleList.get(13)));  
    164. }  
    165. ps.setString(15, (String) singleList.get(14));  
    166. ps.setString(16, (String) singleList.get(15));  
    167. if("".equals(singleList.get(16))){  
    168. ps.setTimestamp(17, null);  
    169. }else{  
    170. ps.setTimestamp(17, returnDate((String) singleList.get(16)));  
    171. }  
    172. ps.setString(18, (String) singleList.get(17));  
    173. ps.setString(19, (String) singleList.get(18));  
    174. ps.setString(20, (String) singleList.get(19));  
    175.   
    176. ps.setTimestamp(21, new java.sql.Timestamp(new java.util.Date().getTime()));  
    177. ps.setString(22, jobID);  
    178.   
    179. ps.addBatch();  
    180. if(++count % batchSize == 0) {  
    181. ps.executeBatch();  
    182. ps.clearBatch();  
    183. }  
    184. }  
    185.   
    186.   
    187. ps.executeBatch();  
    188. ps.clearBatch();  
    189.   
    190. }  
    191.   
    192.   
    193. public Timestamp returnDate(String date){  
    194. Timestamp dateTime = null;  
    195. try{  
    196. DateFormat dateFormat;  
    197. dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm",Locale.ENGLISH);//设定格式  
    198. dateFormat.setLenient(false);  
    199. java.util.Date timeDate = dateFormat.parse(date);//util类型  
    200. dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型  
    201. }catch(Exception ex){  
    202. ex.printStackTrace();  
    203. }  
    204. return dateTime;  
    205. }  
    206.   
    207. public Timestamp returnDate2(String date){  
    208. Timestamp dateTime = null;  
    209. try{  
    210. DateFormat dateFormat;  
    211. dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",Locale.ENGLISH);//设定格式  
    212. dateFormat.setLenient(false);  
    213. java.util.Date timeDate = dateFormat.parse(date);//util类型  
    214. dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型  
    215. }catch(Exception ex){  
    216. ex.printStackTrace();  
    217. }  
    218. return dateTime;  
    219. }     

    转(http://blog.csdn.net/haima573979352/article/details/13505339)

  • 相关阅读:
    第六章:面向对象(下)
    第四章:流程控制和数组
    第三章:数据类型和运算符
    第二章:理解面向对象
    数据库:表设计阶段
    第一章:java语言概述与开发环境
    HTML DOM
    补漏
    不错的资源哦
    阻止事件冒泡
  • 原文地址:https://www.cnblogs.com/sunshine-in/p/4095504.html
Copyright © 2020-2023  润新知