excel 导出 单元格换行

1、使用setWrapText方法来设置单元格的自动换行:style.setWrapText(true);

2、使用\n来表示换行

java 复制代码
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelExporter {
    public static void main(String[] args) throws IOException {
        // 创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook();

        // 创建工作表对象
        XSSFSheet sheet = workbook.createSheet("Sheet1");

        // 创建行和单元格对象
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);

        // 设置单元格的值和样式
        cell.setCellValue("Hello\nWorld");
        XSSFCellStyle style = workbook.createCellStyle();
        style.setWrapText(true);
        cell.setCellStyle(style);

        // 将工作簿写入文件
        FileOutputStream outputStream = new FileOutputStream("excel1.xlsx");
        workbook.write(outputStream);
        outputStream.close();
    }
相关推荐
LAM LAB2 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下2 天前
Keepalived学习总结
excel
Youngchatgpt2 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好2 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu2 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
骆驼爱记录2 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu3 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707783 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong3 天前
B生所学EXCEL
人工智能·excel