Apache POI 文本框看起来与 excel 文本框不同
Apache POI textbox looks different from excel textbox
我想制作一个类似于上图右侧的文本框。
我尝试按照文档并编写了以下代码。
public void TestXSSFPatriarch(String fileName, String richText) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("hssf");
XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFTextBox xssfTextBox = xssfDrawing.createTextbox(
new XSSFClientAnchor(0, 0, 25, 50, (short) 0, 0, (short) 25, 50));
xssfTextBox.setText(new XSSFRichTextString(richText));
try (OutputStream fileOut = new FileOutputStream(fileName)) {
workbook.write(fileOut);
System.out.println("Success");
} catch (IOException e) {
e.printStackTrace();
}
}
上面的代码输出了上图中左边显示的文本框。
如有任何帮助,我们将不胜感激。
在尝试了 XSSFTextBox 提供的不同方法之后。我找到了一个名为 setFillColor
的选项
public void TestXSSFPatriarch(String fileName, String richText) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("hssf");
XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFTextBox xssfTextBox = xssfDrawing.createTextbox(
new XSSFClientAnchor(0, 0, 25, 50, (short) 0, 0, (short) 25, 50));
xssfTextBox.setText(new XSSFRichTextString(richText));
xssfTextBox.setFillColor(255, 255, 255); // Adding the following line solved the problem
try (OutputStream fileOut = new FileOutputStream(fileName)) {
workbook.write(fileOut);
System.out.println("Success");
} catch (IOException e) {
e.printStackTrace();
}
}
我想制作一个类似于上图右侧的文本框。 我尝试按照文档并编写了以下代码。
public void TestXSSFPatriarch(String fileName, String richText) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("hssf");
XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFTextBox xssfTextBox = xssfDrawing.createTextbox(
new XSSFClientAnchor(0, 0, 25, 50, (short) 0, 0, (short) 25, 50));
xssfTextBox.setText(new XSSFRichTextString(richText));
try (OutputStream fileOut = new FileOutputStream(fileName)) {
workbook.write(fileOut);
System.out.println("Success");
} catch (IOException e) {
e.printStackTrace();
}
}
上面的代码输出了上图中左边显示的文本框。 如有任何帮助,我们将不胜感激。
在尝试了 XSSFTextBox 提供的不同方法之后。我找到了一个名为 setFillColor
public void TestXSSFPatriarch(String fileName, String richText) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("hssf");
XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFTextBox xssfTextBox = xssfDrawing.createTextbox(
new XSSFClientAnchor(0, 0, 25, 50, (short) 0, 0, (short) 25, 50));
xssfTextBox.setText(new XSSFRichTextString(richText));
xssfTextBox.setFillColor(255, 255, 255); // Adding the following line solved the problem
try (OutputStream fileOut = new FileOutputStream(fileName)) {
workbook.write(fileOut);
System.out.println("Success");
} catch (IOException e) {
e.printStackTrace();
}
}