java poi 换行,apache poi单元格中的换行

本文概述

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

单元示例中的Apache POI换行符

package poiexample;

import java.io.FileOutputStream;

import java.io.OutputStream;

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

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellStyle;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

public class NewlineExample {

public static void main(String[] args) {

try (OutputStream fileOut = new FileOutputStream("srcmini.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());

}

}

}

输出:

1ad33cf786f82cab92d8d48cadd06096.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值