导入xlsx批量数据java

时间:2017-10-27 11:50:46

标签: java excel

我在Excel工作表中有100万条数据记录。客户端要求是将此文件转换为csv格式。

我尝试了以下一组代码,

File src = new File("C:\\test.xlsx")
File dest = new File("C:\\test.csv")
src.renameTo(dest);

这是转换文件。但是当我打开时,我在打开文件时遇到错误。

另外,我尝试了以下代码,

class XlstoCSV 
  {
    public static void main(String[] args) 
    {
            File inputFile = new File("C:\test.xls");
            File outputFile = new File("C:\output.csv");
              // For storing data into CSV files
    StringBuffer data = new StringBuffer();
    try 
    {
    FileOutputStream fos = new FileOutputStream(outputFile);

    // Get the workbook object for XLS file
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
    // Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);
    Cell cell;
    Row row;

    // 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:
                            data.append(cell.getNumericCellValue() + ",");
                            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('\n'); 
            }
    }

    fos.write(data.toString().getBytes());
    fos.close();
    }
    catch (FileNotFoundException e) 
    {
            e.printStackTrace();
    }
    catch (IOException e) 
    {
            e.printStackTrace();
    }
    }

但我的代码在此文件中失败了,

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));

我在上面的行中遇到了Heap Memory错误。我不确定如何使用java进行批量数据上传。我甚至尝试过Apache POI jar代码。但是那段代码也失败了。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

使用Apache POI导入Excel文件,使用MapDB缓存基于磁盘的文件数据库中的行。

答案 1 :(得分:0)

最简单的方法(假设代码适用于较小的文件):增加JVM使用的内存。

或者,您可以逐行编写文件:

Path outputFile = Paths.get("C:\output.csv");

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
HSSFSheet sheet = workbook.getSheetAt(0);

for (Row row : sheet) {
  List<String> csv = new StringBuilder();
  for (Cell cell : row) {
    //add the logic with csv.add(cell.getXXXValue()); etc., no comma here
  }
  String csvRow = String.join(",", csv) + "\n";
  Files.write(outputFile, csvRow.getBytes(UTF_8), StandardOpenOption.APPEND);
}