Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java以特定格式读取excel文件

我有一个excel文件。。。它有三列。。。我想用一些特殊的方式来阅读它,比如row1cell1,row1cell2,row1cell3:row2cell1,row2cell2,row2cell3:row3cell1,row3cell2,row3cell3意味着在一行中我想要逗号分隔的值,然后一个冒号和另一行应该开始,所以我只想为xlsx文件而不是为xls。。。提前谢谢

public void read() throws IOException {
        String bulkString = "";
        File file = new File(inputfile);
        String fileName = file.getName();
        // System.out.println(fileName);
        InputStream uploadedFileStream = new FileInputStream(file);
        boolean isValid = true;
        boolean isEmpty = true;
        boolean firstcolumn = true;

        try {
            if (uploadedFileStream != null && fileName != null
                    && fileName.indexOf("xlsx") != -1) {

                XSSFWorkbook workbook = new XSSFWorkbook(uploadedFileStream);
                XSSFSheet sheet = workbook.getSheetAt(0);
                Iterator rows = sheet.rowIterator();
                if (!rows.hasNext())
                    isEmpty = false;

                else {
                    firstcolumn = false;
                    while (rows.hasNext()) {
                        XSSFRow row = ((XSSFRow) rows.next());
                        // XSSFCell cell = row.getCell(0);
                        Iterator cells = row.cellIterator();

                        while (cells.hasNext()) {
                            XSSFCell cell = (XSSFCell) cells.next();
                            // XSSFCell cell = (XSSFCell) cells.next();
                            // Cell cellValue = row.getCell(0);
                            if (cell != null) {
                                firstcolumn = true;
                                if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
                                    String cellValue = Math.round(cell
                                            .getNumericCellValue()) + "";
                                    if (cellValue.length() != 10) {
                                        isValid = false;
                                        break;
                                    }
                                    bulkString += cellValue + ",";
                                }
                                if (cell.getCellType() == cell.CELL_TYPE_STRING) {
                                    if (row.getRowNum() == 0) {
                                        continue; // just skip the rows if row
                                                    // number is 0
                                    }
                                    String cellValue = cell
                                            .getStringCellValue();
                                    if (cellValue.length() != 10) {
                                        isValid = false;
                                        break;
                                    }
                                    bulkString += cellValue + ",";
                                }
                            }
                        }

                        bulkString1.append(bulkString + ":");
                    }// bulkString1.append(bulkString+":");
                }// bulkString1.append(bulkString+":");`

共 (0) 个答案