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

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

时间: 2024-09-02 00:36:32

exl导入数据库的相关文章

Java实现Excel导入数据库,数据库中的数据导入到Excel

实现的功能: Java实现Excel导入数据库,如果存在就更新 数据库中的数据导入到Excel 1.添加jxl.jar mysql-connector-java.1.7-bin.jar包到项目的lib目录下­ 2.Excel文件目录:D://book.xls 3.数据库名:javenforexcel 4.表名:stu 5.编写类:连接mysql的字符串方法.插入的方法.实体类­­ 表结构如下 : 连接数据库的工具类 package com.javen.db; import java.sql.Co

几个数据库的小案例(一):将文本文件中的信息导入数据库的表中

从文本文件添加到数据库用户表的记录(有两个文件:frmMain.cs  SqlHelper.cs  ) //FrmMain.cs//作者:Meusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Fo

导入数据库出现html lang=&#39;zh&#39; dir=&#39;ltr&#39; class=&#39;ie ie7错误代码解决方法

今天遇到一个客户导入数据库错误的问题,导入后出现错误代码如下: <!DOCTYPE HTML><html lang='zh' dir='ltr' class='ie ie7'><meta charset="utf-8" /><meta name="robots" content="noindex,nofollow" /><meta http-equiv="X-UA-Compatibl

jeesite导入数据库错误:java.sql.SQLException: Incorrect string value: &#39;\xE4\xB8\xAD\xE5\x9B\xBD&#39; for column &#39;name&#39; at row 1问题解决

如果使用mvn antrun:run -Pinit-db进行数据库导入导致出现如下错误: 解决方法: 这个是由于新建数据库没有选择默认字符集导致的,只要选择utf-8即可. jeesite导入数据库错误:java.sql.SQLException: Incorrect string value: '\xE4\xB8\xAD\xE5\x9B\xBD' for column 'name' at row 1问题解决

python之TXT数据导入数据库

为了导入数据,可以先对数据做些处理,让其更容易导入数据库 #!/usr/bin/python #coding=utf-8 import _mysql,sys,time #读入数据函数 def add_data(id,name,created_time):     try:         conn=_mysql.connect('127.0.0.1','root','')         conn.query("set names utf8")         conn.query(&

PHP读取CSV大文件导入数据库的示例

对于数百万条数据量的CSV文件,文件大小可能达到数百M,如果简单读取的话很可能出现超时或者卡死的现象. 为了成功将CSV文件里的数据导入数据库,分批处理是非常必要的. 下面这个函数是读取CSV文件中指定的某几行数据: /** * csv_get_lines 读取CSV文件中的某几行数据 * @param $csvfile csv文件路径 * @param $lines 读取行数 * @param $offset 起始行数 * @return array * */ function csv_get

atitit.sql server2008导出导入数据库大的表格文件... oracle mysql

atitit.sql server2008导出导入数据库大的表格文件... 1. 超过80M的文件是不能在查询分析器中执行的 1 2. Oracle ,mysql大的文件导入 1 2.1. 使用sql文件 1 2.2. 使用dmp二进制文件(oracle only) 1 2.3. Other 导出txt,excel在导入( 不推荐),常常不能导入 1 3. 本机导入 1 4. 远程导入 2 5. syaolon msg 2 6. 参考 3 1. 超过80M的文件是不能在查询分析器中执行的 imE

CentOS6.4下Mysql数据库的安装与配置,导入数据库,授权远程ip

卸载掉原有mysql 因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面,我们可以通过如下命令来查看我们的操作系统上是否已经安装了mysql数据库 [[email protected] ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 有的话,我们就通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉 [[email protected

jdbc 使用properties导入数据库信息

package day01; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; public class TestClassLoad { publ