Home > php教程 > PHP源码 > excel导入数据库oracle

excel导入数据库oracle

PHP中文网
Release: 2016-05-26 08:18:21
Original
1221 people have browsed it

1.读取execl(只能读取.xls的execl,即只能读取03版的),如果是.xlsx类型的话 手工转化一下即可 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

 

/**

 * @author 作者:gzh

 * @version 创建时间:2015-4-30上午11:26:13

 * 类说明:

 * 1.读取execl(只能读取.xls的execl,即只能读取03版的),如果是.xlsx类型的话 手工转化一下即可

 * 2.应用的jar包是apache的poi系类的jar包和

 * 3.ojdbc14.jar的数据库连接包。

 **/

public class ExeclOperateMain {

 

    /**

     * 方法说明:

     * @param args

     * @return void

     */

    public static void main(String[] args) throws Exception {

        ExeclOperateMain e=new ExeclOperateMain();

        e.getExcel();

        System.out.println("Execl导入完成!");

    }

 

 

     

    /**

     * 用于连接oracle数据库的方法

     * 只需修改中的参数getConnection("url","用户名","密码");

     */

    public Connection conn(){

        try {

        //第一步:加载JDBC驱动

        Class.forName("oracle.jdbc.driver.OracleDriver");

        //第二步:创建数据库连接

        Connection con =DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.202:1521:orcl", "mdb", "mdb");

        return con;

        }catch(ClassNotFoundException cnf){

          System.out.println("驱动未找到!:"+cnf);

          return null;

        }catch(SQLException sqle){

          System.out.println("不能连接数据库:"+sqle);

          return null;

        }

          catch (Exception e) {

        System.out.println("加载失败 JDBC/ODBC driver.");

        return null;

        }

    }

    /**

     * excel表  的读取

     * @throws Exception

     */

    public void getExcel() throws Exception {

        // 创建对Excel工作簿文件的引用    

        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("E:\\Workspaces\\pt-zt-v1.0\\src\\test.xls"));    

         // 创建对工作表的引用。

         // 在Excel文档中,第一张工作表的缺省索引是0,

         // 读取左上端单元

         for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet,3列

                 if(workbook.getSheetAt(i).getPhysicalNumberOfRows()>0){//行,6行

                     HSSFSheet childSheet = workbook.getSheetAt(i);

                        for(int rowi=1;rowi<40000;rowi++){

                            // || childSheet.getRow(rowi).getCell(0).equals("") || childSheet.getRow(rowi).getCell(0)==null

                            if(childSheet.getRow(rowi)==null) break;

                            String cell1=this.publicExcel(childSheet.getRow(rowi).getCell(0));

                            if(cell1==null) break;

                            //对于double类型的数据装换为string类型进行字符串截取 只取整数。

                            cell1=cell1.substring(0, cell1.length()-2);//2

                            String cell2=this.publicExcel(childSheet.getRow(rowi).getCell(1));//111

                            String cell3=this.publicExcel(childSheet.getRow(rowi).getCell(2));//422

                            //拼装插入数据库的sql  

                            String insert="insert into pt_zt values(&#39;"+cell3+"&#39;,&#39;"+cell2+"&#39;,&#39;"+cell1+"&#39;)";

                            System.out.println("SQL:"+insert);

                            insert(insert);

                             

                        }  

                }

            }

    }

 

        /**

         * execl数据格式的转换

         * @param cell

         * @return String

         */

        public String publicExcel( HSSFCell cell){

            String value = null;       

                  switch (cell.getCellType()) {

                  case HSSFCell.CELL_TYPE_NUMERIC:

                   value = "" + cell.getNumericCellValue();

                   break;

                  case HSSFCell.CELL_TYPE_STRING:

                   value = cell.getStringCellValue();

                   break;

                  case HSSFCell.CELL_TYPE_BLANK:

                   ;

                   break;

                  default:

                }

            return value;

        }

         

         

        /**

         * 插入数据 只需要传入插入sql即可

         * 插入sql的样例:insert into pt_zt values(&#39;sys_users&#39;,&#39;用户表&#39;);

         * @param insert 插入语句

         * @return int

         * @throws SQLException

         */

        public int insert(String insert) throws SQLException{

            Connection conn = this.conn();

            int re = 0;

            try{

                conn.setAutoCommit(false);//事物开始               

                Statement sm = conn.createStatement();

                re = sm.executeUpdate(insert);//执行

                if(re < 0){               //插入失败

                    conn.rollback();      //回滚

                    sm.close();         //关闭sm                                     

                    conn.close();    //关闭连接

                    return re;

                }

                conn.commit();            //插入提交正常

                System.out.println("一条已插入正常,sql="+insert);

                sm.close();

                conn.close(); 

                return re;

            }

            catch(Exception e){

                e.printStackTrace();

            }

            conn.close(); 

            return 0;

             

        }

 

}

Copy after login

                   

                   

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template