翻譯|使用教程|編輯:李顯亮|2020-08-10 10:38:23.927|閱讀 1926 次
概述:本文介紹如何使用Spire.Doc for Java讓Word表格的列寬自適應內容,或者讓Word表格的寬度自適應頁面寬度,或者固定表格的列寬。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
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); } }
執行結果如下圖:
表格自適應頁面寬度
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); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn