zoukankan      html  css  js  c++  java
  • 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(); } } }
  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/mapingchuan/p/7736910.html
Copyright © 2011-2022 走看看