java对excel的操作简单

用的是Apache POI开放源码函式库,结合mysql数据库

接到通知需要将给出的一个表格清理出来,所要做的事情就是在把这张表格里的所有数据和本地的另一张表的所有数据对比,如果某条数据存在就做一个标记

下载poi.jar及相关包,配置好环境变量,安装数据库,下载数据库的java驱动配置相关环境变量

1.首先将参考表中的所有特征量导入数据库

import java.io.*;import org.apache.poi.hssf.usermodel.*;
import java.sql.*;

public class test2 {
    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password= "root";
        Connection conn = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        FileInputStream inp =null;
        HSSFWorkbook wb=null;
        try{
            inp= new FileInputStream("E:/test.xls");//打开文件
            wb = new HSSFWorkbook(inp);//创建HSSFWorkbook对象
            HSSFSheet sheet = wb.getSheetAt(0);//取得文件中的第1张表
            int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();//表中有效列
            int rowNum=sheet.getLastRowNum();//表中有效行
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,password);
            String sqlInset = "insert into xls(id) values(?)";
            ps= conn.prepareStatement(sqlInset);
            String temp=null;
            for(int i=0;i<=rowNum;i++) {
                HSSFRow row = sheet.getRow(i);//得到表中的i行
                HSSFCell cell = row.getCell(0);//得到表中i行0列的那个单元格
                if(cell.getCellType()!=1) {
                    cell.setCellType(1); //如果取得单元格中的数据不是字符串格式为了方便设置为可转为字符串格式的标志
                }
                temp=cell.getStringCellValue();//HSSFCell对象转换为字符串对象
                ps.setString(1, temp); 
                ps.executeUpdate();//更新到数据库中
            }
            ps.close();
            conn.close();
            wb.close();
            inp.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        
    }
}

2.打开所需要标记的表依次取出每一行数据和数据库中所有数据对比,存在则做好标记

import java.io.*;import org.apache.poi.hssf.usermodel.*;
import java.sql.*;
public class test3 { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password= "root"; Connection conn = null; PreparedStatement ps=null; ResultSet rs=null; FileInputStream inp =null; HSSFWorkbook wb=null; try{ inp= new FileInputStream("E:/test3.xls"); wb = new HSSFWorkbook(inp); HSSFSheet sheet = wb.getSheetAt(0); int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells(); int rowNum=sheet.getLastRowNum(); Class.forName(driver); conn = DriverManager.getConnection(url,user,password); String sqlInset = "select * from xls"; ps= conn.prepareStatement(sqlInset); rs = ps.executeQuery(); String temp=null; boolean flag=false; for(int i=0;i<=rowNum;i++) { HSSFRow row = sheet.getRow(i); HSSFCell cell = row.getCell(0); HSSFCell cell2 = row.getCell(1); if(cell.getCellType()!=1) { cell.setCellType(1); } temp=cell.getStringCellValue(); rs = ps.executeQuery(); while(rs.next()) { if(temp.equals(rs.getString(1))) { flag=true; System.out.println(temp); break; } } if(flag) { FileOutputStream oup= new FileOutputStream("E:/test3.xls");//写出流,让每次操作之后都写回磁盘 cell2.setCellType(1);//以字符串的方式写回 cell2.setCellValue("yes");//设置写回的值为yes wb.write(oup);//通过HSSFWrokbook提供的函数写回到磁盘 oup.close(); flag=false; } } rs.close(); ps.close(); conn.close(); wb.close(); inp.close(); }catch(Exception e) { e.printStackTrace(); } } }

 

posted @ 2017-10-26 15:07  叮叮猫写BUG  阅读(584)  评论(0编辑  收藏  举报