Apache POI Excel 换行符

为了将多行数据写入单元格,Apache POI 提供了处理它的方法。让我们看一个示例,其中我们将多行数据存储到 a 单元格中。

Apache POI中的 单元格换行符示例

package com.yiidian;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class NewlineExample {
    public static void main(String[] args) {  
        try (OutputStream fileOut = new FileOutputStream("yiidian.xls")) {
            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("Sheet");
            Row row     = sheet.createRow(1);
            Cell cell   = row.createCell(1);
            cell.setCellValue("This is first line and \n this is second line");  
            CellStyle cs = wb.createCellStyle();
            cs.setWrapText(true);  
            cell.setCellStyle(cs);  
            row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));  
            sheet.autoSizeColumn(2);  
            wb.write(fileOut);  
        }catch(Exception e) {  
            System.out.println(e.getMessage());  
        }  
    }  
}  

输出结果为:

热门文章

优秀文章