设为首页 收藏本站
查看: 726|回复: 0

[经验分享] (java入门)用apache.poj读写Excel文件的例子(1)

[复制链接]

尚未签到

发表于 2017-1-13 07:43:26 | 显示全部楼层 |阅读模式
  本例用到以下Class,完成对Excel的基本读写。我用的Excel是2003
  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;

package net.tianyu.study.poi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 String KEY_MARK         = "key";
private static final String END_MARK         = "end";
private static final String UPDATE_DATA_MARK = "Updated";
private static final int    MAX_LINES        = 3000;
private HSSFWorkbook        wb;
private HSSFSheet           sheet;
private List<Integer>       keyColumns       = new ArrayList<Integer>();
private List<KeyData>       orgKeyList       = new ArrayList<KeyData>();
private int                 dataStartRow;
private int                 dataEndColumn;
private FileOutputStream    out              = null;
private FileInputStream     in               = null;
private short               updateColor      = IndexedColors.LIGHT_YELLOW.getIndex();
private short               insertColor      = IndexedColors.LIGHT_GREEN.getIndex();
private short               deleteColor      = IndexedColors.GREY_25_PERCENT.getIndex();
public void open(String inputFileName, String outputFileName) throws IOException {
in = new FileInputStream(inputFileName);
out = new FileOutputStream(outputFileName);
POIFSFileSystem filein = new POIFSFileSystem(in);
wb = new HSSFWorkbook(filein);
sheet = wb.getSheetAt(0);
}
public void run() throws IOException {
initKeyColumn();
initOrgKeyList();
initDataStartRow();
startDiff();
format();
wb.write(out);
}
public void close() throws IOException {
out.close();
in.close();
}
private void initKeyColumn() {
HSSFRow markRow = sheet.getRow(0);
for (int i = 0; i < MAX_LINES; i++) {
HSSFCell cell = markRow.getCell(i);
if (cell != null && KEY_MARK.equals(cell.getStringCellValue())) {
keyColumns.add(i);
System.out.println(i);
}
if (cell != null && END_MARK.equals(cell.getStringCellValue())) {
dataEndColumn = i;
System.out.println("dataEndColumn :" + dataEndColumn);
break;
}
}
}
private void initOrgKeyList() {
for (int i = 2; i < MAX_LINES; i++) {
HSSFRow dataRow = sheet.getRow(i);
if (dataRow == null) {
break;
}
KeyData key = new KeyData();
key.setRow(i);
for (Integer keyColumn : keyColumns) {
HSSFCell cell = dataRow.getCell(keyColumn);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
key.addValue(cell.getStringCellValue());
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
key.addValue(Double.toString(cell.getNumericCellValue()));
}
}
}
System.out.println(key.toString());
orgKeyList.add(key);
}
}
private void format() {
for (int i = 0; i < dataEndColumn; i++) {
sheet.autoSizeColumn(i);
int width = sheet.getColumnWidth(i);
if (width > 256 * 100) {
sheet.setColumnWidth(i, 256 * 100);
}
}
}
private void initDataStartRow() {
for (int i = 1; i < MAX_LINES; i++) {
HSSFRow dataRow = sheet.getRow(i);
if (dataRow == null) {
continue;
}
HSSFCell cell = dataRow.getCell(0);
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING
&& UPDATE_DATA_MARK.equals(cell.getStringCellValue())) {
dataStartRow = i + 2;
break;
}
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING
&& END_MARK.equals(cell.getStringCellValue())) {
break;
}
}
System.out.println(dataStartRow);
}
private int matchKeyRow(KeyData key) {
for (KeyData orgKey : orgKeyList) {
if (orgKey.equals(key)) {
orgKey.setHasSameData(true);
return orgKey.getRow();
}
}
return 0;
}
private boolean isSameData(int orgRow, int targetRow) {
boolean result = true;
for (int i = 0; i < dataEndColumn; i++) {
HSSFRow orgData = sheet.getRow(orgRow);
HSSFRow targetData = sheet.getRow(targetRow);
if (orgData == null || targetData == null) {
result = false;
}
HSSFCell orgCell = orgData.getCell(i);
HSSFCell targetCell = targetData.getCell(i);
if (orgCell == null && targetCell == null) {
continue;
}
if (orgCell == null && targetCell != null) {
setCellColor(orgCell, updateColor);
setCellColor(targetCell, updateColor);
result = false;
}
if (targetCell == null && orgCell != null) {
setCellColor(orgCell, updateColor);
setCellColor(targetCell, updateColor);
result = false;
}
if (orgCell.getCellType() != targetCell.getCellType()) {
setCellColor(orgCell, updateColor);
setCellColor(targetCell, updateColor);
result = false;
}
if (orgCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
if (!orgCell.getStringCellValue().equals(targetCell.getStringCellValue())) {
setCellColor(orgCell, updateColor);
setCellColor(targetCell, updateColor);
result = false;
}
}
if (orgCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (orgCell.getNumericCellValue() != targetCell.getNumericCellValue()) {
setCellColor(orgCell, updateColor);
setCellColor(targetCell, updateColor);
result = false;
}
}
}
return result;
}
private void startDiff() {
for (int i = dataStartRow; i < MAX_LINES; i++) {
HSSFRow dataRow = sheet.getRow(i);
if (dataRow == null) {
break;
}
HSSFCell cell = dataRow.getCell(0);
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING
&& END_MARK.equals(cell.getStringCellValue())) {
break;
}
KeyData key = new KeyData();
key.setRow(i);
for (Integer keyColumn : keyColumns) {
cell = dataRow.getCell(keyColumn);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
key.addValue(cell.getStringCellValue());
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
key.addValue(Double.toString(cell.getNumericCellValue()));
}
}
}
int orgRow = matchKeyRow(key);
if (orgRow != 0) {
System.out.println("updateRow : " + i + " orgRow : " + orgRow + " " + isSameData(i, orgRow));
}
else {
setRowColor(i, insertColor);
}
}
setDeleteColor();
}
private void setDeleteColor() {
for (KeyData orgKey : orgKeyList) {
if (!orgKey.isHasSameData()) {
setRowColor(orgKey.getRow(), deleteColor);
}
}
}
private void setRowColor(int row, short color) {
HSSFRow dataRow = sheet.getRow(row);
for (int i = 0; i < dataEndColumn; i++) {
HSSFCell cell = dataRow.getCell(i);
if (cell == null) {
cell = dataRow.createCell(i);
}
setCellColor(cell, color);
}
}
private void setCellColor(HSSFCell cell, short color) {
CellStyle style = wb.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);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-327649-1-1.html 上篇帖子: 牛人写的 rhel 下 apache + tomat集群文章,经过配置成功 下篇帖子: 主流服务器apache、IIS、tomcat、jboss、resin、weblogic、websphere的区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表