Java 一行中包含多行的Excel行
我创建了一个工作簿来显示包含不同内容的excel。该excel有一些行,每行包含一些行。问题是,当我在特定行中单击时,excel只有这种格式,我希望整个excel都是这样 这是我的代码:Java 一行中包含多行的Excel行,java,excel,apache-poi,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"); L
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");
工作簿工作簿=新的HSSFWorkbook();
Sheet Sheet=worbook.createSheet(“发布列表”);
DateFormat df=新的简化格式(“dd/MM/yyyy HH:MM:ss”);
Locale=resourceResponse.getLocale();
Font headerFont=worbook.createFont();
头方设置方高度输入点((短)11);
表头.立根重量(字体.粗体重量\u粗体);
headerFont.setColor(新的HSSFColor.Derk_BLUE().getIndex());
CellStyle headerStyle=worbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setWrapText(真);
headerStyle.setFillPattern(CellStyle.SOLID_前景);
headerStyle.setFillForegroundColor(新HSSFColor.GREY_25_PERCENT().getIndex());
试一试{
Listcountries=CountryServiceUtil.getCountries();
if(所有请求!=null){
int columnHeader=0;
Row rowHead=sheet.createRow(0);
用于(字符串标题列:标题){
Cell Cell=rowHead.createCell(columnHeader);
cell.setCellValue(LanguageUtil.get(locale,headerColumn));
cell.setCellStyle(headerStyle);
columnHeader++;
}
int requestRow=1;
for(PublicationRequest发布: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);
//出版物项目。第8单元
long scopeGroupId=PortalUtil.getScopeGroupId(resourceRequest);
ListallRequestItems=PublicationRequestItemLocalServiceUtil.findBypublicationRequestId(publication.getPublicationRequestId());
String publicationsvocableary=PrefsParamUtil.getString(resourceRequest.getPreferences(),resourceRequest,“publicationsvocableary”,“”);
AssetVocabulary publicationsTypeVocabulary=null;
if(Validator.isNotNull(PublicationsInvoculary)){
试一试{
PublicationsTypeVocability=AssetVocableAryLocalServiceUtil.GetAssetVocableAryByUuidAndGroupId(PublicationsVocableAry,scopeGroupId);
字符串类别=StringPool.BLANK;
对于(PublicationRequestItem:allRequestItems){
期刊文章晚起;
latestArticle=JournalArticleLocalServiceUtil.getLatestArticle(scopeGroupId,item.getArticleId());
如果(!category.equals(StringPool.BLANK)){
类别=类别+StringPool.NEW_行;
}
category+=RequestDisplayUtil.getPublicationTitle(区域设置、最新公告、PublicationStype词汇表);
}
rowsBody.createCell(9).setCellValue(类别);
}捕获(PortalException e){
e、 printStackTrace();
}
}
requestRow++;
}
for(int column=0;column
我也尝试过char(10),但我得到了同样的结果
谢谢我终于找到了解决办法 我只是为标题单元格提供样式:
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);
你有没有可能在以后重用headerStyle并将wrapText设置为false?我唯一使用它的是autoSizeColum,但我认为这并不重要