4 Replies Latest reply: Jul 9, 2009 3:58 PM by 843789 RSS

    output text to CSV

    843789
      i need some data to output to csv file so that user can open file in excel as well as notepad.
      i have some string which starts with "+" . e.g. +IHKUI
      when i open file in excel, it writes #NAME? instead of +IHKUI ?

      does any one know how to fix this error. ?

      sample program
      try {
                  FileWriter writer = new FileWriter("c:\\sample.csv");
                  //ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(out);
                  //ecsvp.writeln(new String[]{"hello", "\\+IBMAA"});
                  writer.write("hello");
                  writer.write(",");
                  writer.write("+HJUIK");
                  writer.flush();
                     writer.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
        • 1. Re: output text to CSV
          796447
          That seems to be an Excel question, not a Java one. You need to ask someone about Excel's rendering of cells that start with a + symbol.

          I imagine the cell value might need to be enclosed in single-quotes. Just guessing.
          • 2. Re: output text to CSV
            843789
            The problem isn't with your Java, it is with the "+" that you are printing. Excel is treating this as a mathematical statement.
            • 3. Re: output text to CSV
              793415
              Try this source instead.
              import java.awt.*;
              import java.io.*;
              
              class WriteCsv {
               
                public static void main(String[] args) throws Exception {
                  File file = new File("sample.csv");
                  FileWriter writer = new FileWriter(file);
                  writer.write("\"hello\"");
                  writer.write(",");
                  writer.write("5");
                  writer.write(",");
                  writer.write("\"+HJUIK\"");
                  writer.flush();
              
                  Desktop.getDesktop().open(file);
                }
              }
              This is an SSCCE. Please consider posting SSCCEs in future.
              • 4. Re: output text to CSV
                843789
                sorry for posting at wrong place. will remember next time..