본문 바로가기
└ JAVA

[JAVA] 엑셀을 이용해 자동으로 프로퍼티(properties) 파일 생성

by 짜장이누나 2014. 6. 25.



 다국어 작업 중 엑셀에 키값과 한, 중, 일어를 정리하고 그것을 프로퍼티 파일에 다시 정리하려니 음.... 손이 많이간다.

뭔가 한번에 정리해주는 프로그램을 만들어 주셨다. 누가???????????????? 사수께서!!!!!!!!!!!!!!!!!!!!




>> 소스보기

1. Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package 패키지명;
 
public class Main {
 
    public static void main(String[] args) {
 
        ReadSpreadSheet readSpreadSheet = new ReadSpreadSheet(
                "엑셀 파일 위치"01223);
 
        WriteProperties writeProperties = new WriteProperties(
                "프로퍼티 파일 위치");
        writeProperties.writeProperties(readSpreadSheet.readSpreadSheet());
    }
 
}
cs




2. Native2AscII.java

     * 이 파일은 어디선가 퍼온 소스입니다.

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
package 패키지명;
 
public class Native2Ascii {
 
    /**
     * 네이티브 문자열을 아스키코드 문자열로 변환한다.
     * 
     * @param str
     *            네이티브 문자열
     * @return 아스키코드 문자열
     */
    public static String native2Ascii(String str) {
 
        StringBuffer sb = new StringBuffer(str.length());
        sb.setLength(0);
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            sb.append(native2Ascii(c));
        }
        return (new String(sb));
    }
 
    /**
     * 하나의 네이티브 문자를 아스키코드 문자열로 변환한다.
     * 
     * @param charater
     *            네이티브 문자
     * @return 아스키코드 문자열 StringBuffer
     */
    private static StringBuffer native2Ascii(char charater) {
 
        StringBuffer sb = new StringBuffer();
        if (charater > 255) {
            sb.append("\\u");
            int lowByte = (charater >>> 8);
            sb.append(int2HexString(lowByte));
            int highByte = (charater & 0xFF);
            sb.append(int2HexString(highByte));
        } else {
            sb.append(charater);
        }
        return sb;
    }
 
    /**
     * 정수를 16진수 문자열로 변환한다.
     * 
     * @param code
     *            정수
     * @return 16진수 문자열
     */
    private static String int2HexString(int code) {
 
        String hexString = Integer.toHexString(code);
        if (hexString.length() == 1)
            hexString = "0" + hexString;
        return hexString;
    }
}
cs




3. ReadSpreadSheet.java

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
package 패키지명;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.usermodel.WorkbookFactory;
 
import KeyValuePair;
 
public class ReadSpreadSheet {
 
    /**
     * 소스 스프레드시트 파일 경로
     */
    private String sourceSpreadSheetPath;
 
    /**
     * 소스 시트 인덱스
     */
    private int sourceSheetIndex = 0;
 
    /**
     * 키 셀 인덱스
     */
    private int keyCellIndex = 0;
 
    /**
     * 값 셀 인덱스
     */
    private int valueCellIndex = 1;
 
    /**
     * 시작 행 인덱스
     */
    private int startRowIndex = 0;
 
    /**
     * 소스 스프레드시트 파일 경로만 설정하는 생성자
     * 
     * @param sourceSpreadSheetPath
     *            소스 스프레드시트 파일 경로
     */
    public ReadSpreadSheet(String sourceSpreadSheetPath) {
 
        super();
        this.sourceSpreadSheetPath = sourceSpreadSheetPath;
    }
 
    /**
     * 모든 옵션을 설정하는 생성자
     * 
     * @param sourceSpreadSheetPath
     *            소스 스프레드시트 파일 경로
     * @param sourceSheetIndex
     *            소스 시트 인덱스
     * @param keyCellIndex
     *            키 셀 인덱스
     * @param valueCellIndex
     *            값 셀 인덱스
     * @param startRowIndex
     *            시작 행 인덱스
     */
    public ReadSpreadSheet(String sourceSpreadSheetPath, int sourceSheetIndex, int keyCellIndex,
            int valueCellIndex, int startRowIndex) {
 
        super();
        this.sourceSpreadSheetPath = sourceSpreadSheetPath;
        this.sourceSheetIndex = sourceSheetIndex;
        this.keyCellIndex = keyCellIndex;
        this.valueCellIndex = valueCellIndex;
        this.startRowIndex = startRowIndex;
    }
 
    /**
     * 스프레드시트의 정보를 읽어들인다.
     * 
     * @return 스트레드시트 정보의 key-value 쌍 리스트
     */
    public List<KeyValuePair> readSpreadSheet() {
 
        List<KeyValuePair> keyValuePairs = new ArrayList<KeyValuePair>();
 
        try {
            Workbook workbook = WorkbookFactory.create(new File(sourceSpreadSheetPath));
            Sheet sheet = workbook.getSheetAt(sourceSheetIndex);
 
            int currentRow = startRowIndex;
            Row row = null;
            while (true) {
                System.out.println(currentRow);
                row = sheet.getRow(currentRow++);
 
                if (row == null)
                    break;
                
 
                Cell keyCell = row.getCell(keyCellIndex);
                Cell valueCell = row.getCell(valueCellIndex);
 
                if (keyCell == null || valueCell == null)
                    break;
 
                String key = keyCell.getStringCellValue();
                String value = valueCell.getStringCellValue();
 
                keyValuePairs.add(new KeyValuePair(key, value));
            }
 
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return keyValuePairs;
    }
}
cs




4. WriteProperties.java

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
package 패키지명;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List;
 
import 패키지명;
 
public class WriteProperties {
 
    /**
     * 대상 프로퍼티 파일 경로
     */
    private String targetPropertiesPath;
 
    /**
     * 기본 생성자
     * 
     * @param targetPropertiesPath
     *            대상 프로퍼티 파일 경로
     */
    public WriteProperties(String targetPropertiesPath) {
 
        super();
        this.targetPropertiesPath = targetPropertiesPath;
    }
 
    /**
     * 프로퍼티 파일을 작성한다.
     * 
     * @param keyValuePairs
     *            key-value 쌍 리스트
     */
    public void writeProperties(List<KeyValuePair> keyValuePairs) {
 
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(targetPropertiesPath),
                    Charset.forName("UTF-8"));
 
            for (KeyValuePair pair : keyValuePairs) {
                writer.write(pair.getKey() + " = " + Native2Ascii.native2Ascii(pair.getValue())
                        + "\n");
            }
 
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
}
cs




5. KeyValuePair.java

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
package  패키지명2;
 
public class KeyValuePair {
 
    /**
     * 키
     */
    private String key;
 
    /**
     * 값
     */
    private String value;
 
    /**
     * 생성자
     * 
     * @param key
     * @param value
     */
    public KeyValuePair(String key, String value) {
 
        super();
        this.key = key;
        this.value = value;
    }
 
    /**
     * 키를 반환한다.
     * 
     * @return 키
     */
    public String getKey() {
 
        return key;
    }
 
    /**
     * 키를 설정한다.
     * 
     * @param key
     *            키
     */
    public void setKey(String key) {
 
        this.key = key;
    }
 
    /**
     * 값을 반환한다.
     * 
     * @return 값
     */
    public String getValue() {
 
        return value;
    }
 
    /**
     * 값을 설정한다.
     * 
     * @param value
     *            값
     */
    public void setValue(String value) {
 
        this.value = value;
    }
 
}
cs




>> 실행화면

1. 엑셀작성



2. JAVA 실행



3. 프로퍼티 파일이 생성되었는지 확인!





T h e e n d.