简体   繁体   English

如何使用 Apache poi HSSF 库自动将数据拆分为多个单元格?

[英]How automatically split data into multiple cells using Apache poi HSSF libraries?

I have a variable which is more than 32767 characters long and when I'm trying to write that data into a single cell using Apache poi HSSF libraries I'm getting below error我有一个长度超过 32767 个字符的变量,当我尝试使用 Apache poi HSSF 库将该数据写入单个单元格时,出现以下错误错误

How can I automatically split that data into multiple cells column wise ie my row should remain the same but data will be auto split into multiple column on the same row using Apache poi HSSF libraries?我如何自动将该数据按列拆分为多个单元格,即我的行应该保持不变,但数据将使用 Apache poi HSSF 库自动拆分为同一行的多个列? Thanks谢谢

It's an Excel limitation , you won't be able to overcome this.这是一个Excel 限制,你将无法克服它。

What you can do is to split the large string and save it to the neighbor cell.您可以做的是拆分大字符串并将其保存到相邻单元格中。

Example code:示例代码:

def wb = new org.apache.poi.hssf.usermodel.HSSFWorkbook()

def sheet = wb.createSheet("new sheet")

def row = sheet.createRow(0)

def variable = vars.get('your-variable-name')

def chunkSize = 32767

def parts = java.util.stream.IntStream.iterate(0, i -> i < variable.length(), i -> i + chunkSize)
        .mapToObj(i -> variable.substring(i, Math.min(variable.length(), i + chunkSize)))
        .collect(java.util.stream.Collectors.toList());

parts.eachWithIndex { entry, index ->
    def cell = row.createCell(index)
    cell.setCellValue(entry)
}

wb.write(new File('file.xls'))

More information:更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM