翻譯|使用教程|編輯:李顯亮|2020-08-10 10:38:23.927|閱讀 1926 次
概述:本文介紹如何使用Spire.Doc for Java讓Word表格的列寬自適應內(nèi)容,或者讓Word表格的寬度自適應頁面寬度,或者固定表格的列寬。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Spire.Doc 能夠非常完美的識別中文字符并支持豐富的word文檔元素:文本框、頁眉、頁腳、項目符號和編號、表格、文本、超鏈接、水印、圖片、形狀等。
本文介紹如何使用Spire.Doc for Java讓Word表格的列寬自適應內(nèi)容,或者讓Word表格的寬度自適應頁面寬度,或者固定表格的列寬。如果你還沒有使用過Spire.DOC for Java,可點擊此處下載最新版測試。
列寬自適應內(nèi)容
import com.spire.doc.*; import com.spire.doc.fields.TextRange; public class AutofitToContent { public static void main(String[] args) { //創(chuàng)建Document對象 Document document = new Document(); //添加節(jié) Section section = document.addSection(); //添加表格 Table table = section.addTable(); //設(shè)置行數(shù)和列數(shù) table.resetCells(3, 2); //添加內(nèi)容到單元格并設(shè)置字體 TextRange textRange = table.get(0, 0).addParagraph().appendText("產(chǎn)品編號"); 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("宋體"); //列寬自動適應內(nèi)容 table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents); //保存文檔 document.saveToFile("AutofitToContent.docx", FileFormat.Docx); } }
執(zhí)行結(jié)果如下圖:
表格自適應頁面寬度
import com.spire.doc.*; import com.spire.doc.fields.TextRange; public class AutofitToWindow { public static void main(String[] args) { //創(chuàng)建Document對象 Document document = new Document(); //添加節(jié) Section section = document.addSection(); //添加表格 Table table = section.addTable(); //設(shè)置行數(shù)和列數(shù) table.resetCells(3, 2); //添加內(nèi)容到單元格并設(shè)置字體 TextRange textRange = table.get(0, 0).addParagraph().appendText("產(chǎn)品編號"); 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) { //創(chuàng)建Document對象 Document document = new Document(); //添加節(jié) Section section = document.addSection(); //添加表格 Table table = section.addTable(); //設(shè)置行數(shù)和列數(shù) table.resetCells(3, 2); //添加內(nèi)容到單元格并設(shè)置字體 TextRange textRange = table.get(0, 0).addParagraph().appendText("產(chǎn)品編號"); 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("宋體"); //設(shè)置列寬 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); } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn