zoukankan      html  css  js  c++  java
  • java操作Excel文件(Excel2003,Excel2007)

    我是用Apache提供的 POI  jar包(也有其他的方法有个jxl也不错,可以去看看),版本太低不支持Excel2007,所以用高点的版本,我用的是poi3.8网上很多自己搜下载

    Excel2003 和2007就是一个是HSSFWorkbook一个是XSSFWorkbook其他区别不大,这里主要讲Excel2007了

    1、创建Excel

        public void createExcel() throws Exception {
            final String writepath = "d://1.xlsx";
            Workbook createwb = new XSSFWorkbook();
            FileOutputStream output = new FileOutputStream(writepath);
            PlayExcel.createWorkbook(createwb, output);
        }

    2、读取Excel

    public void readExcel() throws IOException {
            final String readpath = "d://1.xlsx";
            FileInputStream input = new FileInputStream(readpath);
            Workbook readwb = new XSSFWorkbook(input);
            PlayExcel.readSheet(readwb);
        }

    3、增删改

     sheet.createRow(3).createCell(0).setCellValue("s");//新建一列(4)给第一行赋值s
     sheet.createRow(3).createCell(0).removeCellComment();//删除
    
     sheet.getRow(1).getCell(1).setCellValue("lol");
     sheet.getRow(1).getCell(2).setCellValue("lll");
     sheet.getRow(2).getCell(2).setCellValue("rere");
     FileOutputStream out = new FileOutputStream("d://1.xlsx");// 把修改后的表格重新写入"d://1.xlsx"
     xwb.write(out);
     out.close();


    4、获取行列数

    String sheetname = sheet.getSheetName().toLowerCase(); // 工作表名称
    int rows = sheet.getLastRowNum() + 1;// 获取工作表行数
    int cells=sheet.getRow(1).getLastCellNum(); //获取某一行的列数

    5、设置宽高

    sheet.setColumnWidth((short)column,(short)width);      
    row.setHeight((short)height);

    6、根据单元格不同属性返回不同字符串数值

    public String getCellStringValue(HSSFCell cell) {    
            String cellValue = "";    
            switch (cell.getCellType()) {    
            case HSSFCell.CELL_TYPE_STRING://字符串类型
                cellValue = cell.getStringCellValue();    
                if(cellValue.trim().equals("")||cellValue.trim().length()<=0)    
                    cellValue=" ";    
                break;    
            case HSSFCell.CELL_TYPE_NUMERIC: //数值类型
                cellValue = String.valueOf(cell.getNumericCellValue());    
                break;    
            case HSSFCell.CELL_TYPE_FORMULA: //公式
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);    
                cellValue = String.valueOf(cell.getNumericCellValue());    
                break;    
            case HSSFCell.CELL_TYPE_BLANK:    
                cellValue=" ";    
                break;    
            case HSSFCell.CELL_TYPE_BOOLEAN:    
                break;    
            case HSSFCell.CELL_TYPE_ERROR:    
                break;    
            default:    
                break;    
            }    
            return cellValue;    
        }   


     

    学习...
  • 相关阅读:
    系统学Python-01
    pandas(二)
    matplotlib
    Python数据分析-初识numpy、pandas、scipy、matplotlib和Scikit-Learn等数据处理库
    Python进行读取或写入等文件操作时的路径问题
    pandas(一)
    00 Python及第三方库的安装问题
    sorted ()函数和列表中的sort()函数
    git 获取branch名和commit id
    查看Android log 和TEE Log
  • 原文地址:https://www.cnblogs.com/istianyu/p/2906670.html
Copyright © 2011-2022 走看看