使用 Apache Poi 如何创建多值电子表格单元格以便在 MS Excel 而不仅仅是 OpenOffice 中正确显示
With Apache Poi how do I create multivalue spreadsheet cells so displayed properly in MS Excel not just OpenOffice
我正在使用 Jakarta Poi 从 Java 创建一个可以在 Excel/Office 中打开的 .xlsx 文件。一些单元格代表多个值,最初是空分隔的,例如 'fred/u000/tom' 当我在单元格中设置时,我将空字符替换为 '\n'
这一切都适用于 OpenOffice,它提高了这些行的高度以容纳多行。但我发现 OpenOffice 打开 xlsx 格式的文件比打开 xls 格式的文件要花更长的时间,所以我投资了 Microsoft Excel,它显示了同一个文件,所有值都在一行上。
如果您编辑一个值,那么 Excel 会增加高度以容纳多个值,但为什么它在读取时不这样做。
由于不需要编辑电子表格,我可以使用其他东西代替 '\n' 或某些选项来强制 Excel 显示文件,如 OpenOffice
代码摘录
private void addFieldValue(Session session, Worksheet sheet, Row r, int spreadsheetIndex, SongFieldNameColumnWidth sfncw, SongChanges fieldChanges, Song song)
{
SongFieldName songFieldName = sfncw.getSongFieldName();
String value = "";
org.apache.poi.ss.usermodel.Cell c ;
c=r.createCell(spreadsheetIndex);
//Chnanges have been made to this field
if(fieldChanges!=null)
{
if (changeType == SongChangeType.ADD)
{
value = fieldChanges.getNewValue().replace('\u0000', '\n');
c.setCellValue(value);
c.setCellStyle(fieldAddedStyle);
}
else if (changeType == SongChangeType.EDIT)
{
value = fieldChanges.getNewValue().replace('\u0000', '\n');
c.setCellValue(value);
c.setCellStyle(fieldChangedStyle);
//addCellComment(r, c, sheet, fieldChanges.getOriginalValue());
}
else if (changeType == SongChangeType.DELETE)
{
c.setCellStyle(fieldDeletedStyle);
}
}
sfncw.setColumnWidthFromValue(value);
}
从您的代码中不清楚您做错了什么,但正如我在评论中所说,setWrapText needs to be set true in the CellStyle 对于应该有换行的单元格。
示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelCellWrapText {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
CellStyle cellstyle = workbook.createCellStyle();
cellstyle.setWrapText(true);
Cell cell = row.createCell(0);
cell.setCellValue("String cell content\nhaving line wrap.");
cell.setCellStyle(cellstyle);
sheet.autoSizeColumn(0);
workbook.write(new FileOutputStream("CreateExcelCellWrapText.xlsx"));
workbook.close();
}
}
结果:
我正在使用 Jakarta Poi 从 Java 创建一个可以在 Excel/Office 中打开的 .xlsx 文件。一些单元格代表多个值,最初是空分隔的,例如 'fred/u000/tom' 当我在单元格中设置时,我将空字符替换为 '\n'
这一切都适用于 OpenOffice,它提高了这些行的高度以容纳多行。但我发现 OpenOffice 打开 xlsx 格式的文件比打开 xls 格式的文件要花更长的时间,所以我投资了 Microsoft Excel,它显示了同一个文件,所有值都在一行上。
如果您编辑一个值,那么 Excel 会增加高度以容纳多个值,但为什么它在读取时不这样做。
代码摘录
private void addFieldValue(Session session, Worksheet sheet, Row r, int spreadsheetIndex, SongFieldNameColumnWidth sfncw, SongChanges fieldChanges, Song song)
{
SongFieldName songFieldName = sfncw.getSongFieldName();
String value = "";
org.apache.poi.ss.usermodel.Cell c ;
c=r.createCell(spreadsheetIndex);
//Chnanges have been made to this field
if(fieldChanges!=null)
{
if (changeType == SongChangeType.ADD)
{
value = fieldChanges.getNewValue().replace('\u0000', '\n');
c.setCellValue(value);
c.setCellStyle(fieldAddedStyle);
}
else if (changeType == SongChangeType.EDIT)
{
value = fieldChanges.getNewValue().replace('\u0000', '\n');
c.setCellValue(value);
c.setCellStyle(fieldChangedStyle);
//addCellComment(r, c, sheet, fieldChanges.getOriginalValue());
}
else if (changeType == SongChangeType.DELETE)
{
c.setCellStyle(fieldDeletedStyle);
}
}
sfncw.setColumnWidthFromValue(value);
}
从您的代码中不清楚您做错了什么,但正如我在评论中所说,setWrapText needs to be set true in the CellStyle 对于应该有换行的单元格。
示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelCellWrapText {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
CellStyle cellstyle = workbook.createCellStyle();
cellstyle.setWrapText(true);
Cell cell = row.createCell(0);
cell.setCellValue("String cell content\nhaving line wrap.");
cell.setCellStyle(cellstyle);
sheet.autoSizeColumn(0);
workbook.write(new FileOutputStream("CreateExcelCellWrapText.xlsx"));
workbook.close();
}
}
结果: