导出xlsx文件可以打开错误"无效的文件格式或扩展"

时间:2017-02-12 12:08:42

标签: c# excel apache-poi

Apache POI为3.15我使用SXSSFWorkbook导出.xlsx文件,但导出损坏的文件.MS Excel无法打开文件格式或扩展名无效错误的文件。 任何人都可以帮助我在这里做错了吗?

代码是:

public void exportToExcel(FacesContext facesContext, OutputStream
   outputStream) {
                Workbook wb = new XSSFWorkbook();

                CreationHelper createHelper = wb.getCreationHelper();
                Sheet sheet = wb.createSheet("RouteDetails");
                Row row = sheet.createRow(0);
                Cell cell = row.createCell(0);

                Font font = wb.createFont();
                font.setFontHeightInPoints((short) 9);
                font.setFontName("Courier New");
                font.setItalic(true);
                font.setBold(true);
                CellStyle style = wb.createCellStyle();
                style.setFont(font);
                cell.setCellStyle(style);
                cell.setCellValue("Route Detail Import Export Template");
                Row r1 = sheet.createRow(1);
                Cell c1 = r1.createCell(0);
                c1.setCellStyle(style);
                c1.setCellValue("Route Id");
                Cell c11 = r1.createCell(1);
                c11.setCellValue(getRouteId());
                Row r2 = sheet.createRow(2);
                Cell c2 = r2.createCell(0);
                c2.setCellStyle(style);
                c2.setCellValue("Route Name");
                Cell c12 = r2.createCell(1);
                c12.setCellValue("ABC");

                Row r3 = sheet.createRow(3);
                Cell c3 = r3.createCell(0);
                c3.setCellStyle(style);
                c3.setCellValue("Salesman");
                Cell c13 = r3.createCell(1);
                c13.setCellValue(route.getSalesmanId() + route.getName());


                Row r5 = sheet.createRow(4);
                Cell c5 = r5.createCell(0);
                c5.setCellStyle(style);
                c5.setCellValue("WeekNumber");
                Cell c15 = r5.createCell(1);
                c15.setCellValue(getWeekNo());
                Row r6 = sheet.createRow(5);
                Cell c6 = r6.createCell(0);
                c6.setCellStyle(style);
                c6.setCellValue("Year");
                Cell c16 = r6.createCell(1);
                c16.setCellValue(getYear());
                Row r7 = sheet.createRow(6);
                Cell c7 = r7.createCell(0);
                c7.setCellStyle(style);
                c7.setCellValue("Date");
                CellStyle cellStyle = wb.createCellStyle();
                cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy"));
                Cell c8 = r7.createCell(1);
                c8.setCellValue(new Date());
                c8.setCellStyle(cellStyle);
                Row r9 = sheet.createRow(7);
                Cell c9 = r9.createCell(0);
                c9.setCellValue("");
                Row detRow = sheet.createRow(8);
                cell.setCellStyle(style);
                cell = detRow.createCell(0);
                cell.setCellStyle(style);
                cell.setCellValue("Customer Code");
                cell = detRow.createCell(1);
                cell.setCellStyle(style);
                cell.setCellValue("Customer Name");
                cell = detRow.createCell(2);
                cell.setCellStyle(style);
                cell.setCellValue("Remarks");
                cell = detRow.createCell(3);
                cell.setCellStyle(style);
                cell.setCellValue("Frequency");
                cell = detRow.createCell(4);
                cell.setCellStyle(style);
                cell.setCellValue("Sunday");
                cell = detRow.createCell(5);
                cell.setCellStyle(style);
                cell.setCellValue("Monday");
                cell = detRow.createCell(6);
                cell.setCellStyle(style);
                cell.setCellValue("Tuesday");
                cell = detRow.createCell(7);
                cell.setCellStyle(style);
                cell.setCellValue("Wednesday");
                cell = detRow.createCell(8);
                cell.setCellStyle(style);
                cell.setCellValue("Thursday");
                cell = detRow.createCell(9);
                cell.setCellStyle(style);
                cell.setCellValue("Friday");
                cell = detRow.createCell(10);
                cell.setCellStyle(style);
                cell.setCellValue("Saturday");

                // Write the output to a file
                for (int i = 0; i < getDetails().size(); i++) {
                    Row detailRow = sheet.createRow(i + 9);
                    RouteDetail det = getDetails().get(i);
                    detailRow.createCell(0).setCellValue(det.getCustomerCode());
                    detailRow.createCell(1).setCellValue(det.getCustomerName());
                    detailRow.createCell(2).setCellValue(det.getRemarks());
                    detailRow.createCell(3).setCellValue(det.getFrequency());
                    detailRow.createCell(4).setCellValue(det.getSun());
                    detailRow.createCell(5).setCellValue(det.getMon());
                    detailRow.createCell(6).setCellValue(det.getTue());
                    detailRow.createCell(7).setCellValue(det.getWed());
                    detailRow.createCell(8).setCellValue(det.getThu());
                    detailRow.createCell(9).setCellValue(det.getFri());
                    detailRow.createCell(10).setCellValue(det.getSat());


                }

                sheet.autoSizeColumn(0);
                sheet.autoSizeColumn(1);
                sheet.autoSizeColumn(2);
                sheet.autoSizeColumn(3);
                sheet.autoSizeColumn(4);
                sheet.autoSizeColumn(5);
                sheet.autoSizeColumn(6);
                sheet.autoSizeColumn(7);
                sheet.autoSizeColumn(8);
                sheet.autoSizeColumn(9);
                sheet.autoSizeColumn(10);
                sheet.autoSizeColumn(11);
                sheet.autoSizeColumn(13);


                try {
                    wb.write(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

0 个答案:

没有答案