[英]Apache-poi Java: How can I change the font name and size of a list numbering in a WORD document?
[英]How to auto fit table and aligning the table to center, according to Word Document Size Apache-POI?
當列大小增加時,如何使用apache-poi並將該表對齊到中心,使此表自動適應文檔頁面寬度。
此代碼生成一個單詞Document,將數據從Java提取到位於c盤中的word文件。 我有手動設置寬度,但它現在工作正常。 如果提供適當的指導,如果對我有價值
public Word2Doc() throws Exception {
System.out.println("This is Word To Document Class");
File file = null;
FileOutputStream fos = null;
XWPFDocument document = null;
XWPFParagraph para = null;
XWPFRun run = null;
try {
// Create the first paragraph and set it's text.
document = new XWPFDocument();
para = document.createParagraph();
para.setAlignment(ParagraphAlignment.CENTER);
para.setSpacingAfter(100);
para.setSpacingAfterLines(10);
run = para.createRun();
for(int i=1; i<=5; i++)
run.setText("Test Name \009\009\009 Value \t\t\t\t Normal Ranges\013\013");
run.addBreak(); // similar to new line
run.addBreak();
XWPFTable table = document.createTable(4, 3);
table.setRowBandSize(1);
table.setWidth(1);
table.setColBandSize(1);
table.setCellMargins(1, 1, 100, 30);
table.setStyleID("finest");
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
table.getRow(2).getCell(1).setText("fine");
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
p1.setAlignment(ParagraphAlignment.CENTER);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("Test Name");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
//Locating the cell values
table.getRow(0).getCell(1).setText("Value");
table.getRow(0).getCell(2).setText("Normal Ranges");
table.getRow(2).getCell(2).setText("numeric values");
table.setWidth(120);
file = new File("c:\\nwhpe.docx");
if(file.exists())
file.delete();
FileOutputStream out = new FileOutputStream(file);
document.write(out);
out.close();
}
}
public static void main(String ar[]) throws Exception{
new Word2Doc();
}
}
要將表設置為自動調整,基本上只是將寬度擴展到特定大小。 例
CTTbl table = poiTable.getCTTbl();
CTTblPr pr = table.getTblPr();
CTTblWidth tblW = pr.getTblW();
tblW.setW(BigInteger.valueOf(5000));
tblW.setType(STTblWidth.PCT);
pr.setTblW(tblW);
table.setTblPr(pr);
至於將桌子與中心對齊
CTJc jc = pr.addNewJc();
jc.setVal(STJc.RIGHT);
pr.setJc(jc);
要設置表格的寬度:
XWPFTable table = doc.createTable(nRows, nCols);
table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000));
設置列寬:
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
如果單元格中的數據增加,它會影響列的寬度,即列寬也會增加,所以為了避免這種問題,我們需要檢查單元格中字符串值的長度,我們需要在字符串中添加\\ n(新行)我們可以設置列的寬度。
在我的項目中,我已經努力設置列寬,我已經通過控制單元格中的數據解決了這個問題。
您可以設置表格的寬度,以實際頁面大小的百分比表示。 首先,獲取頁面的寬度:
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
CTPageSz pageSize = sectPr.getPgSz();
double pageWidth = pageSize.getW().doubleValue();
第二步,獲取頁面的邊距並計算有效頁面大小:
CTPageMar pageMargin = sectPr.getPgMar();
double pageMarginLeft = pageMargin.getLeft().doubleValue();
double pageMarginRight = pageMargin.getRight().doubleValue();
double effectivePageWidth = pageWidth - pageMarginLeft - pageMarginRight;
現在,假設您希望表格的寬度為有效頁面大小的60%。 然后計算表格的大小。 (如果您希望表格符合實際頁面大小,只需使用1.0):
double widthInPercent = 0.6;
int size = (int) (effectivePageWidth * widthInPercent);
最后,將此大小設置為您的表:
CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
width.setType(STTblWidth.DXA);
width.setW(new BigInteger(size + ""));
希望這可以幫助!
最好,霍爾格
只需使用“100%”:
package io.github.baijifeilong.excel;
import lombok.SneakyThrows;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import java.io.FileOutputStream;
/**
* Created by BaiJiFeiLong@gmail.com at 2019-08-21 13:49
*/
public class WordDemo {
@SneakyThrows
public static void main(String[] args) {
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable(3, 4);
table.setWidth("100%");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
table.getRow(i).getCell(j).setText(String.format("%d:%d", i + i, j + 1));
}
}
document.write(new FileOutputStream("hello.docx"));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.