diff --git a/com.connor.jd.plm/.settings/org.eclipse.core.resources.prefs b/com.connor.jd.plm/.settings/org.eclipse.core.resources.prefs index 2c04b7f..0b79718 100644 --- a/com.connor.jd.plm/.settings/org.eclipse.core.resources.prefs +++ b/com.connor.jd.plm/.settings/org.eclipse.core.resources.prefs @@ -1,2 +1,3 @@ eclipse.preferences.version=1 encoding//src/com/connor/jd/plm/handlers/Wf001ScheduleHandler.java=UTF-8 +encoding//src/com/connor/jd/plm/utils/JDMethodUtil.java=UTF-8 diff --git a/com.connor.jd.plm/src/com/connor/jd/operations/ExcelImportServiceImpl.java b/com.connor.jd.plm/src/com/connor/jd/operations/ExcelImportServiceImpl.java new file mode 100644 index 0000000..3fb6ec0 --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/operations/ExcelImportServiceImpl.java @@ -0,0 +1,203 @@ +package com.connor.jd.operations; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +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; + +public class ExcelImportServiceImpl { + + public String importExcel(InputStream inputStream, String fileName) throws Exception { + + String message = "Import success"; + + boolean isE2007 = false; + // 判断是否是excel2007格式 + if (fileName.endsWith("xlsx")) { + isE2007 = true; + } + + int rowIndex = 0; + try { + InputStream input = inputStream; // 建立输入流 + Workbook wb; + // 根据文件格式(2003或者2007)来初始化 + if (isE2007) { + wb = new XSSFWorkbook(input); + } else { + wb = new HSSFWorkbook(input); + } + Sheet sheet = wb.getSheetAt(0); // 获得第一个表单 + int rowCount = sheet.getLastRowNum() + 1; + + for (int i = 1; i < rowCount; i++) { + rowIndex = i; + Row row; + + for (int j = 0; j < 26; j++) { + if (isMergedRegion(sheet, i, j)) { + System.out.print(getMergedRegionValue(sheet, i, j) + "\t"); + } else { + row = sheet.getRow(i); + System.out.print(row.getCell(j) + "\t"); + } + } + System.out.print("\n"); + } + } catch (Exception ex) { + message = "Import failed, please check the data in " + rowIndex + " rows "; + } + return message; + } + + /** + * 获取单元格的值 + * + * @param cell + * @return + */ + public String getCellValue(Cell cell) { + if (cell == null) + return ""; + return cell.getStringCellValue(); + } + + /** + * 合并单元格处理,获取合并行 + * + * @param sheet + * @return List + */ + public List getCombineCell(Sheet sheet) { + List 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; + } + + private int getRowNum(List listCombineCell, Cell cell, Sheet sheet) { + int xr = 0; + int firstC = 0; + int lastC = 0; + int firstR = 0; + int lastR = 0; + for (CellRangeAddress ca : listCombineCell) { + // 获得合并单元格的起始行, 结束行, 起始列, 结束列 + firstC = ca.getFirstColumn(); + lastC = ca.getLastColumn(); + firstR = ca.getFirstRow(); + lastR = ca.getLastRow(); + if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { + if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { + xr = lastR; + } + } + + } + return xr; + + } + + /** + * 判断单元格是否为合并单元格,是的话则将单元格的值返回 + * + * @param listCombineCell 存放合并单元格的list + * @param cell 需要判断的单元格 + * @param sheet sheet + * @return + */ + public String isCombineCell(List listCombineCell, Cell cell, Sheet sheet) throws Exception { + int firstC = 0; + int lastC = 0; + int firstR = 0; + int lastR = 0; + String cellValue = null; + for (CellRangeAddress ca : listCombineCell) { + // 获得合并单元格的起始行, 结束行, 起始列, 结束列 + firstC = ca.getFirstColumn(); + lastC = ca.getLastColumn(); + firstR = ca.getFirstRow(); + lastR = ca.getLastRow(); + if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { + if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { + Row fRow = sheet.getRow(firstR); + Cell fCell = fRow.getCell(firstC); + cellValue = getCellValue(fCell); + break; + } + } else { + cellValue = ""; + } + } + return cellValue; + } + + /** + * 获取合并单元格的值 + * + * @param sheet + * @param row + * @param column + * @return + */ + public String getMergedRegionValue(Sheet sheet, int row, int column) { + int sheetMergeCount = sheet.getNumMergedRegions(); + + for (int i = 0; i < sheetMergeCount; i++) { + CellRangeAddress ca = sheet.getMergedRegion(i); + int firstColumn = ca.getFirstColumn(); + int lastColumn = ca.getLastColumn(); + int firstRow = ca.getFirstRow(); + int lastRow = ca.getLastRow(); + + if (row >= firstRow && row <= lastRow) { + if (column >= firstColumn && column <= lastColumn) { + Row fRow = sheet.getRow(firstRow); + Cell fCell = fRow.getCell(firstColumn); + return getCellValue(fCell); + } + } + } + + return null; + } + + /** + * 判断指定的单元格是否是合并单元格 + * + * @param sheet + * @param row 行下标 + * @param column 列下标 + * @return + */ + private boolean isMergedRegion(Sheet sheet, int row, int column) { + int sheetMergeCount = sheet.getNumMergedRegions(); + for (int i = 0; i < sheetMergeCount; i++) { + CellRangeAddress range = sheet.getMergedRegion(i); + int firstColumn = range.getFirstColumn(); + int lastColumn = range.getLastColumn(); + int firstRow = range.getFirstRow(); + int lastRow = range.getLastRow(); + if (row >= firstRow && row <= lastRow) { + if (column >= firstColumn && column <= lastColumn) { + return true; + } + } + } + return false; + } + +} \ No newline at end of file diff --git a/com.connor.jd.plm/src/com/connor/jd/operations/ExcelOperation.java b/com.connor.jd.plm/src/com/connor/jd/operations/ExcelOperation.java new file mode 100644 index 0000000..8252a53 --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/operations/ExcelOperation.java @@ -0,0 +1,264 @@ +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> jfw) { + if (jfw.size() == 1) { + writeoneExcel(file, outpath, jfw); + } else { + writeallExcel(file, outpath, jfw); + } + } + + public static void writeallExcel(File file, String outpath, Map> 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 cellRangeList = getCombineCell(sheet); + for (Entry> ever : jfw.entrySet()) { + List 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> 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 cellRangeList = getCombineCell(sheet); + for (Entry> ever : jfw.entrySet()) { + List 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 valueList = new ArrayList(); + 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 + */ + public static List getCombineCell(Sheet sheet) { + List 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; + } +} \ No newline at end of file diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java b/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java index e693586..d59c99a 100644 --- a/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java +++ b/com.connor.jd.plm/src/com/connor/jd/plm/dialogs/CSXWHDialog.java @@ -1,5 +1,39 @@ package com.connor.jd.plm.dialogs; -public class CSXWHDialog { +import javax.swing.JPanel; -} +import com.teamcenter.rac.aif.AbstractAIFApplication; +import com.teamcenter.rac.kernel.TCException; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; + +public class CSXWHDialog extends Application { + + private AbstractAIFApplication app; + + public CSXWHDialog() throws TCException { + + } + + private JPanel Panel; + private Object[] obj = new Object[] { "序号", "产品类型", "试验项目", "试验时间", "样品数量", "试验费用" };// 民用表 + private Object[] obj2 = new Object[] { "序号", "类别", "项目", "项目费用" };// 工业表 + + public void initUI() { + + } + + @Override + public void start(Stage arg0) throws Exception { + Stage primaryStage = new Stage(); + Pane root = new Pane(); + Scene scene = new Scene(root, 382, 178); + primaryStage.setScene(scene); + primaryStage.setTitle("测试项维护"); + primaryStage.show(); + + } +} \ No newline at end of file diff --git a/com.connor.jd.plm/src/com/connor/jd/plm/utils/JDMethodUtil.java b/com.connor.jd.plm/src/com/connor/jd/plm/utils/JDMethodUtil.java new file mode 100644 index 0000000..f7b63ad --- /dev/null +++ b/com.connor.jd.plm/src/com/connor/jd/plm/utils/JDMethodUtil.java @@ -0,0 +1,47 @@ +package com.connor.jd.plm.utils; + +import com.teamcenter.rac.kernel.TCPreferenceService; +import com.teamcenter.rac.kernel.TCSession; +import com.teamcenter.rac.kernel.TCUserService; + +public class JDMethodUtil { + + public static TCPreferenceService service; + public static TCUserService userservice; + + /** + * + * + * @param prefName + * @return + */ + public static String getPrefStr(String prefName, TCSession session) { + if (service == null) { + service = session.getPreferenceService(); + } + String str = service.getString(TCPreferenceService.TC_preference_site, prefName); + if (str == null) { + str = new String(""); + } + return str; + } + + /** + * + * + * @param prefName + * @return + */ + public static String[] getPrefStrArray(String prefName, TCSession session) { + if (service == null) { + service = session.getPreferenceService(); + } + String[] strs = service.getStringArray(TCPreferenceService.TC_preference_site, prefName); + service.getStringValues(prefName); + service.getStringValue(prefName); + if (strs == null) { + strs = new String[] { "" }; + } + return strs; + } +}