How to read a .csv file(excel format) using Java.

Hi Everybody,
I need to read a .csv file(excel) and store all the columns and rows in 2d arrays. Then I can do the rest of the coding myself. I would like it if somebody could post their code to read .csv files over here. The .csv file can have different number of columns and different number of rows every time it is ran. The .csv file is in excel format, so I don't know if that affects the code or not. I would also appreciate it if the classes imported are posted too. I would also like to know if there is a way I can recognize how many rows and columns the .csv file has. I need this urgently so I would be very grateful to anybody who has the solution. Thanks.
Sincerely Taufiq.

I used this
BufferedReader in = new BufferedReader (new FileReader ("test.csv"));
// and                
StringTokenizer parser = new StringTokenizer (str, ", ");
                while (parser.hasMoreTokens () == true)
                { //crap }works like a charm!

Similar Messages

  • How to read a data from USB port using JAVA

    hi all,
    i need to know how to read a data from USB port using java. any API are available for java ?.........please give your valuable ideas !!!!!!!!!
    Advance Thanks!!

    You can do this. Please use this link
    [http://www.google.co.in/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=uHu&q=java+read+data+from+usb+port&btnG=Search&meta=&aq=f&oq=]
    What research did you do of your own? Have you done some testing application and tried yourself??

  • How to read a .CSV file using UTL_FILE

    HI,
    How do i read a .csv file line by line using UTL_FILE?
    Thanks in advance
    Regards,
    Gayatri

    ----do open the file logic
    begin
    ----Let's say this file is delimited by ','
    ---declare variables
    v_startPos number; -- starting position of field
    v_Pos number; -- position of string
    v_lenString number; -- length
    v_first_field varchar2(30);
    v_second_field varchar2(30);
    v_third_field varchar2(30);
    v_fourth_field varchar2(30);
    input_String varchar2(1000); -- buffer for each line of file
    ----Say you have a 4 column file delimited by ','
    delimitChar varchar2(1) := ','
    Joe;Joan;People;Animal
    Teddy;Bear;Beans;Toys
    begin
    loop
    utl_file.get_line(input_file, input_String); -- get each line
    ---- this will get the first field as specified by the last number
    v_Pos := instr(input_String,delChar,1,1);
    v_lenString := v_Pos - 1;
    v_first_field := substr(input_String,1,v_lenString);
    v_startPos := v_Pos + 1;
    -- this will get the second field
    v_Pos := instr(inString,delChar,1,2);
    v_lenString := v_Pos - v_startPos;
    v_second_field := substr(input_String,v_startPos,v_lenString);
    v_startPos := v_Pos + 1;
    -- 3rd field
    v_Pos := instr(inString,delChar,1,3);
    v_lenString := v_Pos - v_startPos;
    v_third_field := substr(input_String,v_startPos,v_lenString);
    v_startPos := v_Pos + 1;
    -- last field -- there is no delimiter for last field
    v_Pos := length(input_String) + 1;
    v_lenString := v_Pos - v_startPos;
    v_fourth_field := substr(input_String,v_StartPos,v_lenString);
    end;
    EXCEPTION
    WHEN no_data_found then
              fnd_file.put_line(FND_FILE.LOG, 'Last line so exit');
              exit;
    end loop;

  • How to read the CSV Files into Database Table

    Hi
    Friends i have a Table called tblstudent this has the following fields Student ID, StudentName ,Class,Father_Name, Mother_Name.
    Now i have a CSV File with 1500 records with all These Fields. Now in my Program there is a need for me to read all these Records into this Table tblstudent.
    Note: I have got already 2000 records in the Table tblstudent now i would like to read all these CSV File records into the Table tblstudent.
    Please give me some examples to do this
    Thank your for your service
    Cheers
    Jofin

    1) Read the CSV file line by line using BufferedReader.
    2) Convert each line (record) to a List and add it to a parent List. If you know the columns before, you might use a List of DTO's.
    3) Finally save the two-dimensional List or the List of DTO's into the datatable using plain JDBC or any kind of ORM (Hibernate and so on).
    This article contains some useful code snippets to parse a CSV: http://balusc.xs4all.nl/srv/dev-jep-csv.html

  • How To open a MS Word/Excel document using Java

    How do i open a MS-Word/Excel document using Java Code.

    Get SDK (which is freeware) at
    http://www.simtel.net/product.php?id=60701&sekid=0&SiteID=simtel.net
    http://shareware.pcmag.com/product.php?id=60701&SiteID=pcmag
    http://downloads.suntimes.com/product.php?id=60701&SiteID=suntimes
    There you will find examples for MSWord and Excel (example sources are packed with binaries).

  • How to read a CSV File

    Hi All,
    I am new to the forum so i dont know if i can post this message in this section or not but i did not find any related section in the menu so i am posting it here.
    I want to read a .CSV (comma separated file) in a particular location and see if the format is correct. if the format is ok then ftp it to another directory or another location.
    Thanks in advance.

    I want to read a .CSV (comma separated file) in a particular location and see if
    the format is correct. if the format is ok then ftp it to another directory or another locationYou don't specify how you want to handle the file. You can do it three ways:
    1) Just read the file as is, including the delimiters, then process it.
    2) Remove the delimiters from the file, then read the file and process it.
    3) Parse the file using the delimiters to separate the file into tokens, then read the tokens and process them.
    I'll assume that you want to do #3 above. The following code will handle CSV data that is delimited by a comma, and is either quoted or unquoted.
    public class Xb
        public static void main(String[] args)
            // csv include both quoted and unquoted data
            String csv = "\"a\",\"b\",\"c\",d,e,f";
            System.out.println(csv); // so you can see the data
            String[] stArray = csv.replaceAll("\"", "").split(",");
            for (int j = 0; j < stArray.length; j++)
                System.out.println(stArray[j]);
    }This is the printed output:
    "a","b","c",d,e,f
    a
    b
    c
    d
    e
    f
    Note: If you want #2 above, delete[b] .split(",") from this line:
    String[] stArray = csv.replaceAll("\"", "").split(",");

  • How to read/write .CSV file into CLOB column in a table of Oracle 10g

    I have a requirement which is nothing but a table has two column
    create table emp_data (empid number, report clob)
    Here REPORT column is CLOB data type which used to load the data from the .csv file.
    The requirement here is
    1) How to load data from .CSV file into CLOB column along with empid using DBMS_lob utility
    2) How to read report columns which should return all the columns present in the .CSV file (dynamically because every csv file may have different number of columns) along with the primariy key empid).
    eg: empid report_field1 report_field2
    1 x y
    Any help would be appreciated.

    If I understand you right, you want each row in your table to contain an emp_id and the complete text of a multi-record .csv file.
    It's not clear how you relate emp_id to the appropriate file to be read. Is the emp_id stored in the csv file?
    To read the file, you can use functions from [UTL_FILE|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#BABGGEDF] (as long as the file is in a directory accessible to the Oracle server):
    declare
        lt_report_clob CLOB;
        l_max_line_length integer := 1024;   -- set as high as the longest line in your file
        l_infile UTL_FILE.file_type;
        l_buffer varchar2(1024);
        l_emp_id report_table.emp_id%type := 123; -- not clear where emp_id comes from
        l_filename varchar2(200) := 'my_file_name.csv';   -- get this from somewhere
    begin
       -- open the file; we assume an Oracle directory has already been created
        l_infile := utl_file.fopen('CSV_DIRECTORY', l_filename, 'r', l_max_line_length);
        -- initialise the empty clob
        dbms_lob.createtemporary(lt_report_clob, TRUE, DBMS_LOB.session);
        loop
          begin
             utl_file.get_line(l_infile, l_buffer);
             dbms_lob.append(lt_report_clob, l_buffer);
          exception
             when no_data_found then
                 exit;
          end;
        end loop;
        insert into report_table (emp_id, report)
        values (l_emp_id, lt_report_clob);
        -- free the temporary lob
        dbms_lob.freetemporary(lt_report_clob);
       -- close the file
       UTL_FILE.fclose(l_infile);
    end;This simple line-by-line approach is easy to understand, and gives you an opportunity (if you want) to take each line in the file and transform it (for example, you could transform it into a nested table, or into XML). However it can be rather slow if there are many records in the csv file - the lob_append operation is not particularly efficient. I was able to improve the efficiency by caching the lines in a VARCHAR2 up to a maximum cache size, and only then appending to the LOB - see [three posts on my blog|http://preferisco.blogspot.com/search/label/lob].
    There is at least one other possibility:
    - you could use [DBMS_LOB.loadclobfromfile|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_lob.htm#i998978]. I've not tried this before myself, but I think the procedure is described [here in the 9i docs|http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96591/adl12bfl.htm#879711]. This is likely to be faster than UTL_FILE (because it is all happening in the underlying DBMS_LOB package, possibly in a native way).
    That's all for now. I haven't yet answered your question on how to report data back out of the CLOB. I would like to know how you associate employees with files; what happens if there is > 1 file per employee, etc.
    HTH
    Regards Nigel
    Edited by: nthomas on Mar 2, 2009 11:22 AM - don't forget to fclose the file...

  • How to read a CSV file into the portal

    hi all,
    I want to read the content of CSV file that is avaliable in my desktop.
    plz help me with suitable code
    Regards
    Savitha
    Edited by: Savitha S R on Jun 1, 2009 8:25 AM

    Please use this code for that
    REPORT  znkp_upload_csv line-size 400.
    DATA : v_filename TYPE string.
    PARAMETER : p_file LIKE rlgrap-filename DEFAULT 'C:\Documents and Settings\Administrator\Desktop\JV.csv' .
    *Types for Reading CSV file
    TYPES : BEGIN OF x_csv,
              text(400) TYPE c,
            END OF x_csv.
    *Global internal table for reading CSV file
    DATA : lt_csv TYPE TABLE OF x_csv.
    *Global work area for reading CSV file
    DATA : wa_csv LIKE LINE OF lt_csv.
    v_filename = p_file.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                      = v_filename
      FILETYPE                      = 'ASC'
      HAS_FIELD_SEPARATOR           = ' '
      HEADER_LENGTH                 = 0
      READ_BY_LINE                  = 'X'
      DAT_MODE                      = ' '
      CODEPAGE                      = ' '
      IGNORE_CERR                   = ABAP_TRUE
      REPLACEMENT                   = '#'
      CHECK_BOM                     = ' '
      VIRUS_SCAN_PROFILE            =
      NO_AUTH_CHECK                 = ' '
    IMPORTING
      FILELENGTH                    =
      HEADER                        =
      TABLES
        data_tab                      = lt_csv
    EXCEPTIONS
       file_open_error               = 1
       file_read_error               = 2
       no_batch                      = 3
       gui_refuse_filetransfer       = 4
       invalid_type                  = 5
       no_authority                  = 6
       unknown_error                 = 7
       bad_data_format               = 8
       header_not_allowed            = 9
       separator_not_allowed         = 10
       header_too_long               = 11
       unknown_dp_error              = 12
       access_denied                 = 13
       dp_out_of_memory              = 14
       disk_full                     = 15
       dp_timeout                    = 16
       OTHERS                        = 17
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    DATA : BEGIN OF item OCCURS 0 ,
            t1(20) TYPE c,
            t2(20) TYPE c,
            t3(20) TYPE c,
            t4(20) TYPE c,
            t5(20) TYPE c,
            t6(20) TYPE c,
            t7(20) TYPE c,
            t8(20) TYPE c,
           END OF item.
    DATA : txt(1) TYPE c. " 1-Header 2-Item
    LOOP AT lt_csv into wa_csv.
    split wa_csv-text at ',' into item-t1
                                  item-t2
                                  item-t3
                                  item-t4
                                  item-t5
                                  item-t6
                                  item-t7
                                  item-t8.
    append item.
    clear item.
    ENDLOOP.
    Check ITEM TABLE
    Regards
    Naresh

  • How to Read a CSV file using pure PL/SQL

    Hi friends - Is there a possibility to read a .TXT/CSV file from a local machine and display the contents on the form fields each record at one time.
    The logic to flow through the records can be build, but the problem is where to begin from to start reading the file using PL/SQL. I don't want to use any third party component/API as such. Is there any way in forms by using PL/SQL to read an external CSV file and interpret the contents.
    I have read about SQL * Loader on some sites but cannot find any way to invoke it on windows platform. There seems to be UNIX commands to invoke the .CTL file that is used by SQL Loader. Any help is much apreciated.
    Rgds

    Hi Thanks for your replies, TEXT_IO seems to be a solution but not very comprehensive as it provides limited exposed functions to perform complex operations while read and write of files.
    I think SQL*Loader is a valid solution, but as I stated in my original quote Im not able to invoke it from the command prompt. The command that is shown on the suggested URL(http://www.orafaq.com/faqloadr.htm) is not found on my machine. I have Windows 2K installed. Is there a seperate patch or a download available from where I can get that .EXE to invoke the utility.
    Thanks..

  • How to search a .csv file for data using its timestamp, then import to labview

    Hi, I'm currently obtaining density, viscosity and temperature data from an instrument, adding a timestamp and writing it to a .csv file which I can view in Excel. This works fine (see attached code) but what I need to do now is to search that csv file for data which was obtained at a certain time, import the temperature, density & viscosity values at this time back into Labview to do some calculations with them, while the data acquisition process is still ongoing.
    I've found various examples on how to import an entire csv file into labview, but none on how to extract data at a specific time. Also, whenever I try to do anything with the .csv file while my data acquistion VI is running, I receive error messages (presumably because I'm trying to write to and import data from the .csv file simultaneously). Is there some way around this, maybe using case structures?
    If you need to know my skill level, I've been using Labview for a few weeks and prior to that have basically no experience of writing code, so any help would be great. Thanks!
    Solved!
    Go to Solution.
    Attachments:
    Lemis VDC-30 read registers MODBUS v5.vi ‏56 KB

    It sounds as if you are going about this a little backwards writing to a data file and then extracting from the file but its the weekend so I can't think of an improved way to do it at the moment. 
    Searching for a specific time with those specific values is quite easy, or if you wanted to select any time then you could interpolate the values to find any value that you want (This is where the contiguous measurement comes in, as you have readings at discrete times you will have to interpolate the values if you want to get the 'measured value' at a time point that is not exactly one of your measured points).
    If you can extract the TDMS time column and the T, D & V then simply thresholding and interpolating each of your array/data sets should allow readings at your desired times.
    Attachments:
    Interpolate.png ‏301 KB

  • How to Export SSRS report into Excel format using query string

    Hello all,
    I am new to SSRS world.
    Last night i was trying to add a hyperlink  on the SSRS report (.rdl) file, by clicking which we can export the report into "Excel" format, but with no luck.
    Can anybody tell me how to form the hyperlink, so that we can export the report with its current content ( I mean the report content generated by selecting some parameter values)

    Hi Suman85,
    Based on your scenario, you want to add a link on the SSRS report file, and then click it you can export the report into “Excel” format.
    Just as kishan1901 said , you can add a textbox to your report and type in the words “Click here and export the report
    to excel” .Then right-click the textbox and select Properties. Switch to Navigation tab, click the radiobutton of ‘Jump to URL’ and type in the expression ,here is just a example:
     ="http://vyof1884200/ReportServer$youhoo?/My+Adventure+Works/Product+Sales+and+Profitability+by+Month&rs:Command=Render&rs:Format=excel"
    Then preview the report ,you will see
    Click here and export the report to excel 
    when you move the mouse to it ,the mouse will become a hand ,then left-click, it will jump out a dialog box to indicate user whether the excel file should be saved , opened or canceled. 
    I think this is what you want, if you have any further requirement, could you please describe it in more detail? We would give you some resolution or workaround.
    Regards,
    Challen Foo

  • How to read Microsoft Project File(.mpp) with pure Java?

    Hi all:
    I want to read Mircorsoft Project File in Solaris,but it too difficult to me.
    Anyone that can help me with this it would be much appreciated!
    Eagle

    most of microsoft documents (doc,excel,mpp) files are formatted in propriety way. at the same time they follow some xml standards.
    i think you can get those xml DTDs and try to build read mpp as xml.
    have a trial in that way also.

  • How to read an xml file in OSB using file protocol where reading file path is dynamic.

    HI,
    We have to read a xml file from a location and write into Db.
    Here reading file location is dynamic .
    Please suggest

    Hi
    With open dataset you can open any file on the application server. So it should be working, but maybe you are looking for:
    CALL TRANSFORMATION my_transformation
           SOURCE  XML xml_string
           RESULT  ROOT1 = field1
                   ROOT2 = field2.
    Best regards,
    Guido Koopmann

  • How to convert from xml file to html using java code

    How to convert from xml file to html file using java code

    Get yourself Apache Xalan or Saxon or some XSLT processor
    String styleSheet = "/YourXSLTStylesheet.xsl";
    String dataSource = "/YourXMLDocument.xml";
    InputStream stylesheetSource = TransformMe.class.getResourceAsStream(styleSheet);
    InputStream dataSourceStream = TransformMe.class.getResourceAsStream(dataSource);
    OutputStream transformedOut = new FileOutputStream("filename.html");
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheetSource));
    transformer.transform(new StreamSource(dataSourceStream), new StreamResult(transformedOut));You'll also need to learn XSLT if you don't already know that. Here's a good place to start
    http://www.w3schools.com/xsl/

  • How to read the hard disk serial number using java this is urgent

    Please do let me know any knows how to read the hard disk unique number provided by manufactruer using java library

    I don't know; I think the fate of the free worldrests upon the answer.
    Well, as long as the non-free world stays unharmed,
    it can't be so bad ...I think the "Axis of Evil" constitutes a group of countries that are just "misunderstood".

Maybe you are looking for

  • I want to move everything from one apple id to another

    I have an apple id using my gmail account. I have had that since the 1st iphone. Then I got a mobieme account and now icloud. What I want to do is get rid of my gmail and only use my me.com. Is there any way to merge or move accounts? I tried just ch

  • Getting error while posting vendor invoice

    Hi, I was trying to post a vendor invoice and am getting an error  "Balancing field "Profit Center" in line item 001 not filled" I have changed the field status of the posting key and the reconciliation account but am still getting the same message.

  • ORA-00917: missing comma when using NVL

    Hi All, I have a INSERT statement which works fine and looks something like: EXECUTE IMMEDIATE 'insert into MYTABLE(ID,TASK_ROLE, PROGRESS,SALES_PERSON) values ... However if I change the above piece to use NVL function like this: EXECUTE IMMEDIATE '

  • HT4009 In-App purchase not showing up even though I have a digital receipt...?

    I have a receipt that I purchased an item in-app but it never downloaded to my ipad. Went to see if I could redownload the item, but I'm going to be charged the price for the item again. [It's just a comic] I'd prefer to not get charged again, though

  • After automatic renaming dhcpcd on enp4s0 fails to load

    Hello, I installed arch and had this from the very beginning (and reinstalled it and had the same issue): hwdb.bin does not exist, please run udevadm hwdb --update Everything worked, but after 4 days this error occured: [ TIME ] Timed out waiting for