【问题标题】:while converting excel to the .csv file i am not able to converting date and save it in the csv file?在将 excel 转换为 .csv 文件时,我无法转换日期并将其保存在 csv 文件中?
【发布时间】:2016-03-18 05:26:24
【问题描述】:

我正在使用 Apache POI 3.6,我正在尝试从 excel 读取数据并将 excel 数据转换为 .CVS 文件。 我的 Excel 数据将采用这种格式

  1. 第一列将是:日期(2009 年 8 月 6 日)
  2. 第二列将是:时间(12:00:01 AM)
  3. 第三列将是:值 1 (30)
  4. 第 4 列将是:值 2 (400.99)。

我需要将此数据转换为这种格式 (8-6-2009) 并将其存储在 CVS 文件中 错误:我可以将数据存储在 csv 文件中,但我无法在其中转换日期和时间。它具有一些价值 在日期列中我得到 41426,在时间列中我得到这个 3456.0。

我不知道如何在上面进行转换: 请任何人帮助我.. :(

我的代码在这里:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

 public class Excel {

static void xlsx(File inputFile, File outputFile) {
    // For storing data into CSV files
    StringBuffer data = new StringBuffer();

    try {
        FileOutputStream fos = new FileOutputStream(outputFile);
        FileInputStream fio = new FileInputStream(inputFile);
        // Get the workbook object for XLSX file
        XSSFWorkbook wBook = new XSSFWorkbook(fio);
        // Get first sheet from the workbook
        XSSFSheet sheet = wBook.getSheetAt(0);
        Row row;
        Cell cell;
        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            row = rowIterator.next();

            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {

                cell = cellIterator.next();

                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        data.append(cell.getBooleanCellValue() + ",");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (row.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC){
                            data.append(cell.getNumericCellValue() + ",");
                            }
                        if (DateUtil.isCellDateFormatted(row.getCell(0))) {
                            System.out.println ("Row No.: " + row.getRowNum ()+ " " + 
                                row.getCell(0).getDateCellValue());
                            data.append(row.getCell(0).getDateCellValue() + ",");
                        }
                        break;
                    case Cell.CELL_TYPE_STRING:
                        data.append(cell.getStringCellValue() + ",");
                        break;
                    case Cell.CELL_TYPE_BLANK:
                        data.append("" + ",");
                        break;
                    default:
                        data.append(cell + ",");

                }
            }data.append("\r\n");
        }//System.out.println();
        //System.out.println(data);
        fos.write(data.toString().getBytes());
        fos.close();

    } catch (Exception ioe) {
        ioe.printStackTrace();
    }
}
//testing the application 

public static void main(String[] args) {
    //reading file from desktop
    File inputFile = new File("D:\\project-work\\sampleData.xlsx");
    //writing excel data to csv 
    File outputFile = new File("D:\\project-work\\Data.csv");
    xlsx(inputFile, outputFile);
}

【问题讨论】:

  • 如果您将日期值存储为 CSV 中的字符串,您将无法获得所看到的数值。在 Excel 中,使用如下公式添加包含日期为字符串的列:=YEAR(A1)&TEXT(A1,"mm")&TEXT(A1,"dd")。还有其他方法可以做到这一点……快速的 Google 搜索会更清楚地说明这一点。
  • 为什么不直接使用Apache POI provided example program for converting a XSLX file to CSV,它会为您处理所有这些,而且内存比您的代码低?

标签: java excel csv apache-poi


【解决方案1】:

如果 Excel 将单元格正确设置为日期/时间,您应该能够通过

Date date = cell.getDateCellValue()

然后,您可以使用SimpleDateFormatter 或类似方法在 CSV 中以任何您需要的方式对其进行格式化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2021-07-15
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多