• 使用POI对EXCEL 读入写出


    一同事让我给他解析60万条数据的excel表格 剔除指定的数据  果断写了 等写了一半 他又不用了 ... 继续研究  由于是第一次使用  瞎弄弄

    所需jar包

    commons-codec-1.5.jar
    
    commons-logging-1.1.jar
    
    dom4j-1.6.1.jar
    
    junit-3.8.1.jar
    
    log4j-1.2.13.jar
    
    poi-3.9-20121203.jar
    
    poi-examples-3.9-20121203.jar
    
    poi-excelant-3.9-20121203.jar
    
    poi-ooxml-3.9-20121203.jar
    
    poi-scratchpad-3.9-20121203.jar
    
    stax-api-1.0.1.jar
    
    xmlbeans-2.3.0.jar
    package com.jokey;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class ExcelReaderAndWriter {
        
        private POIFSFileSystem fs;
        private  XSSFWorkbook wb;
        private  XSSFSheet sheet;
        private XSSFRow row;
        private XSSFCell cell;
        
        public static void main(String[] args) {
            try {
                // 对读取Excel表格标题测试
                
                
                ExcelReaderAndWriter excelReader = new ExcelReaderAndWriter();
                excelReader.reader();
                //excelReader.writer();
            } catch (FileNotFoundException e) {
                System.out.println("未找到指定路径的文件!");
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        private void reader() throws IOException{
            FileInputStream is = new FileInputStream("f:\\3.xlsX");
                wb = new XSSFWorkbook(is);
                sheet = wb.getSheetAt(0);
            sheet = wb.getSheetAt(0);
            //获取总行数
            int totalRowNum = sheet.getLastRowNum();
            //这里i<==
            for (int i = 0; i <= totalRowNum; i++) {
                row = sheet.getRow(i);
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    System.out.print(row.getCell(j));
                }
                System.out.println("");
            }
        }
        
        private void writer() throws IOException{
            FileOutputStream os = new FileOutputStream("f:\\5.xlsx");
            //创建工作表
            wb = new XSSFWorkbook();
            sheet = wb.createSheet();
            row = sheet.createRow(0);
            cell = row.createCell(0);
            cell.setCellValue("测试的啦");
            try {
                //写入
                wb.write(os);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    toString() 与 JSON.stringify()
    ajax+node实现图片上传
    scrollHeight与offsetHeight
    【CSS】纯css实现立体摆放图片效果
    【逻辑】赛出25匹马的前3名
    【js】数组去重时间复杂度为n的方法
    【css】css2实现两列三列布局的方法
    初始原型链(三)
    初始原型链(二)
    织梦网站后台管理网站栏目管理项不显示问题解决办法
  • 原文地址:https://www.cnblogs.com/cnjava/p/3041654.html
Copyright © 2020-2023  润新知