将问题转换为Java-xlsx转换为CSV问题

时间:2019-02-08 10:25:41

标签: java csv class xlsx

我目前正在开发一个小程序,用于将xlsx文件自动转换为csv,但实际上并没有用。

它有一个输入文件和一个输出文件,详细信息在代码段中。

您需要以下内容:

import java.io.*;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

您能帮我解决这个问题吗?谢谢

static void convertToXlsx(File inputFile, File outputFile) {
    // For storing data into CSV files
    StringBuffer cellValue = new StringBuffer();
    try {
        FileOutputStream fos = new FileOutputStream(outputFile);

        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));

        // Get first sheet from the workbook
        XSSFSheet sheet = wb.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:
                        cellValue.append(cell.getBooleanCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellValue.append(cell.getNumericCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_STRING:
                        cellValue.append(cell.getStringCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_BLANK:
                        cellValue.append("" + ",");
                        break;

                    default:
                        cellValue.append(cell + ",");

                }
            }
        }

        fos.write(cellValue.toString().getBytes());
        fos.close();

    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }
}

static void convertToXls(File inputFile, File outputFile) {

    StringBuffer cellDData = new StringBuffer();
    try {
        FileOutputStream fos = new FileOutputStream(outputFile);

        // Get the workbook instance 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:
                        cellDData.append(cell.getBooleanCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellDData.append(cell.getNumericCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_STRING:
                        cellDData.append(cell.getStringCellValue() + ",");
                        break;

                    case Cell.CELL_TYPE_BLANK:
                        cellDData.append("" + ",");
                        break;

                    default:
                        cellDData.append(cell + ",");
                }
            }
        }

        fos.write(cellDData.toString().getBytes());
        fos.close();

    } catch (FileNotFoundException e) {
        System.err.println("Exception" + e.getMessage());
    } catch (IOException e) {
        System.err.println("Exception" + e.getMessage());
    }
}

public static void main(String[] args) {
    File inputFile = new File("C:\input.xls");
    File outputFile = new File("C:\output1.csv");
    File inputFile2 = new File("C:\\Users\\lendvaigy\\Desktop\\AKK\\DOWNLOAD TEST\\legjobb-eladasi-es-veteli-hozamok-arfolyamok.xlsx");
    File outputFile2 = new File("C:\\Users\\lendvaigy\\Desktop\\AKK\\DOWNLOAD TEST\\legjobb-eladasi-es-veteli-hozamok-arfolyamok.csv");
    convertToXls(inputFile, outputFile);
    convertToXlsx(inputFile2, outputFile2);
}

主要问题是我不是专业人士,只是尝试创建小型程序的DIY新秀。

1 个答案:

答案 0 :(得分:0)

您的代码中的问题出在 convertToXls()中。要将xls转换为csv文件,请使用 XSSFWorkbook 而不是 HSSFWorkbook 。 尝试在 convertToXls()中更改以下内容。

static void convertToXls(File inputFile, File outputFile) throws IOException {
    Workbook wb = new XSSFWorkbook(inputFile.getPath());
    DataFormatter formatter = new DataFormatter();
    PrintStream out = new PrintStream(new FileOutputStream(outputFile), true, "UTF-8");
    Sheet sheet =wb.getSheetAt(0);
        for (Row row : sheet) {
            boolean firstCell = true;
            for (Cell cell : row) {
                if (!firstCell)
                    out.print(',');
                String text = formatter.formatCellValue(cell);
                out.print(text);
                firstCell = false;
            }
            out.println();
    }
}

其余的都应该照常工作。