DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=32150>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=32150





------- Additional Comments From [EMAIL PROTECTED]  2006-03-16 01:37 -------
Source Code
----------------------

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Calendar;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelExporter {

    private String spreadsheet;

    private final String NEW_LINE = "\r\n";

    private final String DELIMITER = ",";

    private final String EXTENSION = ".csv";

    public static void main(String... args) throws Exception {
        new ExcelExporter("x.xls").execute();
        System.out.println("All done!");
    }

    public ExcelExporter(String spreadsheet) {
        this.spreadsheet = spreadsheet;
    }

    private void execute() throws Exception {
        POIFSFileSystem fs = new POIFSFileSystem(new 
FileInputStream(spreadsheet));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        String tsv = "";
        Calendar cal = Calendar.getInstance();
        for (int r = 0; r < sheet.getPhysicalNumberOfRows(); r++) {
            for (short c = 0; c < sheet.getRow(r).getPhysicalNumberOfCells(); 
c++) {
                HSSFCell cell = sheet.getRow(r).getCell(c);
                String value = null;
                if (cell == null)
                    continue;
                switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC:
                    // Excel stores dates as numbers therefore the only way to
                    // determine if a cell is actually stored as a date is to
                    // look at the formatting.
                    double d = cell.getNumericCellValue();
                    // test if a date!
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        // format in form of M/D/YY
                        cal.setTime(HSSFDateUtil.getJavaDate(d));
                        value =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
                        value = cal.get(Calendar.MONTH) + 1 + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" + value;
                    } else {
                        value = "" + cell.getNumericCellValue();
                    }
                    break;

                case HSSFCell.CELL_TYPE_STRING:
                    value = cell.getStringCellValue();
                    break;

                default:
                }
                tsv += value + DELIMITER;

            }
            tsv += r == (sheet.getPhysicalNumberOfRows() - 1) ? "" : NEW_LINE; 
        }
        File f = new File("x" + EXTENSION);
        f.createNewFile();
        FileWriter fr = new FileWriter("x" + EXTENSION);
        fr.write(tsv);
        fr.flush();
        fr.close();

    }

}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/

Reply via email to