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();
    }
相关推荐
jiayong2326 分钟前
Excel基础操作详细文档01
excel
伟贤AI之路5 小时前
原创分享:Markdown 表格导出 Excel/Json - 方便数据处理分析
json·excel·markdown
jiayong237 小时前
Excel自动化操作详细文档04
运维·自动化·excel
jiayong237 小时前
Excel高级功能详细文档03
excel
Channing Lewis20 小时前
Python读取excel转成html,并且复制excel中单元格的颜色(字体或填充)
python·html·excel
醉卧考场君莫笑1 天前
excel数据统计与数据可视化
信息可视化·excel
weixin_440401691 天前
WPS Excel 宏使用
excel··wps
GalenZhang8881 天前
Excel/WPS 表格数据合并操作指南
excel·wps
海拥✘1 天前
Excel制作跳动爱心动画:一步步创建动态数学心形图
excel
教练、我想打篮球1 天前
127 apache poi3.11 写 word 中内嵌 表格换行的输出
word·excel·docx·换行