简体   繁体   English

使用 openCSV 导出数据时是否可以删除分隔符?

[英]Is removing separator when exporting data with openCSV possible?

In my app i export data from database table to .csv file with opencsv.在我的应用程序中,我使用 opencsv 将数据从数据库表导出到 .csv 文件。 It works nicely.它工作得很好。 BUT customer need those written lines without the separator comma.但是客户需要那些没有分隔符逗号的书面行。 Is this possible?这可能吗?

I've thought and tried changing separator with CSVParserWriter.DEFAULT_SEPARATOR = '';我想过并尝试使用CSVParserWriter.DEFAULT_SEPARATOR = '';更改分隔符but char iterator cannot be empty.但字符迭代器不能为空。

Below is my exportData method下面是我的 exportData 方法

private void exportData() {

        DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
        File exportDir = new File(Environment.getExternalStorageDirectory(), "kerailymestari_tiedostot");
        if (!exportDir.exists())
        {
            exportDir.mkdirs();
        }

        File file = new File(exportDir, kerailyNimi+".csv");
        try
        {
            file.createNewFile();
            CSVWriter csvWrite = new CSVWriter(new FileWriter(file), CSVParserWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
            SQLiteDatabase db = dbhelper.getReadableDatabase();
            Cursor curCSV = db.rawQuery("SELECT * FROM tuotteet WHERE KID = " + kid,null);
            //csvWrite.writeNext(curCSV.getColumnNames());
            while(curCSV.moveToNext())
            {
                //Which column you want to exprort
                String arrStr[] ={curCSV.getString(1),curCSV.getString(2), curCSV.getString(3), curCSV.getString(4)};
                csvWrite.writeNext(arrStr);
            }
            csvWrite.close();
            curCSV.close();
        }
        catch(Exception sqlEx)
        {
            Log.e("Results", sqlEx.getMessage(), sqlEx);
        }
    }

That code needs to be changed so that separator would be remove or changed.该代码需要更改,以便删除或更改分隔符。 Or are there better solutions?或者有更好的解决方案吗?

I don't see why you'd keep using openCsv then.我不明白你为什么要继续使用 openCsv。 But if you want to do it further, just concat the strings from the columns on each other and create an array with the length 1. Something like below should work:但是,如果您想进一步操作,只需将列中的字符串相互连接并创建一个长度为 1 的数组。类似下面的内容应该可以工作:

String arrStr[] ={curCSV.getString(1) + curCSV.getString(2) + curCSV.getString(3) +  curCSV.getString(4)};
            csvWrite.writeNext(arrStr);

Note the concatenation using + operator instead of your original , .请注意使用+运算符而不是原始,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM