在Excel中无法写入多行使用POI [英] Unable To Write Multiple Lines in Excel Using POI

查看:314
本文介绍了在Excel中无法写入多行使用POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我会读多行和一些逻辑后,我需要写在一个Excel工作表行。我使用Apache POI用于这一目的。但是,我面临的问题是,只有(从回路)的最后一行被写入到Excel

有人可以帮我在这个或提供一些code段?

感谢


解决方案

 工作簿WB =新XSSFWorkbook(); //或新HSSFWorkbook();
    苫布苫布= wb.createSheet();    鳞次栉比= sheet.createRow(2);
    细胞细胞= row.createCell(2);
    cell.setCellValue(使用\\ n与自动换行上创建一个新的行);    //使换行符您需要设定自动换行的单元格样式=真
    CellStyle CS = wb.createCellStyle();
    cs.setWrapText(真);
    cell.setCellStyle(CS);    //增加行高度以容纳两行文本
    row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));    //调整列宽以适合内容
    sheet.autoSizeColumn((短)2);    FileOutputStream中FILEOUT =新的FileOutputStream(OOXML-newlines.xlsx);
    wb.write(FILEOUT);
    fileOut.close();

I have a situation where I will be reading multiple lines and after some logic I need to write the lines in an Excel Sheet. I am using Apache POI for this purpose. However, the problem that I am facing is that, only the last line (from the loop) is being written to the Excel

Can someone please help me on this or provide some code snippet?

Thanks

解决方案

    Workbook wb = new XSSFWorkbook();   //or new HSSFWorkbook();
    Sheet sheet = wb.createSheet();

    Row row = sheet.createRow(2);
    Cell cell = row.createCell(2);
    cell.setCellValue("Use \n with word wrap on to create a new line");

    //to enable newlines you need set a cell styles with wrap=true
    CellStyle cs = wb.createCellStyle();
    cs.setWrapText(true);
    cell.setCellStyle(cs);

    //increase row height to accomodate two lines of text
    row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));

    //adjust column width to fit the content
    sheet.autoSizeColumn((short)2);

    FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
    wb.write(fileOut);
    fileOut.close();

这篇关于在Excel中无法写入多行使用POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆