4 Replies - 3333 Views - Last Post: 27 July 2012 - 12:08 PM Rate Topic: -----

#1 Udoelerhard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-July 12

Formatting CSV file output

Posted 27 July 2012 - 09:37 AM

Hello everyone!

I am trying to write a CSV file that will be opened in Microsoft Excel. I am formatting the excel file by using \t which pushes the "pointer"? to the next rightward cell. The problem is that "\t" does not seem to be interpreted as a tab by Excel when the file format is .CSV. If I write the file out as a .xls file it works fine but there is an annoying "This file may be corrupt" message that pops up every time the file is opened.

So my question- is there a different way to write this out to where I can either get rid of the "This file may be corrupt" message OR still write the file as a CSV but use something in place of \t to push the pointer to the next cell?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Driver extends JFrame implements ActionListener
{
    JPanel panel = new JPanel();
    protected JTextField link;
    public Driver(){
        super("Dashboard Formatter"); setBounds(100,100,300,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        Container con = this.getContentPane();
        JButton open = new JButton("Open");

        open.addActionListener(this);
        link = new JTextField(20);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx=0;
        c.gridy=2;
        con.add(open, c);
        
        c.gridx=0;
        c.gridy=0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        con.add(link, c);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        readFile();
    }
    public void readFile(){
        try{
            ArrayList lines = new ArrayList();
            String strLine;
            int arraySize;
            String fpath = link.getText();
            FileInputStream fstream = new FileInputStream(fpath);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            
            while((strLine = br.readLine()) != null){
                lines.add(strLine);
            }
            in.close();
            fstream.close();
            br.close();
            for(int i=0;i<lines.size();i++){
            	String lineOut = lines.get(i).toString();
                String line = editMethod(lineOut);
                lines.set(i,line);
            }
            writeMethod(lines);
            openMethod();
        }
        catch(Exception e){
            System.err.println("Error1: " + e.getMessage());
        }
    }
    public String editMethod(String lineIn){
        String line = "testNull";
        String test = "testNull";
        String[] tmp;
        tmp = lineIn.split("~", -1);
        
        for(int i=0;i<tmp.length;i++){
        	if(tmp[i].indexOf(",--") == -1){
        		if(tmp[i].length() > 0){
            		if(line.equals(test)){
                		line = "\t" + tmp[i];
                	}
            		else{
            			line = line + "\t" + tmp[i];
            		}
            	}
        	}else if(tmp[i].length() > 0){
        		if(line.equals(test)){
            		line = tmp[i];
            	}
        		else{
        			line = line + "\t" + tmp[i];
        		}
        	}
        }
        return line;
    }
    public void writeMethod(ArrayList lines){
        try{
            File file = new File("dashboard.csv");
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            for(int i=0;i<lines.size();i++){ 
            	String lineWrite = lines.get(i).toString();
                bw.write(lineWrite);
                bw.newLine();
            }
            bw.close();
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
    public void openMethod(){
        try{
            java.awt.Desktop.getDesktop().open(new File("dashboard.csv"));  
            System.exit(0);
        }
        catch(Exception e){
            System.err.println("Error2: " + e.getMessage());
        }
    }
    public static void main(String[] args)
    {
        new Driver();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Formatting CSV file output

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5295
  • View blog
  • Posts: 14,133
  • Joined: 18-April 07

Re: Formatting CSV file output

Posted 27 July 2012 - 09:58 AM

Uhhh... CSV.... Comma Separated Values. Use commas that separate the values, not a darn tab. ;)

This post has been edited by Martyr2: 27 July 2012 - 09:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 Udoelerhard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-July 12

Re: Formatting CSV file output

Posted 27 July 2012 - 10:17 AM

View PostMartyr2, on 27 July 2012 - 09:58 AM, said:

Uhhh... CSV.... Comma Separated Values. Use commas that separate the values, not a darn tab. ;)

Uhhh... this is embarrassing :). Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11224
  • View blog
  • Posts: 19,242
  • Joined: 19-March 11

Re: Formatting CSV file output

Posted 27 July 2012 - 11:55 AM

Actually, these days it's safest to think of CSV as standing for "character-separated values" since nobody restricts the delimiter to commas. Excel will happily import tab-separated data, or data delimited by any other character you choose. Just use "import" instead of "open".
Was This Post Helpful? 2
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Formatting CSV file output

Posted 27 July 2012 - 12:08 PM

View Postjon.kiparsky, on 27 July 2012 - 02:55 PM, said:

Just use "import" instead of "open".

I knew there was a trick... can also be fixed size column if you want
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1