๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

[Java]CSV ํŒŒ์ผ ์ƒ์„ฑ ํด๋ž˜์Šค ์˜ˆ์‹œ ๋ณธ๋ฌธ

๐Ÿ’ป Java Study/Practice

[Java]CSV ํŒŒ์ผ ์ƒ์„ฑ ํด๋ž˜์Šค ์˜ˆ์‹œ

jhj.sharon 2024. 6. 4. 14:06
๋ฐ˜์‘ํ˜•

| writeListToCSVFile ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํŒŒ์ผ์„ ์ƒ์„ฑ

- ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ์ œ๋ชฉํ–‰(header row)์™€ ๋‚ด์šฉํ–‰(data row)๋ฅผ ๊ฐ๊ฐ ๋ถ„๋ฆฌํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.

- fileName : ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•œ ํŒŒ์ผ์ด๋ฆ„(ex. /portal/upload/DATA_202305240323420290)

- headerlist : ์ œ๋ชฉํ–‰

- dataList : ๋‚ด์šฉํ–‰

- dataList ์ž์ฒด๋Š” ์ปฌ๋Ÿผ๊ณผ ๋ฐ์ดํ„ฐ๋กœ ์ง์ง€์–ด ์žˆ์ง€๋งŒ ์‹ค์ œ ํŒŒ์ผ ์ƒ์„ฑ์€ ๋ฐ์ดํ„ฐ๋งŒ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰ ์ œ๋ชฉํ–‰์€ ๋นจ๊ฐ•,  ํŒŒ๋ž‘, ๋…ธ๋ž‘์ด๊ณ  ๋ฐ์ดํ„ฐ 1, ๋ฐ์ดํ„ฐ2, ๋ฐ์ดํ„ฐ3์ด ์Œ“์ธ๋‹ค.

List<String> headers = Arrays.asList("๋นจ๊ฐ•", "ํŒŒ๋ž‘", "๋…ธ๋ž‘");
List<Map<String, String>> data = new ArrayList<>();
Map<String, String> row1 = new HashMap<>();
row1.put("์ปฌ๋Ÿผ1", "๋ฐ์ดํ„ฐ1");
row1.put("์ปฌ๋Ÿผ2", "๋ฐ์ดํ„ฐ2");
row1.put("์ปฌ๋Ÿผ3", "๋ฐ์ดํ„ฐ3");
data.add(row1);

DataCSVUtil.writeListToCSVFile("output.csv", headers, data);โ€‹
package chungnam.jobutil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;

public class DataCSVUtil {

    public static void writeListToCSVFile(String fileName, List<String> headerList, List<Map<String, String>> dataList) {
        Writer writer = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");

            // Write BOM for UTF-8
            writer.write('\uFEFF');

            // Header row
            for (int i = 0; i < headerList.size(); i++) {
                writer.append(escapeSpecialCharacters(headerList.get(i)));
                if (i < headerList.size() - 1) {
                    writer.append(",");
                }
            }
            writer.append("\n");

            // Write the data rows
            for (Map<String, String> data : dataList) {
                int cellIndex = 0;
                for (String key : data.keySet()) {
                    String value = (String) data.values().toArray()[cellIndex++];
                    writer.append(escapeSpecialCharacters(value != null ? value : ""));
                    if (cellIndex < data.size()) {
                        writer.append(",");
                    }
                }
                writer.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String escapeSpecialCharacters(String data) {
        if (data == null) {
            return "";
        }
        String escapedData = data.replace("\"", "\"\"");
        if (data.contains(",") || data.contains("\n") || data.contains("\"")) {
            escapedData = "\"" + escapedData + "\"";
        }
        return escapedData;
    }
}

 

 

๋ฐ˜์‘ํ˜•
Comments