4

im trying to return Data from Database and save it on a file as CSV in SDcard and attach the file and email it, all this works perfect and the Data should be like this on the Excel.

Example

COLUMN A...........COLUMN B

123123232..........John

964803400..........Smith

657484738..........Mike

my problem is when i open the Excel the Data is displayed like this

COLUMN A...........COLUMN B

123123232..........

964803400..........

657484738..........

John

Smith

Mike

Here is the code Names.Java

dbUser.open();

                File root=null;     
             try {  
                    // check for SDcard   
                    root = Environment.getExternalStorageDirectory();                   
                    Log.i(TAG,"path.." +root.getAbsolutePath());  

                    //check sdcard permission  
                    if (root.canWrite()){  
                        File fileDir = new File(root.getAbsolutePath()+"/fun/");  
                        fileDir.mkdirs();  

                        Log.d("PATH",fileDir.toString());

                        File file= new File(fileDir, "itisfun.csv");  
                        FileWriter filewriter = new FileWriter(file);  
                        BufferedWriter out = new BufferedWriter(filewriter); 
                        String[] div = dbUser.getVname_Mnumber(sharedUsername1,product).toString().split(",");

                        Log.d(sharedUsername1, product);

                        Log.d("VVVVVVVVVV", dbUser.getVname_Mnumber(sharedUsername1,product).toString());

                        for (int i =0; i<div.length;i++)
                        out.write( div[i] +"\n" );

                        out.close();  
                    }  
                } catch (IOException e) {  
                    Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());  
                }

Here is my DBadapter

public String getVname_Mnumber(String date , String Vname) throws SQLException {    

    String[] whereArgs = new String[]{date,Vname}; 

    Cursor mcursor = db.rawQuery("SELECT MeterNumber,VendorName FROM " + SCAN_TABLE + " WHERE  Date = ? AND VendorName = ? ",whereArgs);

    ArrayList<String> results = new ArrayList<String>();
    ArrayList<String> results1 = new ArrayList<String>();
    while (mcursor.moveToNext()) {
        results.add(mcursor.getString(0));
        results1.add(mcursor.getString(1));
    }

    return results + ","+ results1;

}

will appreciate your help

1 Answer 1

3

You're basically creating two ArrayLists - the first only contains column 0 from the Cursor and the second only contains column 1. You then return all of the column 0 results followed by a "," followed by all the results for column 1.

Try a StringBuilder for the whole thing. Example...

StringBuilder sb = new StringBuilder();
while (mcursor.moveToNext()) {
    sb.append(mcursor.getString(0) + "," + mcursor.getString(1) + "\n");
}
return sb.toString();
Sign up to request clarification or add additional context in comments.

5 Comments

i just want it to show this(45488556) on column A and show John on column B but your code is show on the same column?
Then you are not importing the CSV data correctly. The whole point about CSV is each line represents a data "record" and each column is separated by a comma. If you import the CSV file into Excel correctly, it will show exactly what you want.
Oh wait. Forget the split(...) part of your 'writer' code and just write the whole string returned by getVname_Mnumber(...) to your output file.
im lost. can you please edit my code so that i can see where im doing wrong?
Sorry I had to rush off and do something so didn't see your previous comment. Glad you've got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.