在java中写入文件时xlsx文件已损坏

时间:2015-05-14 06:06:21

标签: java excel

我编写此代码用于编写xlsx文件

try {  
                FileInputStream fis = new FileInputStream(filePath);  
                Workbook workbook = new XSSFWorkbook(fis);    
                Workbook workbook_ = new XSSFWorkbook();  
                String value = cmbsheets.getSelectedItem().toString();  
                Sheet sheet = workbook.getSheet(value);  
                Sheet sheet1 = workbook_.createSheet("Sheet1");  
                int q = 1;  
                Row headrow = sheet1.createRow((short) 0);  

            Iterator<Row> rowIterator = sheet.iterator();

            while (rowIterator.hasNext()) {
                row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    cell = cellIterator.next();

                    switch (cell.getCellType()) {

                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.println(cell.getBooleanCellValue());
                            break;

                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.println(cell.getNumericCellValue());
                            break;

                        case Cell.CELL_TYPE_STRING:
                            //System.out.println(cell.getStringCellValue());
                            String cellvalue = cell.getStringCellValue();
                            String[] spllitedvalues = cellvalue.split(" ");

                            headrow.createCell(0).setCellValue("No.");
                            headrow.createCell(1).setCellValue("Name");
                            headrow.createCell(2).setCellValue("Address");
                            headrow.createCell(3).setCellValue("synonyms");
                            headrow.createCell(4).setCellValue("PIN");
                            headrow.createCell(5).setCellValue("contact");
                            headrow.createCell(6).setCellValue("Age");

                            //for (int r = 0; r <= spllitedvalues.length; r++) {

                            //  Set setA = new HashSet();
                            for (String token : spllitedvalues) {


                                setA.add(token);
                            }

                            break;


                        case Cell.CELL_TYPE_BLANK:
                            System.out.println(" ");
                            break;

                        default:
                            System.out.println(cell);
                    }//End of Switch case
                    for (Object object : setA) {
                        String element = (String) object;
                        if (element.matches("([A-Za-z0-9]+)|([A-Za-z0-9]+[-][A-Za-z0-9]+)+")) {

                            Row row_ = sheet1.createRow((short) q);
                            row_.createCell(1).setCellValue(element);

                            q++;
                        }//End of If 
                    }

                    fileOut = new FileOutputStream(System.getProperty("user.home") + "\\" + "Desktop\\" + finaltargetfile + ".xlsx");
                    workbook_.write(fileOut);
                    fileOut.flush();
                    fileOut.close();

但是当打开xlsx文件时,我收到此错误信息
Excel found unreadable content in 'filename.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.
我的代码有什么问题?

0 个答案:

没有答案