如何使用java在Excel的基础上编写csv文件?

时间:2011-06-29 12:07:12

标签: java excel csv apache-poi

我有包含某些数据的电子表格。我想以编程方式将其转储为CSV文件。我怎么能用java做到这一点?

3 个答案:

答案 0 :(得分:0)

您可以使用其中一个允许您阅读Excel文件的API。就个人而言,我最喜欢JExcelApi。这很容易学习和使用。

答案 1 :(得分:0)

我在这里使用OpenCsv API和Apache-POI.TO读取excel文件并编写CSV文件。

public void ExcelTOCSV(){
    CSVWriter writer=null;
    int columnLength =0;
    int i =0;String[] rowData;
    HSSFCell cell = null;HSSFSheet sheet=null;
    int SheetIndex=0;
    try
    {
        InputStream  resourceAsStream =ClassLoader.getSystemResourceAsStream(filePath);
        HSSFWorkbook workbook = new HSSFWorkbook(resourceAsStream);
        sheet = workbook.getSheetAt(sheetIndex);

        //Here i am create my custom method to get column row on the basis of Sheet index. 
        columnLength = getNumberOfColumns(sheetIndex);
        writer = new CSVWriter(new FileWriter("c:\\yourfile.csv"));
        Iterator<Row> rows = sheet.rowIterator();
        while (rows.hasNext())
        {
                rowData=new String[columnLength];
                HSSFRow row = (HSSFRow) rows.next();
                Iterator<Cell> cells = row.cellIterator();
                i = 0;
                        while (cells.hasNext())
                        {
                            cell = (HSSFCell) cells.next();
                            //Must do this, you need to get value based on the cell type
                                                switch (cell.getCellType())
                                                {
                                                    case HSSFCell.CELL_TYPE_NUMERIC:
                                                       rowData[i]=Double.toString(cell.getNumericCellValue());
                                                    break;

                                                    case HSSFCell.CELL_TYPE_STRING:
                                                         rowData[i]=cell.getStringCellValue();
                                                       break;

                                                    case HSSFCell.CELL_TYPE_BLANK:

                                                       break;

                                                    default: break;
                                                }//end of switch
                                   i++;
                                }//cell while loop
                 writer.writeNext(rowData);//writing data into file
            }//Row While loop
             writer.close();

    } 
    catch (IOException ex)
    {
        Logger.getLogger(CreateCSVFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

方法getNumberofColumns:

 public int getNumberOfColumns(int SheetIndex)
{
    int NO_OF_Column=0;HSSFCell cell = null;
    HSSFSheet sheet=null;
            try {
                loadFile();  //load give Excel
                if(validateIndex(SheetIndex))
                {
                    sheet  = workbook.getSheetAt(SheetIndex);
                    Iterator rowIter = sheet.rowIterator();
                    HSSFRow firstRow = (HSSFRow) rowIter.next();
                    Iterator cellIter = firstRow.cellIterator();
                    while(cellIter.hasNext())
                    {
                          cell = (HSSFCell) cellIter.next();
                          NO_OF_Column++;
                    }
                }
                else
                    throw new InvalidSheetIndexException("Invalid sheet index.");
            } catch (Exception ex) {
                logger.error(ex);

            }

    return NO_OF_Column;
}

答案 2 :(得分:0)

http://techblog.ralph-schuster.eu/csv-utility-package-for-java/可以解决您的问题。有一个CSVUtils类可以为您复制。