-
2020-12-20 17:42:39
poi 操作word里表格,如设置表格宽度、行高、表格样式等。
1.表格或单元格宽度:
默认TblW的type属性为STTblWidth.AUTO,即自动伸缩。所以要调整为指定类型:STTblWidth.DXA 1)表格宽:
CTTblPr tblPr = xtab2.getCTTbl().getTblPr();
tblPr.getTblW().setType(STTblWidth.DXA);
tblPr.getTblW().setW(new BigInteger("7000"));
单元格宽:
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
CTTblWidth cellw = tcpr.addNewTcW();
cellw.setType(STTblWidth.DXA);
cellw.setW(BigInteger.valueOf(360*5));
2.表格风格
注:如果不设置风格,将采用默认的Normal风格
CTTblPr tblPr = xtab2.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");
3.表格行高:获取表格行的CTTrPr.增加CTHeight属性
List rows = xtab2.getRows();
for (XWPFTableRow row : rows) {
CTTrPr trPr = row.getCtRow().addNewTrPr();
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(360));
......
}
表格行内容垂直居中:
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
4.表格单元格颜色
例如下面的标题行与奇偶行颜色设置
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
// 标题行
ctshd.setFill("A7BFDE");
}
else if (rowCt % 2 == 0) {
// even row
ctshd.setFill("D3DFEE");
}
else {
// odd row
ctshd.setFill("EDF2F8");
}
5.获取某指定位置对象并生成新的光标位置
注:这个更新或插入操作比较有用,比如更新文档目录.
XmlCursor cursor = doc.getDocument().getBody().getPArray(0).newCursor();
XWPFParagraph cP = doc.insertNewParagraph(cursor);
6.插入图片:
XWPFParagraph parapictest = document.createParagraph();
XWPFRun runtest = parapictest.createRun();
runtest.setText("图片:");
XWPFRun pictest = document.createParagraph().createRun();
XWPFPicture picture = pictest.addPicture(new FileInputStream("D://563.jpg"), Document.PICTURE_T YPE_JPEG, "D://563.jpg", 1000*360*10,1000*360*10);
————————————————
版权声明:本文为CSDN博主「garbageNewbie」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36161345/article/details/100237170
更多相关内容 -
如何设置PHPWord固定单元格宽度?
2013-11-17 11:39:12我正在尝试创建一个单元格宽度为固定宽度的表。 假设我的代码如下: <pre><code>$table->addCell(300)->addText('first'); $table->addCell(300)->addText('text that is very looooooooong');... -
如何通过JAVA增加Word文件中表格的列宽
2021-07-16 15:09:25I working on a small project where I am creating word file by Java and enter some detail in this word file.I am able to create word file and also able to enter data into it. I also write a table into ...I working on a small project where I am creating word file by Java and enter some detail in this word file.
I am able to create word file and also able to enter data into it. I also write a table into word file and enter some details.
Now what I want, I want to increase width of specific column.
Is there any way to do this? I am using Apache POI drivers for creating word file and writing data into it.
I am using below code:
XWPFDocument document= new XWPFDocument();
try{
FileOutputStream out = new FileOutputStream(
new File("d:\\createparagraph.docx"));
XWPFParagraph paragraph = document.createParagraph();
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("CLientID");
tableRowOne.addNewTableCell().setText(txtCID.getText());
//create second row
XWPFTableRow tableRow2 = table.createRow();
tableRow2.getCell(0).setText("AccountID");
tableRow2.getCell(1).setText(txtAID.getText());
document.write(out);
out.close();
}
This code working fine and generate normal table but I want to increase width of specific column (Column 2).
Please help.
解决方案
As far as I see, the column width settings are not implemented (as of POI version 3.15 final) in XWPFTable. So we must use the underlying low level objects.
Example:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import java.math.BigInteger;
public class CreateWordTableColumnWidth {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = document.createParagraph();
XWPFTable table = document.createTable(1, 2);
//values are in unit twentieths of a point (1/1440 of an inch)
table.setWidth(5*1440); //should be 5 inches width
//create CTTblGrid for this table with widths of the 2 columns.
//necessary for Libreoffice/Openoffice to accept the column widths.
//first column = 2 inches width
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
//other columns (only one in this case) = 3 inches width
for (int col = 1 ; col < 2; col++) {
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3*1440));
}
//set width for first column = 2 inches
CTTblWidth tblWidth = table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(2*1440));
//STTblWidth.DXA is used to specify width in twentieths of a point.
tblWidth.setType(STTblWidth.DXA);
//set width for second column = 3 inches
tblWidth = table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(3*1440));
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("CLientID");
tableRowOne.getCell(1).setText("CID001");
//create second row
XWPFTableRow tableRow2 = table.createRow();
tableRow2.getCell(0).setText("AccountID");
tableRow2.getCell(1).setText("ACCID001");
paragraph = document.createParagraph();
document.write(new FileOutputStream("CreateWordTableColumnWidth.docx"));
document.close();
}
}
The code is commented to describe what it does. Especially mentioned should be the special measurement unit Twip (twentieth of a point).
-
a-table设置单元格宽度,超出自动换行附效果图
2022-05-30 17:48:34设置 a-table 中 columns 的指定单元格属性,通过插槽实现文本超出指定宽度自动换行 { title: '描述', align: "center", scopedSlots: { customRender: 'description' } }, html <span slot.示例效果:
设置 a-table 中 columns 的指定单元格属性,通过插槽实现文本超出指定宽度自动换行
{ title: '描述', align: "center", scopedSlots: { customRender: 'description' } },
html
<span slot="description" slot-scope="text, record"> <div class="descriptionTxt">{{record.description}}视频上传时间视频上传时间视频上传时间视频上传时间</div> </span>
css
.descriptionTxt { width: 220px; word-wrap: break-word; padding: 2px !important; white-space: pre-wrap; }
-
Word插入的表格如何调整长和宽
2021-11-29 11:20:44首先,选择需要调整长宽的单元格,如图: 然后,点击右侧工具栏的“表设置”如图: “表设置”里面有个“单元格大小”选项,我们在“高度”“宽度”输入对应的数字即可: 以设置长宽均为1CM为例,...有时,我们需要在Word插入表格,但是插入的表格默认的长宽不是自己想要的,那么自己能自定义吗?下面以最常用的极速办公speedoffice为例。
首先,选择需要调整长宽的单元格,如图:
然后,点击右侧工具栏的“表设置”如图:
“表设置”里面有个“单元格大小”选项,我们在“高度”“宽度”输入对应的数字即可:
以设置长宽均为1CM为例,输入后效果如图:
-
如何通过JAVA增加word文件中表格的列宽
2020-12-20 17:42:33据我所知,在XWPFTable中没有实现列宽设置(从POI 3.15版开始)。所以我们必须使用底层的低级对象。例子:import java.io.FileOutputStream;import org.apache.poi.xwpf.usermodel.*;import org.openxmlformats.... -
word怎么设置单元格大小
2020-12-31 11:27:08word中的单元格是可以改变的,以适应不同的数据长度。但是新手不会,怎么办?有简单易懂的方法吗?下面让学习啦小编为你带来excel设置单元格大小的方法吧,希望看完本教程的朋友都能学会并运用起来。word表格中设置... -
excel拉长单元格_excel调整单元格大小的方法步骤详解
2021-06-28 11:12:18excel调整单元格大小的方法一1、打开一篇需要调整的Excel文档,如下图,把鼠标放在C列和D列之间的线上,此时鼠标会变成一个双箭头的图标,拖动鼠标把它调整为合适的列款;2、如果下图所示,需要调整行高,则把鼠标... -
word拆分表格宽度发生大小变化问题
2022-03-04 22:57:07第一步: 右键表格--》自动调整--》 固定列宽 第二步: 右键要拆分表格位置--》拆分表格 -
POI设置单元格的宽度和高度
2015-06-24 17:17:27POI 1.2教程 - 2.2.7 设置单元格的宽度和高度 博客分类: Java学习 POI NPOI 1.2教程 - 2.2.7 设置单元格的宽度和高度 作者:Tony Qu NPOI官方网站:http://npoi.codeplex.com/ ... -
C#实现表格单元格自动居中、宽度等格式化操作
2022-04-06 17:12:08C#中档表格数据绑定后,可以进行各种单元格数据格式处理,这里选择部分列进行演示 -
极速office(Word)插入的表格如何调整长和宽
2021-11-29 11:34:47首先,选择需要调整长宽的单元格,如图: 然后,点击右侧工具栏的“表设置”如图: “表设置”里面有个“单元格大小”选项,我们在“高度”“宽度”输入对应的数字即可: 以设置长宽均为1CM为例,... -
html中表格tr的td单元格怎么设置宽度属性
2021-04-08 09:03:56现在来看下如何设置表格td单元格的宽度。例1:Table的宽度为600px,Table的td所有宽度总和不到600px,浏览器会自动按照td的宽度的比例算出宽度我是200px我也是200px运行结果:两个td都是300px;Table的宽度为600px,前两... -
java poi如何设置word的页面的大小和水平方向?
2020-12-20 17:42:35//利用类fontfactory结合font和color可以设置各种各样字体样式 /** * font.underline 下划线,font.bold 粗体 */ paragraph underline = new paragraph("下划线的实现", fontfactory.getfont( fontfactory.... -
三种方法解决html中表格宽度和高度对齐问题
2021-04-13 12:15:34很多程序员在做网站的时候,都会遇到表格不能对齐的问题,那么,html中表格怎么设置单元格的宽度和高度?下面我们来总结一下html中表格宽度和高度对齐设置的三种方法。程序员在做网页的时候,经常会碰到表格宽度对不... -
bootstrapTable表格设置单元格宽度无效并且数据太长而不换行的解决办法
2018-09-28 10:47:13根据上图的滚动条可以发现,消费详情这列的数据的长度特别长,所以导致了页面出现了滚动条,在实际的应用中,页面水平方向使用滚动条去观察数据是较为不方便的,所以我们需要将此列的数据换行显示。 第一时间想到的... -
table宽度
2021-04-17 03:45:54题目虽然是说table的宽度,但其实最让人抓狂的是单元格td的宽度。平时开发中也经常会遇到这方面的问题,所以我找资料学习table的宽度的算法。table-layouttable-layout定义了表格布局算法,值为auto或fixed。fixed... -
JAVA使用POI-TL生成word表格列宽自定义
2022-02-11 11:06:06JAVA使用POI-TL生成word表格列宽自定义 -
活用word分散对齐
2020-12-29 09:01:24在word中有一个分散对齐,分散对齐是在左右边距之间均匀分布文本,使您的文档看起来整洁、干净。在字符和单词之间添加空格。如果最后一行短,将在字符之间添加额外空格,以使其与段落宽度匹配。下面先用一张图片来看... -
poi-tl导出word;自定义列表序号和表格宽度,表格合并,自定义标题,更新目录
2021-12-16 14:55:38poi导出word 合并自定义表格,自定义标题,更新目录 -
HTML表格的宽度怎么设置
2021-06-10 08:09:461 回答2021-05-06 浏览:3 分类:其他问题回答:其实这样的表格主要是通过跨行和跨列来实现这,首先你这里有11行,17列(按最多的算)然后...1 回答2021-05-06 浏览:1 分类:其他问题回答:CSS中宽度和高度分别可以通... -
python-docx设置表格单元格边距在Office和WPS中显示不同
2020-11-23 12:30:35造成显示不同的原因是底层使用的XML字段不同,把对应字段`left`和`right`加上即可 -
Python-docx设置表格列宽度
2020-11-27 15:58:53width_dic = {0: 2, 1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2} for col_num in range(7): t.cell(0, col_num).width = Inches(col_width_dic[col_num]) 注意:表的所有列宽度合计为10,所以在设置表格每列宽宽度时要同时... -
20191012——POI设置单元格自动行高(思路)
2021-04-15 16:00:56在经过Jxls或者POI导出数据至excel中后,发现有的单元格内容太多,既没有自动换行,也没有自动增大行高。那如何通过Java代码来实现呢?请看下面步骤:(一)首先,将excel设置为最合适的行高,通过CTRow对象的... -
如何设置excel表中的单元格大小统一
2021-04-21 13:56:51设置excel表中的单元格大小统一的方法:首先选择需要调整大小的单元格;...设置excel表中的单元格大小统一的方法:1、首先, 选择需要调整大小的单元格---单击开始命名下面的行和列命令。2、在弹出菜... -
单元格属性
2021-06-12 04:17:33值是单元格的真实值,当单元格被引用时,引用的就是单元格的真实值●举例:表达式A1+B1,其运算结果就是A1单元格的值加上B1单元格的值。显示格式单元格属性列表中第二项是显示格式,用来设置值在... -
CSS设置表格TD宽度布局
2021-08-04 07:22:06使用表格布局时,对单元格的宽度控制很伤脑筋,所以查阅资料整理如下:一...fixed :表格和列的宽度通过表格的宽度来设置,某一列的宽度仅由该列首行的单元格决定;在当前列中,该单元格所在行之后的行并不会影响整... -
table样式测试总结tr td宽度分析
2020-12-24 19:27:10题外话:一直以来习惯布局用ul,li样式调整比较方便,不会互相影响出现一些问题,but~现在公司涉及很多表格打印,都是用table写的,好多宽度高度合并啊,组合啊~~~,单元格之间互相影响,有的样式设置还不起作用。... -
关于Aspose.Cells 如何设置行高和列宽
2021-08-03 16:35:06//设置每行的高度(i表示第几行,第二參數就是高度) sheet... //上面两种是自定义高度和宽度 //下面这个方法是根据文字长度自适应高度和宽度 sheet.AutoFitColumns(); sheet.AutoFitRows(); 非常简单,直接调用方法就可。