ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.vci.client.oq.ui;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
 
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 com.vci.client.LogonApplication;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.client.oq.QTClient;
import com.vci.client.oq.QTDClient;
import com.vci.corba.omd.qtm.QTInfo;
 
public class ImpQT {
 
    private String fileName;
    public ImpQT() {
    }
    public boolean impExcel(){
        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("打开");
        jf.removeChoosableFileFilter(jf.getFileFilter());
        jf.setFileFilter(new FileFilter() {
            public String getDescription() {
                return "xls";
            }
            public boolean accept(File f) {
                return ((f.isDirectory()) || (f.getName().endsWith(".xls")));
            }
        });
        int showOpenDialog = jf.showOpenDialog(LogonApplication.frame);
        if(showOpenDialog == JFileChooser.APPROVE_OPTION){
            File selectedFile = jf.getSelectedFile();
            fileName = selectedFile.getAbsolutePath();
            if(!fileName.endsWith(".xls")){
                fileName = fileName + ".xls";
            }
            FileInputStream fin = null;
            try {
                List<QTInfo> qts = new ArrayList<QTInfo>();
                fin = new FileInputStream(fileName);
                HSSFWorkbook workbook = new HSSFWorkbook(fin);
                HSSFSheet sheet = workbook.getSheetAt(0);
                HSSFRow titleRow = sheet.getRow(0);
                for(int i = 1; i<=sheet.getLastRowNum(); i++){
                    HSSFRow row = sheet.getRow(i);
                    QTInfo qt = this.parse2QT(row, titleRow);
                    if(qt != null){
                        qts.add(qt);
                    }
                }
                System.out.println(qts.size());
                for(QTInfo qt: qts){
                    boolean isExist = ServiceProvider.getOMDService().getQTDService().isExistsQT(qt.qtName);
                    if(!isExist){
                        boolean isSuc = ServiceProvider.getOMDService().getQTDService().saveQT(qt);
                    }
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(fin != null){
                    try{
                        fin.close();
                    }catch(Exception e){
                    }
                }
            }
        }
        return false;
    }
    private QTInfo parse2QT(HSSFRow row, HSSFRow titleRow) {
        QTInfo qt = null;
        File selFile = new File(fileName);
        String parentPath = selFile.getParent();
        for(int i=0; i< titleRow.getLastCellNum(); i++){
            String title = titleRow.getCell(i).getStringCellValue();
            /*int cellType = titleRow.getCell(i).getCellType();
            String value = "";
            if(cellType == HSSFCell.CELL_TYPE_NUMERIC){
                value = String.valueOf(row.getCell(i).getNumericCellValue());
            }else if(cellType == HSSFCell.CELL_TYPE_STRING){
                value = row.getCell(i).getStringCellValue();
            }*/
            String value = row.getCell(i).toString();
            if(value == null || value.trim().length() == 0){
                continue;
            }
            if(qt == null){
                qt = new QTInfo();
            }
            if(title.equals("模板名称")){
                qt.qtName = value.trim();
            }else if(title.equals("业务类型")){
                qt.btmName = value.trim();
            }else if(title.equals("查询模板级别")){
                qt.levelFlag = Short.valueOf(value.trim());
            }else if(title.equals("查询模板")){
                qt.qtText = readFile(parentPath+value.trim());
            }else if(title.equals("查询模板界面")){
                qt.qtUIText = readFile(parentPath+value.trim());
            }else if(title.equals("创建人")){
                qt.creator = value.trim();
            }else if(title.equals("创建时间")){
                qt.createTime = Long.valueOf(value.trim());
            }
        }
        return qt;
    }
    private String readFile(String filePath){
        FileInputStream fin = null;
        try {
            byte[] arr = new byte[1024];
            int len = 0;
            StringBuffer sbuf = new StringBuffer();
            fin = new FileInputStream(filePath);
            while((len =fin.read(arr)) >0){
                sbuf.append(new String(arr,0, len));
            }
            return sbuf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fin != null){
                try{
                    fin.close();
                }catch(Exception e){}
            }
        }
        return"";
    }
}