java文档开发doc表格的列_Word管理控件Spire.Doc for Java使用指南:让Word表格的列宽自适应内容..._张浩捷的博客-CSDN博客

Spire.Doc 能够非常完美的识别中文字符并支持丰富的word文档元素:文本框、页眉、页脚、项目符号和编号、表格、文本、超链接、水印、图片、形状等。

本文介绍如何使用Spire.Doc for Java让Word表格的列宽自适应内容,或者让Word表格的宽度自适应页面宽度,或者固定表格的列宽。如果你还没有使用过Spire.DOC for Java,可点击此处下载最新版测试。

列宽自适应内容

import com.spire.doc.*;

import com.spire.doc.fields.TextRange;

public class AutofitToContent {

public static void main(String[] args) {

//创建Document对象

Document document = new Document();

//添加节

Section section = document.addSection();

//添加表格

Table table = section.addTable();

//设置行数和列数

table.resetCells(3, 2);

//添加内容到单元格并设置字体

TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(0, 1).addParagraph().appendText("名称");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 0).addParagraph().appendText("T1052");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 0).addParagraph().appendText("T1062");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");

textRange.getCharacterFormat().setFontName("宋体");

//列宽自动适应内容

table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

//保存文档

document.saveToFile("AutofitToContent.docx", FileFormat.Docx);

}

}

执行结果如下图:

b9f0180150f48bf614ee11eeef50665d.png

表格自适应页面宽度

import com.spire.doc.*;

import com.spire.doc.fields.TextRange;

public class AutofitToWindow {

public static void main(String[] args) {

//创建Document对象

Document document = new Document();

//添加节

Section section = document.addSection();

//添加表格

Table table = section.addTable();

//设置行数和列数

table.resetCells(3, 2);

//添加内容到单元格并设置字体

TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(0, 1).addParagraph().appendText("名称");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 0).addParagraph().appendText("T1052");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 0).addParagraph().appendText("T1062");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");

textRange.getCharacterFormat().setFontName("宋体");

//表格自适应页面宽度

table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

//保存文档

document.saveToFile("AutofitToWindow.docx", FileFormat.Docx);

}

}

75a59ceb01bcfdcebdbe170bfd20c9ec.png

固定列宽

import com.spire.doc.*;

import com.spire.doc.fields.TextRange;

public class FixColumnWidths {

public static void main(String[] args) {

//创建Document对象

Document document = new Document();

//添加节

Section section = document.addSection();

//添加表格

Table table = section.addTable();

//设置行数和列数

table.resetCells(3, 2);

//添加内容到单元格并设置字体

TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(0, 1).addParagraph().appendText("名称");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 0).addParagraph().appendText("T1052");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 0).addParagraph().appendText("T1062");

textRange.getCharacterFormat().setFontName("宋体");

textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");

textRange.getCharacterFormat().setFontName("宋体");

//设置列宽

for (int i = 0; i < table.getRows().getCount(); i++) { table.get(i,0).setCellWidth(80f,CellWidthType.Point); table.get(i,1).setCellWidth(160f,CellWidthType.Point); } //固定列宽 table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths); //保存文档 document.saveToFile("FixColumnWidths.docx", FileFormat.Docx); } }

cda19f890c7c9910ef0670ed54d0ce6a.png

*这么优秀的国产工具怎能错过呢!想要询价购买正版授权的朋友可以咨询在线客服哦~

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果

a6e1590ae4b228073faff3806334194e.png0

好文不易,鼓励一下吧!


原网址: 访问
创建于: 2021-04-01 14:15:04
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论