将Excel文件数据导入到数据库表中(Java版)
Java代码
1.<%@page language="java" contentType="text/html; charset=utf-8" %>
2.<%@page import="java.io.File"%>
3.<%@page import="java.io.InputStream"%>
4.<%@page import="java.io.FileInputStream"%>
5.<%@page import="java.util.Vector"%>
6.<%@page import="java.util.Iterator"%>
7.<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
8.<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
9.<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
10.<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
11.<%@page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
12.<%@page import="com.fuyun.hpmon.PubUtil"/>
13.<%@page import="com.fuyun.hp.hibernate.mapping.RadioType"%>
14.<%@page import="com.fuyun.hp.hibernate.mapping.RadioTypeDAO"%>
15.<%@page import="com.fuyun.hpmon.SpringContext"/>
16.<%@page import="hibernate3.HibernateTransactionManager"/>
17.<%@page import="ansaction.TransactionDefinition"/>
18.<%@page import="ansaction.TransactionStatus"/>
19.<%@page import="ansaction.support.DefaultTransactionDefinition"/>
20.<%
21. //出于考虑显示格式,实际运行时,请将代码中的全角空格转换为半角空格
22. //代码中的PubUtil.nvl(Object)方法,当Object为null时返回空串(也就是类似Oracle中的nvl方法)
23. request.setCharacterEncoding("utf-8");
24. File tempPath = new File(getServletContext().getRealPath("/")
25. + AdsUtil.TEMP_UPLOAD_PATH);
26. if (!ists())
27. return;
28.
29. String fileName = "radioTree.xls";
30. fileName = String() + "\\" + fileName;
31. InputStream inp = new FileInputStream(fileName);
32. HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
33. HSSFSheet sheet = wb.getSheetAt(0);
34. RadioType rt;
35. RadioTypeDAO rtDao = Instance();
36.
37. HibernateTransactionManager tManager = TransactionManager();
38. TransactionDefinition td = new DefaultTransactionDefinition();
39. TransactionStatus ts = Transaction(td);
40.
41. for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)wIterator(); rit.hasNext(); ) {
42. HSSFRow row = ();
43. boolean existFlag = true;
44. for(short index = 0;index < PhysicalNumberOfCells();index ++){
45. HSSFCell cell = Cell(index);
46. if(index == LastCellNum() && cell == null)
47. existFlag = false;
48. else if(index < LastCellNum() && cell == null)
49.
continue;
50. else
51. break;
52. }
53. if(existFlag == false){
54. break;
55. }
56. else{
57. Vector<String> values = new Vector<String>();
58. rt = null;
59. try{
60. RowNum() == 0)//从第2行开始读取
61. continue;
62. for (int index = 0; index < 5; index ++) {
63. HSSFCell cell = Cell(new Short(index + ""));
64. if(cell == null)
65. values.add("");
66. else{
67. CellType() == cell.CELL_TYPE_NUMERIC){
68. values.add(String.format("%.0f", NumericCellValue()));
69. }
70. else
71. values.add(PubUtil.StringCellValue()));
72. }
java库73. }
74.
75. if("".equals(PubUtil.(0)))){
76. rt = new RadioType();
77. }
78. else{
79. rt = ((0));
80. }
81. rt.(0));//id
82. rt.setParentId(PubUtil.(1)));//上级id
83. rt.setName(PubUtil.(2)));//名称
84. rt.setType(PubUtil.(3)));//类型
85. rt.setOrderIndex(Integer.parseInt(PubUtil.(4))));//排序
86.
87. if("".equals(PubUtil.(0)))){
88. rtDao.save(rt);
89. }
90. else{
91. rtDao.update(rt);
92. }
93. out.println(rt);
94. }
95. catch(Exception e){
96. llback(ts);
97. e.printStackTrace();
98. out.print("{success: false, info:'导入失败!'}");
99. return;
100. }
101. }
102. }
103. tManagermit(ts);
104. out.print("{success: true}");
105. %>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论