Excel中包含多行的行

时间:2015-12-17 11:30:38

标签: java excel apache-poi

我创建了一个工作簿来显示具有不同内容的Excel。那个excel有一些行,每行包含一些行。问题是,当我点击特定行时,excel只有那种格式,我希望整个excel看起来像那样。

这是我的代码:

Workbook worbook = new HSSFWorkbook();
Sheet sheet = worbook.createSheet("Publication List");
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Locale locale = resourceResponse.getLocale();
Font headerFont = worbook.createFont();
headerFont.setFontHeightInPoints((short) 11);
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerFont.setColor(new HSSFColor.DARK_BLUE().getIndex());
CellStyle headerStyle = worbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setWrapText(true);
headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headerStyle.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());

try {
    List < Country > countries = CountryServiceUtil.getCountries();

    if (allRequests != null) {
        int columnHeader = 0;
        Row rowHead = sheet.createRow(0);

        for (String headerColumn: header) {
            Cell cell = rowHead.createCell(columnHeader);
            cell.setCellValue(LanguageUtil.get(locale, headerColumn));
            cell.setCellStyle(headerStyle);
            columnHeader++;
        }

        int requestRow = 1;
        for (PublicationRequest publication: allRequests) {
            Row rowsBody = sheet.createRow(requestRow);
            Country country = CountryServiceUtil.fetchCountryByA2(publication.getCountry());
            String fullAddress = publication.getAddress() + StringPool.RETURN_NEW_LINE + publication.getCp() + StringPool.SPACE + publication.getCity() + StringPool.RETURN_NEW_LINE + country.getName();

            rowsBody.createCell(8).setCellValue(fullAddress);

            //Publication items. Cell 8
            long scopeGroupId = PortalUtil.getScopeGroupId(resourceRequest);
            List < PublicationRequestItem > allRequestItems = PublicationRequestItemLocalServiceUtil.findBypublicationRequestId(publication.getPublicationRequestId());
            String publicationsVocabulary = PrefsParamUtil.getString(resourceRequest.getPreferences(), resourceRequest, "publicationsVocabulary", "");
            AssetVocabulary publicationsTypeVocabulary = null;
            if (Validator.isNotNull(publicationsVocabulary)) {

                try {
                    publicationsTypeVocabulary = AssetVocabularyLocalServiceUtil.getAssetVocabularyByUuidAndGroupId(publicationsVocabulary, scopeGroupId);
                    String category = StringPool.BLANK;
                    for (PublicationRequestItem item: allRequestItems) {
                        JournalArticle latestArticle;
                        latestArticle = JournalArticleLocalServiceUtil.getLatestArticle(scopeGroupId, item.getArticleId());
                        if (!category.equals(StringPool.BLANK)) {
                            category = category + StringPool.NEW_LINE;
                        }

                        category += RequestDisplayUtil.getPublicationTitle(locale, latestArticle, publicationsTypeVocabulary);
                    }

                    rowsBody.createCell(9).setCellValue(category);
                } catch (PortalException e) {
                    e.printStackTrace();
                }

            }

            requestRow++;

        }

        for (int column = 0; column < columnHeader + 1; column++) {
            sheet.autoSizeColumn(column);
        }
    }
} catch (SystemException e) {
    _log.error("Error while creating Excel file", e);
} catch (PortalException e1) {
    e1.printStackTrace();
}

resourceResponse.setContentType("application/vnd.ms-excel");
resourceResponse.setProperty("expires", "-1d");
resourceResponse.setProperty("Pragma", "no-cache");
resourceResponse.setProperty(HttpHeaders.CACHE_CONTROL, "no-cache");

我也尝试过char(10),但我得到同样的结局

谢谢

1 个答案:

答案 0 :(得分:0)

最后我得到了一个解决方案。

我只是给标题单元格提供样式:

for (String headerColumn: header) {
    Cell cell = rowHead.createCell(columnHeader);
    cell.setCellValue(LanguageUtil.get(locale, headerColumn));
    cell.setCellStyle(headerStyle);
    columnHeader++;

我不得不先创建身体细胞并给它一些风格:

rowsBody.createCell(8).getCellStyle().setWrapText(true);
rowsBody.getCell(8).setCellValue(fullAddress);