Apache Poi:溢出的文字

发布于 2025-01-28 05:41:35 字数 1534 浏览 3 评论 0 原文

我正在使用Apache POI 4.1.2编写XLSX文件。

我想确保每个文本都会溢出其单元格。 该图显示了所需的结果。

我的代码:

package poisamples;

import java.io.FileOutputStream;
import java.io.IOException;

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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSFTxt {

  public static void main(String[] args) throws IOException {
    String txt = "Random txt, random txt, random txt";
    String outputFile = "exampleFileTxt.xlsx";
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Txt");

    sheet.setColumnWidth(1, 15);
    Row row = sheet.getRow(1);
    if (row == null) {
      row = sheet.createRow(1);
    }
    Cell cell = row.getCell(1);
    if (cell == null) {
      cell = row.createCell(1);
    }
    CellStyle newCellStyle = workbook.createCellStyle();
    newCellStyle.setWrapText(true);
    cell.setCellStyle(newCellStyle);
    cell.setCellValue(txt);

    try {
      FileOutputStream fos = new FileOutputStream(outputFile);
      workbook.write(fos);
      fos.close();
    }
    catch (IOException ioe) {
      System.out.println("Error");
    }
    workbook.close();
  }
}

“理想的结果”

我正在看文档,但我找不到任何可以帮助我的文档。 有小费吗?

I'm using apache poi 4.1.2 to write an xlsx file.

I would like to be sure that every text overflows its cell.
The image shows the desired result.

My code:

package poisamples;

import java.io.FileOutputStream;
import java.io.IOException;

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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSFTxt {

  public static void main(String[] args) throws IOException {
    String txt = "Random txt, random txt, random txt";
    String outputFile = "exampleFileTxt.xlsx";
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Txt");

    sheet.setColumnWidth(1, 15);
    Row row = sheet.getRow(1);
    if (row == null) {
      row = sheet.createRow(1);
    }
    Cell cell = row.getCell(1);
    if (cell == null) {
      cell = row.createCell(1);
    }
    CellStyle newCellStyle = workbook.createCellStyle();
    newCellStyle.setWrapText(true);
    cell.setCellStyle(newCellStyle);
    cell.setCellValue(txt);

    try {
      FileOutputStream fos = new FileOutputStream(outputFile);
      workbook.write(fos);
      fos.close();
    }
    catch (IOException ioe) {
      System.out.println("Error");
    }
    workbook.close();
  }
}

Desired result

I'm looking at the documentation, but I can't find anything to help me.
Any tip?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2025-02-04 05:41:35

,它以字符宽度的1/256单位设置宽度。因此,宽度为256表示一个字符的宽度。您的15宽度太小。因此,该列的有效宽度为0个字符,并被隐藏。

进行

...
sheet.setColumnWidth(1, 256);
...

所以在您的代码中

。此外.setWrapText(true)表示通过在多行上显示所有内容,在此单元格中可见。这与溢出细胞边框完全相反。因此,如果您希望它溢出单元边框,请不要将包装文本设置为TRUE。

...
CellStyle newCellStyle = workbook.createCellStyle();
//newCellStyle.setWrapText(true);
cell.setCellStyle(newCellStyle);
...

As documented in Sheet.setColumnWidth, it sets the width in units of 1/256th of a character width. So a width of 256 means a width of one character. Your width of 15 is too small. So the column has a effective width of 0 characters and is hidden.

So do

...
sheet.setColumnWidth(1, 256);
...

in your code.

Moreover the setting of CellStyle.setWrapText(true) means all content is visible within this cell by displaying it on multiple lines. This is the exact opposite of overflow the cell border. So if you want it to overflow the cell border, don't set wrap text true.

...
CellStyle newCellStyle = workbook.createCellStyle();
//newCellStyle.setWrapText(true);
cell.setCellStyle(newCellStyle);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文