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);
}
}
分享到:
相关推荐
NULL 博文链接:https://dncsoft.iteye.com/blog/687122
02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ...
POJ1048,加强版的约瑟夫问题 难度中等
北大POJ1004-Financial Management 解题报告+AC代码
NULL 博文链接:https://128kj.iteye.com/blog/1705139
用java的biginteger实现的poj1001,比较简单的方法
只是poj上的一条题目,对于理解后缀数组很有帮助.poj3261
C语言写的,通过了.我第一个POJ通过的文件,纪念一下.POJ上对于格式要求还真是紧啊!
POJ 1328 java做!雷达问题!java版本!AC答案~
北大poj JAVA源码
poj平台有关数据结构题的Java源码 1159 1276 2406 2502 2509 2513 2533 2778 3176
poj.grids.cn题型汇总 Dp状态设计与方程总结 1.不完全状态记录 青蛙过河问题 <2>利用区间dp 2.背包类问题 <1> 0-1背包,经典问题 <2>无限背包,经典问题 判定性背包问题 带附属关系的背包问题 <5> + -1背包...
题目分类 目前网上最全的 PKU 的 网上所有的 分类总结 祝ACM 一路顺风
poj题目2775文件子目录源代码,递归经典题目,
...
北大POJ2002-Squares 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 2785 4 Values whose Sum is 0 测试数据 解题报告: http://blog.csdn.net/qq7366020/article/details/8623208
用Java代码实现POJ(PKU)上题2494!
这是我个人写的POJ上2314题的Java实现,希望对喜欢ACM的人有帮助