(java入门)用apache.poj读写Excel文件的例子(2)

本文介绍了一个Java程序,用于比较两个Excel文件(.xls格式)的内容差异,并将不同之处标记为蓝色。程序使用Apache POI库读取Excel文件,并逐行逐列地比较单元格数据,若发现不匹配则修改背景色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Excelファイル比較サンプル

package net.tianyu.study.poi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

public class DiffXls {

    private static final int   MAX_LINES  = 3000;
    private static final short DIFF_COLOR = IndexedColors.BLUE.getIndex();

    private HSSFWorkbook       wb1;
    private HSSFSheet          sheet1;
    private HSSFWorkbook       wb2;
    private HSSFSheet          sheet2;
    private FileInputStream    diff1In    = null;
    private FileInputStream    diff2In    = null;
    private FileOutputStream   out        = null;

    public void open(String input1FileName, String input2FileName, String outputFileName) throws IOException {
        diff1In = new FileInputStream(input1FileName);
        diff2In = new FileInputStream(input2FileName);
        out = new FileOutputStream(outputFileName);

        POIFSFileSystem filein = new POIFSFileSystem(diff1In);
        wb1 = new HSSFWorkbook(filein);

        filein = new POIFSFileSystem(diff2In);
        wb2 = new HSSFWorkbook(filein);
    }

    public void run() throws IOException {

        int countSheet = wb1.getNumberOfSheets();

        for (int k = 0; k < countSheet; k++) {
            sheet1 = wb1.getSheetAt(k);
            sheet2 = wb2.getSheetAt(k);
            for (int i = 4; i < MAX_LINES; i++) {
                HSSFRow row1 = sheet1.getRow(i);
                HSSFRow row2 = sheet2.getRow(i);
                if (row1 == null || row2 == null) {
                    continue;
                }
                for (int j = 3; j < MAX_LINES; j++) {
                    HSSFCell cell1 = row1.getCell(j);
                    HSSFCell cell2 = row2.getCell(j);
                    if (cell1 == null || cell2 == null) {
                        continue;
                    }

                    String value1 = getCellValue(cell1);
                    String value2 = getCellValue(cell2);

                    if (value1 == null && value2 == null) {
                        continue;
                    }
                    if (value1 == null && value2 != null) {
                        setCellColor(cell1, DIFF_COLOR);
                    }
                    else if (value1 != null && value2 == null) {
                        setCellColor(cell1, DIFF_COLOR);
                    }
                    else if (!value1.equals(value2)) {
                        setCellColor(cell1, DIFF_COLOR);
                    }
                }
            }
        }

        wb1.write(out);
    }

    public void close() throws IOException {
        out.close();
        diff1In.close();
    }

    private String getCellValue(HSSFCell cell) {
        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return cell.getStringCellValue();
        }
        else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            return Double.toString(cell.getNumericCellValue());
        }
        return null;
    }

    private void setCellColor(HSSFCell cell, short color) {
        CellStyle style = wb1.createCellStyle();
        style.cloneStyleFrom(cell.getCellStyle());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFillForegroundColor(color);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderRight(CellStyle.BORDER_THIN);
        cell.setCellStyle(style);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值