以特定格式读取excel文件

时间:2014-04-10 07:42:47

标签: java excel

我有一个excel文件...它有3列...我想以某种特定的方式读取它,如row1cell1,row1cell2,row1cell3:row2cell1,row2cell2,row2cell3:row3cell1,row3cell2,row3cell3表示在一行i中想要逗号分隔值然后一个冒号和另一行应该开始,所以oni想要只为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 个答案:

没有答案