本文介绍如何使用Spire.Doc for Java让Word表格的列宽自适应内容,或者让Word表格的宽度自适应页面宽度,或者固定表格的列宽。
列宽自适应内容
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);
    }
}
表格自适应页面宽度
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);
    }
}
固定列宽
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);
    }
}
 



 
					



