You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.connor.jd.operations;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.teamcenter.rac.kernel.TCComponent;
public class ExcelOperation {
private static final String XLS = "xls";
private static final String XLSX = "xlsx";
public static void writeExcel(File file, String outpath, Map<String, List<TCComponent>> jfw) {
if (jfw.size() == 1) {
writeoneExcel(file, outpath, jfw);
} else {
writeallExcel(file, outpath, jfw);
}
}
public static void writeallExcel(File file, String outpath, Map<String, List<TCComponent>> jfw) {
Workbook workbook;
try {
workbook = getworkbook(file);
System.out.println("ÒÑÓÐÎļþ");
Workbook newwb = getWorkbook(outpath);
Sheet newsheet = newwb.createSheet();
Sheet sheet = workbook.getSheetAt(0);
CellStyle style = workbook.createCellStyle();
Row row = sheet.getRow(0);
row.setRowStyle(style);
Row newrow0 = newsheet.createRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
// Cell newCell = newrow0.createCell(i);
// newCell.setCellValue(row.getCell(i).getStringCellValue());
}
List<CellRangeAddress> cellRangeList = getCombineCell(sheet);
for (Entry<String, List<TCComponent>> ever : jfw.entrySet()) {
List<TCComponent> itemList = ever.getValue();
String sfname = ever.getKey();
System.out.println("itemList³¤¶È" + itemList.size());
for (int i = 0; i < cellRangeList.size(); i++) {
CellRangeAddress ca = cellRangeList.get(i);
int firstColumn = 0;
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
String cellVelue = fCell.getStringCellValue();
System.out.println("ºÏ²¢µ¥Ôª¸ñµÄÄÚÈÝ" + cellVelue);
System.out.println("ÆðʼÐÐ" + firstRow + "×îºóÐÐ" + lastRow);
if (sfname.equals(cellVelue)) {
for (int z = firstRow; z <= lastRow; z++) {
Row newRow = sheet.getRow(z);
String value = newRow.getCell(1).getStringCellValue();
System.out.println("±í¸ñÄÚÈÝ" + value);
for (int p = 0; p < itemList.size(); p++) {
if (itemList.get(p).getTCProperty("object_type").getDisplayableValue().equals(value)) {
TCComponent[] revlist = itemList.get(p).getTCProperty("revision_list")
.getReferenceValueArray();
TCComponent component = null;
for (int revlength = 0; revlength < revlist.length; revlength++) {
String status = revlist[revlength].getProperty("release_status_list");
if (!status.equals("")) {
component = revlist[revlength];
}
}
String[] info = new String[4];
if (component != null) {
info[0] = component.getProperty("owning_group");
info[1] = component.getProperty("owning_user");
info[2] = component.getProperty("creation_date");
info[3] = component.getProperty("item_id");
} else {
info[0] = "";
info[1] = "";
info[2] = "";
info[3] = "";
}
for (int v = 0; v < info.length; v++) {
Cell cellvalue = newRow.createCell(v + 2);
// cellvalue.setCellValue(info[v]);
System.out.println("ÄÚÈÝÊÇ" + info[v]);
}
}
}
}
}
}
}
FileOutputStream fos = new FileOutputStream(outpath);
workbook.write(fos);
workbook.close();
fos.close();
} catch (Exception e) {
}
}
public static void writeoneExcel(File file, String outpath, Map<String, List<TCComponent>> jfw) {
Workbook workbook;
try {
workbook = getworkbook(file);
System.out.println("ÒÑÓÐÎļþ");
Workbook newwb = getWorkbook(outpath);
Sheet newsheet = newwb.createSheet();
Sheet sheet = workbook.getSheetAt(0);
CellStyle style = newwb.createCellStyle();
Row row = sheet.getRow(0);
Row newrow0 = newsheet.createRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell oldCell = row.getCell(i);
style.cloneStyleFrom(oldCell.getCellStyle());
Cell newCell = newrow0.createCell(i);
newCell.setCellStyle(style);
// newCell.setCellValue(oldCell.getStringCellValue());
}
List<CellRangeAddress> cellRangeList = getCombineCell(sheet);
for (Entry<String, List<TCComponent>> ever : jfw.entrySet()) {
List<TCComponent> itemList = ever.getValue();
String sfname = ever.getKey();
System.out.println("itemList³¤¶È" + itemList.size());
for (int i = 0; i < cellRangeList.size(); i++) {
CellRangeAddress ca = cellRangeList.get(i);
int firstColumn = 0;
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
String cellVelue = fCell.getStringCellValue();
System.out.println("ºÏ²¢µ¥Ôª¸ñµÄÄÚÈÝ" + cellVelue);
System.out.println("ÆðʼÐÐ" + firstRow + "×îºóÐÐ" + lastRow);
if (sfname.equals(cellVelue)) {
List<String> valueList = new ArrayList<String>();
for (int z = firstRow; z <= lastRow; z++) {
Row oldRow = sheet.getRow(z);
String value = oldRow.getCell(1).getStringCellValue();
valueList.add(value);
}
for (int j = 1; j <= lastRow - firstRow + 1; j++) {
Row newRow = newsheet.createRow(j);
Cell cell = newRow.createCell(0);
// cell.setCellValue(cellVelue);
Cell cell2 = newRow.createCell(1);
// cell2.setCellValue(valueList.get(j - 1));
System.out.println("ÌØÊâ±í¸ñÄÚÈÝ" + valueList.get(j - 1));
for (int p = 0; p < itemList.size(); p++) {
if (itemList.get(p).getTCProperty("object_type").getDisplayableValue()
.equals(valueList.get(j - 1))) {
System.out.println(
"ÀàÐÍ£º" + itemList.get(p).getTCProperty("object_type").getDisplayableValue());
TCComponent[] revlist = itemList.get(p).getTCProperty("revision_list")
.getReferenceValueArray();
TCComponent component = null;
for (int revlength = 0; revlength < revlist.length; revlength++) {
String status = revlist[revlength].getProperty("release_status_list");
if (!status.equals("")) {
component = revlist[revlength];
}
}
String[] info = new String[4];
if (component != null) {
info[0] = component.getProperty("owning_group");
info[1] = component.getProperty("owning_user");
info[2] = component.getProperty("creation_date");
info[3] = component.getProperty("item_id");
} else {
info[0] = "";
info[1] = "";
info[2] = "";
info[3] = "";
}
for (int v = 0; v < info.length; v++) {
System.out.println("¿ªÊ¼´´½¨ÄÚÈÝ");
Cell cellvalue = newRow.createCell(v + 2);
// cellvalue.setCellValue(info[v]);
System.out.println("ÄÚÈÝÊÇ" + info[v]);
}
}
}
}
CellRangeAddress region = new CellRangeAddress(1, lastRow - firstRow + 1, 0, 0);
newsheet.addMergedRegion(region);
}
}
}
FileOutputStream fos = new FileOutputStream(outpath);
newwb.write(fos);
newwb.close();
fos.close();
} catch (Exception e) {
}
}
/**
* ºÏ²¢µ¥Ôª¸ñ´¦Àí,»ñÈ¡ºÏ²¢ÐÐ
*
* @param sheet
* @return List<CellRangeAddress>
*/
public static List<CellRangeAddress> getCombineCell(Sheet sheet) {
List<CellRangeAddress> list = new ArrayList<>();
// »ñµÃÒ»¸ö sheet Öкϲ¢µ¥Ôª¸ñµÄÊýÁ¿
int sheetmergerCount = sheet.getNumMergedRegions();
// ±éÀúËùÓеĺϲ¢µ¥Ôª¸ñ
for (int i = 0; i < sheetmergerCount; i++) {
// »ñµÃºÏ²¢µ¥Ôª¸ñ±£´æ½ølistÖÐ
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
public static Workbook getWorkbook(String file) throws IOException {
Workbook workbook = null;
// FileInputStream in = new FileInputStream(file);
if (file.endsWith(XLS)) {
System.out.println("xls");
workbook = new HSSFWorkbook();
} else if (file.endsWith(XLSX)) {
System.out.println("xlsx");
workbook = new XSSFWorkbook();
} else {
System.out.println("¸ñʽ´íÎó");
}
return workbook;
}
public static Workbook getworkbook(File file) throws IOException {
Workbook workbook = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(XLS)) {
System.out.println("xls");
workbook = new HSSFWorkbook(in);
} else if (file.getName().endsWith(XLSX)) {
System.out.println("xlsx");
workbook = new XSSFWorkbook(in);
} else {
System.out.println("¸ñʽ´íÎó");
}
in.close();
return workbook;
}
}