views:

110

answers:

5

hi,

i want to insert data from an excel file into a local database in a UNIX server with java without any manipulation of data.

1- someone told me that i've to convert the excel file extension into .csv to conform with unix. i created a CSV file for each sheet (i've 12) with a macro. the problem is it changed the date format from DD-MM-YYYY to MM-DD-YYYY. how to avoid this?

2- i used LOAD DATA command to insert data from the CSV files to my database. there's a date colonne that is optionnaly specified in the excel file. so in CSV it become ,, so the load data doesn't work (an argument is needed). how can i fix this? thanks for your help

A: 

We are using SQLite in our java application. It's serveless, really simple to use and very efficient.

Manuel Selva
+1  A: 

Save the EXCEL file as CSV (comma separated values) format. It will make it easy to read and parse with fairly simple use of StringTokenizer.

Use MySQL (or SQLite depending on your needs) and JDBC to load data into the database.

Here is a CSVEnumeration class I developed:

package com.aepryus.util;

import java.util.*;

public class CSVEnumeration implements Enumeration {
    private List<String> tokens = new Vector<String>();
    private int index=0;

    public CSVEnumeration (String line) {
        for (int i=0;i<line.length();i++) {
            StringBuffer sb = new StringBuffer();
            if (line.charAt(i) != '"') {
                while (i <  line.length() && line.charAt(i) != ',') {
                    sb.append(line.charAt(i));
                    i++;
                }
                tokens.add(sb.toString());
            } else {
                i++;
                while(line.charAt(i) != '"') {
                    sb.append(line.charAt(i));
                    i++;
                }
                i++;
                tokens.add(sb.toString());
            }
        }
    }

// Enumeration =================================================================
    public boolean hasMoreElements () {
        return index < tokens.size();
    }
    public Object nextElement () {
        return tokens.get(index++);
    }
}

If you break the lines of the CSV file up using split and then feed them one by one into the CSVEnumeration class, you can then step through the fields. Or here is some code I have lying around that uses StringTokenizer to parse the lines. csv is a string that contains the entire contents of the file.

StringTokenizer lines = new StringTokenizer(csv,"\n\r");
lines.nextToken();
while (lines.hasMoreElements()) {
    String line = lines.nextToken();
    Enumeration e = new CSVEnumeration(line);
    for (int i=0;e.hasMoreElements();i++) {
        String token = (String)e.nextElement();
        switch (i) {
            case 0:/* do stuff */;break;
        }
    }
}
aepryus
i've read that StringTokenizer is deprecated and that's better to use Split. do you really think it's easier with StringTokenizer?
daria
StringTokenizer is not deprecated, but I do see that .split() is the preferred method now. I'll have to do a little research, but my guess is that they are equally easy to use. At any rate, I'll post some code.
aepryus
thanks, sorry but i need to ask u one last thing: my file (csv) is made of over 788 line, parsing it will take quite some time plus i need to update the database periodically (every 30min to 1h). does this really fit my case?
daria
Well, give it a try, but 788 lines is nothing. It should be able to parse it instantaneously.
aepryus
A: 

You can use MySQL DB. Then export the xls file with Excel to csv - format and import this csv-file with phpMyAdmin.

There are also many tool available for same purpose like http://www.easyfrom.net/download/ You can find plenty on Google.

YoK
+1  A: 

I suggest MySQL for its performance and obviously open source.

Here comes two situations:

  1. If you want just to store the excel cell values into the database. You can convert the excel to CSV format, so that you can simply LOAD DATA command in MySQL command.

  2. If you have to do some manipulation before the values to get into the tables, I suggest Apache POI. I've used, that works so fine, whatever you're format of Excel you just have to use the correct implementation.

venJava
@ven coder: all i need is inserting data without any manipulation. i've a problem, my excel file has many sheets. when i convert my file to csv it copy only one sheet.
daria
+2  A: 

It should be quite easy to read out the values from Excel with Apache POI. Then you save yourself the extra step of converting to another format and possible problems when your data contains comma and you convert to CSV.

rlovtang
i think u're right. thanks
daria