您的位置:首页 > 数据库 > Oracle

利用java导入导出excel到oracle数据库

2017-12-27 20:17 471 查看


原地址:http://jingyan.baidu.com/article/1709ad80a975fe4634c4f0ae.html


方法/步骤

1

用到的JAR包如下(可以直接到POI官网上下载也可以在文章的附件中下载):

poi-3.9-20121203.jar

poi-ooxml-3.9-20121203.jar

poi-ooxml-schemas-3.9-20121203.jar

xmlbeans-2.3.0.jar

可能有冲突的JAR包,如果工程lib中存在,需要删除。

xbean-2.1.0.jar

具体代码如下:

Java代码 

package com.yusj; 

 

import java.io.FileInputStream; 

import java.io.FileNotFoundException; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import java.io.OutputStream; 

 

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

import org.apache.poi.ss.usermodel.Row; 

import org.apache.poi.ss.usermodel.Sheet; 

import org.apache.poi.ss.usermodel.Workbook; 

import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

 

/**

* 导入和导出Excel文件类

* 支持2003(xls)和2007(xlsx)版本的Excel文件



* @author yxm

*/ 

public class OperationExcelForPOI { 

 

    public static void main(String[] args) { 

        // 文件所在路径 

        String execelFile = "C:/Book2007.xlsx" ; 

        //String execelFile = "C:/Book2003.xls" ; 

        // 导入Excel 

        new OperationExcelForPOI().impExcel(execelFile) ; 

        // 导出Excel 

        String expFilePath = "C:/testBook.xls" ; 

        new OperationExcelForPOI().expExcel(expFilePath); 

    } 

     

    /**

     * 导入Excel

     * @param execelFile

     */ 

    public void impExcel(String execelFile){ 

        try { 

            // 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区) 

            Workbook book = null; 

            try { 

                // Excel 2007获取方法 

                book = new XSSFWorkbook(new FileInputStream(execelFile)); 

            } catch (Exception ex) { 

                // Excel 2003获取方法 

                book = new HSSFWorkbook(new FileInputStream(execelFile)); 

            } 

             

            // 读取表格的第一个sheet页 

            Sheet sheet = book.getSheetAt(0); 

            // 定义 row、cell 

            Row row; 

            String cell; 

            // 总共有多少行,从0开始 

            int totalRows = sheet.getLastRowNum() ; 

            // 循环输出表格中的内容,首先循环取出行,再根据行循环取出列 

            for (int i = 1; i <= totalRows; i++) { 

                row = sheet.getRow(i); 

                // 处理空行 

                if(row == null){ 

                    continue ; 

                } 

                // 总共有多少列,从0开始 

                int totalCells = row.getLastCellNum() ; 

for (int j = row.getFirstCellNum(); j < totalCells; j++) {

                    // 处理空列 

                    if(row.getCell(j) == null){ 

                        continue ; 

                    } 

                    // 通过 row.getCell(j).toString() 获取单元格内容 

                    cell = row.getCell(j).toString(); 

                    System.out.print(cell + "\t"); 

                } 

                System.out.println(""); 

            } 

        } catch (FileNotFoundException e) { 

            e.printStackTrace(); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } 

    } 

     

    public void expExcel(String expFilePath){ 

        OutputStream os = null ; 

        Workbook book = null; 

        try { 

            // 输出流 

            os = new FileOutputStream(expFilePath); 

            // 创建工作区(97-2003) 

            book = new HSSFWorkbook(); 

            // 创建第一个sheet页 

            Sheet sheet= book.createSheet("test"); 

            // 生成第一行 

            Row row = sheet.createRow(0); 

            // 给第一行的第一列赋值 

            row.createCell(0).setCellValue("column1"); 

            // 给第一行的第二列赋值 

            row.createCell(1).setCellValue("column2"); 

            // 写文件 

            book.write(os); 

             

        } catch (FileNotFoundException e) { 

            e.printStackTrace(); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } finally { 

            // 关闭输出流 

            try { 

                os.close(); 

            } catch (IOException e) { 

                e.printStackTrace(); 

            } 

        } 

         

    } 



如果读出的数字为科学计数法,将Excel的单元格属性设置由"常规"->"文本"或其他试试.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: