POI Excel - 换行单元格

POI Excel - 换行单元格 首页 / POI入门教程 / POI Excel - 换行单元格

要将多行数据写入单元格,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("Learnfk.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());
	    }
	}
}

输出:

Apache POI Newline in Cell

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

机器学习40讲 -〔王天一〕

浏览器工作原理与实践 -〔李兵〕

分布式技术原理与算法解析 -〔聂鹏程〕

性能测试实战30讲 -〔高楼〕

Linux内核技术实战课 -〔邵亚方〕

流程型组织15讲 -〔蒋伟良〕

容量保障核心技术与实战 -〔吴骏龙〕

性能优化高手课 -〔尉刚强〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

好记忆不如烂笔头。留下您的足迹吧 :)