Skip to content Skip to sidebar Skip to footer

Apache Poi Xssf Creating Excel Files - Create Returns Empty File With Format Or File Extension Not Valid?

The relevant part of the Controller class: @RequestMapping(value = '/DBCompare/download', method = RequestMethod.GET) public void handleDownloadDBCompareReportGet(HttpServletRespon

Solution 1:

Here is my own sollution, I forgot to add it when I solved the issue:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import address.Class1;
import address.Class2;

publicclassCreateTable {

publicvoidnewTableCompare(Class1 class1, Class1 class2,
        ReportDcResultViewDto resultDto, OutputStream outputStream)throws FileNotFoundException, IOException {

    Workbookwb=newXSSFWorkbook();

    XSSFSheetsheet= (XSSFSheet) wb.createSheet();
    introwCount=1;
    intcolumnCount=7;
    ArrayList<String> columnNames = newArrayList<String>();

    columnNames.add("filename");
    columnNames.add("type");
    columnNames.add("created by");
    columnNames.add("modified by");
    columnNames.add("modification date");
    columnNames.add(class1.getName());
    columnNames.add(class2.getName());

    CreationHelpercreationHelper= wb.getCreationHelper();
    // CreateXSSFRowrowHeader= sheet.createRow(0);
    for (intj=0; j < columnCount; j++) {
        // create first rowXSSFCellcell= rowHeader.createCell(j);
        RichTextStringrichString= creationHelper
                .createRichTextString(columnNames.get(j));
        Fontfont= wb.createFont();
        font.setBold(true);
        richString.applyFont(font);

        cell.setCellValue(richString);
        CellStylestyle= wb.createCellStyle();
        style.setFont(font);

        cell.setCellStyle(style);
    }

    if (CollectionUtils.isNotEmpty(resultDto
            .getClass1ExclusiveViewDtos())) {
        for (ReportDcResultViewExclusiveDto dto : resultDto
                .getClass1ExclusiveViewDtos()) {

            XSSFRowrow= sheet.createRow(rowCount);
            for (intj=0; j < columnCount; j++) {
                XSSFCellcell= row.createCell(j);

                    switch (j) {
                    case0:
                        cell.setCellValue(dto.getFile().getFilename());
                        break;
                    case1:
                        cell.setCellValue(dto.getFile().getType()
                                .toString());
                        break;
                    case2:
                        cell.setCellValue(dto.getFile().getCreatedBy()
                                .getUsername());
                        break;
                    case3:
                        cell.setCellValue(dto.getModification().getUser()
                                .getUsername());
                        break;
                    case4:
                        cell.setCellValue(dto.getModification().getDate().toString());
                        break;
                    case5:
                        cell.setCellValue("yes");
                        break;
                    case6:
                        cell.setCellValue(" ");
                        break;
                    default:
                        cell.setCellValue("Missing");
                        break;
                    }
                }
                rowCount++;
            }
        }
        for (intk=0; k < columnCount; k++) {
            sheet.autoSizeColumn(k);
    }
    System.out.println("check rowCount: current="+rowCount);
    wb.write(outputStream);
    outputStream.close();
    wb.close(); 
}
}

I hope this will be of use to somebody.

Post a Comment for "Apache Poi Xssf Creating Excel Files - Create Returns Empty File With Format Or File Extension Not Valid?"