CoT
CoT

Reputation: 157

display csv file on excel

The following code take all data from database and write it in SDCard as CSV format. my problem is when i open the file in Excel it write the data in this format

Example

254868,2356489,4587963,787954,7895487,452369,4546545

but i want the result to be like this:

254868
2356489
4587963
7879544
7895487
4523697
4546545

Here is the code

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


                        File file= new File(fileDir, "itisfun.csv");  
                        FileWriter filewriter = new FileWriter(file);  
                        BufferedWriter out = new BufferedWriter(filewriter);  
                        out.write( dbUser.getMeterNUmber2(dd,sp).toString()+"\n" ); 
                        //out.write( "\"" );


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

Upvotes: 0

Views: 635

Answers (2)

Adrian G
Adrian G

Reputation: 785

Check in the getMeterNUmber2 function whether it writes comma or newline after each record. If you use comma then excel is correct to put them all on one line, in columns. If you use newline then one should be in each row like you want.

Upvotes: 0

Nimish Choudhary
Nimish Choudhary

Reputation: 2088

try

String[] div = dbUser.getMeterNUmber2(dd,sp).toString().split(',');
for (int i =0; i<div.length;i++)
out.write( div[i] +"\n" );

I assume

dbUser.getMeterNUmber2(dd,sp).toString() 

returns

254868,2356489,4587963,787954,7895487,452369,4546545

Upvotes: 1

Related Questions