-
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
更多相关内容 -
如何通过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).
-
word的宏_vba统一设置表格宽度
2017-12-05 11:35:09近来需要编辑一个文档,其中有一个问题,就是把表格都设置为100宽,因为表格很多,处理很麻烦,于是就打算学下vba,把表格处理好. 把内容存下来用于后续参考。宏的简单操作宏一个实用操作就是 录用-> 执行, 比如,...近来需要编辑一个文档,其中有一个问题,就是把表格都设置为100宽,因为表格很多,处理很麻烦,于是就打算学下vba,把表格处理好.
把内容存下来用于后续参考。宏的简单操作
宏一个实用操作就是 录用-> 执行,
比如,设置ctrl+shift+b 就自动 插入一个只有一列的表格,具体可参考:
https://jingyan.baidu.com/article/ea24bc39ba09dcda62b331fa.html查看录制宏的vba脚本
视图->宏->查看宏
->编辑
Sub 宏2() ' ' 宏2 宏 ' ' ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=8, NumColumns:= _ 1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ wdAutoFitFixed With Selection.Tables(1) If .Style <> "网格型" Then .Style = "网格型" End If .ApplyStyleHeadingRows = True .ApplyStyleLastRow = False .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = False .ApplyStyleRowBands = True .ApplyStyleColumnBands = False End With End Sub
上面的内容如果没怎么了解语法,可能看不懂,但更多都是table对象的操作,可参考api
可查看word 相关对象的方法(感觉这个网站操作不太便捷,建议在vba的编辑界面,可视图->对象浏览器 查看相关对象.)
https://msdn.microsoft.com/zh-cn/vba/word-vba/articles/table-applystyleheadingrows-property-word自己写一个vba脚本
设置所有的table为100的脚本
Sub table_100() ' ' 宏 ' ', Dim tempTable As Table Application.ScreenUpdating = False '判断文档是否被保护 If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then MsgBox "文档已保护,此时不能选中多个表格!" Exit Sub End If '删除所有可编辑的区域 ActiveDocument.DeleteAllEditableRanges wdEditorEveryone '添加可编辑区域 For Each tempTable In ActiveDocument.Tables tempTable.Range.Editors.Add wdEditorEveryone tempTable.PreferredWidthType = wdPreferredWidthPercent tempTable.PreferredWidth = 100 Next '选中所有可编辑区域 ActiveDocument.SelectAllEditableRanges wdEditorEveryone '删除所有可编辑的区域 ActiveDocument.DeleteAllEditableRanges wdEditorEveryone Application.ScreenUpdating = True End Sub
宏->查看宏->填好名字后,创建
把上面的脚本填上去, 这个脚本 是把表格设置成 100,可把100设置成其它值,然后按 运行,即可把word中的所有表格都设置成 指定宽度.
-
java poi 生成word表格 怎么逐列设置列宽
2020-12-20 17:42:35九州编程publicstaticint[]COLUMN_WIDTHS=newint[]{1504,1504,1504,1504,1504,1504};setTableGridCol(table,COLUMN_WIDTHS);/***@Description:设置表格列宽*/publicvoidsetTableGridCol(XWPFTable...九州编程
public static int[] COLUMN_WIDTHS = new int[] {1504,1504,1504,1504,1504,1504};
setTableGridCol(table, COLUMN_WIDTHS);
/**
* @Description: 设置表格列宽
*/
public void setTableGridCol(XWPFTable table, int[] colWidths) {
CTTbl ttbl = table.getCTTbl();
CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
: ttbl.addNewTblGrid();
for (int j = 0, len = colWidths.length; j
CTTblGridCol gridCol = tblGrid.addNewGridCol();
gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
}
}
-
css 表格宽度设置_table width
2021-04-28 08:35:29对table表格宽度定义有直接table标签内使用width宽度属性定义其宽度,其余也可以经过css名堂设置界说其宽度,遵循需求可选择自身实用的界说宽度方式。一、html table标签内宽度定义我们直接在table标签内参与width... -
如何通过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插入的表格如何调整长和宽
2021-11-29 11:20:44有时,我们需要在Word插入表格,但是插入的表格默认的长宽不是自己想要的,那么自己能自定义吗?下面以最常用的极速办公speedoffice为例。 首先,选择需要调整长宽的单元格,如图: 然后,点击右侧工具栏的... -
Python-docx设置表格列宽度
2020-11-27 15:58:53设置表格列宽的方法:table.cell(row,col).width=Inches(),指定单元格列宽,同列单元格列宽相同。from docx import Documentfrom docx.shared import Inchesdocument = Document()t = document.add_table(rows=3, ... -
poi word 表格设置居中、左对齐缩进、边框、字体
2022-03-25 15:30:482.边框设置可参考POI 设置Word表格边框、表格文字水平居中 package com.gsafety.anjian.analysis.util; /** * 设置poi-tl生成嵌套子模板中的表格垂直居中 * */ import com.deepoove.poi.NiceXWPFDocument; ... -
word怎么设计表格高和宽
2021-01-12 15:22:36在word中输入文字可能大部分人都能熟练掌握,但是有时word中也需要插入表格,这样不仅能准确的表达出想要表达的意思,那么下面就由学习啦小编给大家分享下技巧,希望能帮助到您。word设计表格高和宽的步骤如下:步骤... -
Python-docx,如何在表格中设置单元格宽度?
2021-07-16 13:11:39The problem is that Word ignores it. Other clients, like LibreOffice, respect the column width setting. A .docx file is in XML format (hence the 'x' suffix in the file extension). The XML vocabulary ... -
HTML表格的大小怎么设置
2021-06-29 03:21:32或者第一页要求纵排,第二页要求横排,第三页又要求纵排,第四页之后要求全部横排等,也就是同一文档或要求设置不同的纸张大小、或要求设置不同的页边距、纵页或横页;所有这些要求在 Word 的页面设... -
CSS设置表格TD宽度布局
2021-08-04 07:22:06使用表格布局时,对单元格的宽度控制很伤脑筋,所以查阅资料整理如下:一...fixed :表格和列的宽度通过表格的宽度来设置,某一列的宽度仅由该列首行的单元格决定;在当前列中,该单元格所在行之后的行并不会影响整... -
Word表格斜线怎么弄?这里有三种方法很实用
2021-07-26 02:02:38大家应该都知道Excel表格斜线怎么弄,那么Word表格斜线怎么弄吗?今天呢小编就帮大家总结了三种方法哦,有需要的小伙伴不妨试试这三种方法。![]... -
三种方法解决html中表格宽度和高度对齐问题
2021-04-13 12:15:34很多程序员在做网站的时候,都会遇到表格不能对齐的问题...详细地看了html中表格标签table的高度和宽度设置的细节,现总结如下:1、table中的width和height设置及其作用:table中设置的height其实是设置个最小值,也... -
html – 最大宽度不适用于IE7的表格
2021-06-28 09:23:06最大宽度在其他元素上可用,并且似乎工作正常,但是当应用于表时,我不能使它做任何事情,即简单地忽略最大宽度规则.这是代码:FooView7/20/2011James Smith345 NW 23rd Ave Gainesville,FL 32606Alachua Blah blah sf ... -
poi-tl导出word;自定义列表序号和表格宽度,表格合并,自定义标题,更新目录
2021-12-16 14:55:38poi导出word 合并自定义表格,自定义标题,更新目录 -
div中有表格,超出了div的宽度
2022-04-08 16:58:59固定宽度div中有一个table,table设置宽度100%,但是发现table中的内容有一部分被隐藏掉了,实际内容宽度超过了div的宽度 实际效果图:表格内容超出了div的宽度,但是超出部分是被隐藏的 期望效果图: 表格关键部分... -
html 表格中如何隐藏超出表格长度的字型
2021-06-11 03:39:32表格中如何隐藏超出表格长度的字型选定单元格,在水平标尺上会出现左缩排和右缩排滑块,拖动一下,使其对齐表格线内就行子。中如何隐藏表格的某一条边框表格边框的显示与隐藏,是可以用frame引数来控制的。请注意... -
Word表格跨页断行如何排版?Word表格换页脱节怎么办?
2020-12-08 00:43:09使用Word处理表格经常会因为跨页断行而让用户阅读文章很不方便,使用Word文件建立一些合同文件的时候用户也会...关于Word表格的跨页断行属性的解释1、禁止断开表格出现在不同的页面上页面排版时,由于该表格上方出... -
ms-word – Python-docx,如何在表中设置单元格宽度?
2020-11-27 15:58:54简答:单独设置单元格宽度.for cell in table_columns[0].cells:cell.width = Inches(0.5)设置列宽时,python-docx执行您要求它执行的操作.问题是Word忽略了它.其他客户端(如LibreOffice)尊重列宽设置..docx文件是XML... -
Python-docx 读写 Word 文档:插入图片、表格,设置表格样式,章节,页眉页脚等
2020-12-01 07:33:22Python-docx 模块读写 Word 文档基础(二):图片、表格,表格样式,章节设置,页眉页脚等前言:1、插入图片、设置大小:2、插入表格、设置格式:3、设置章节、页面设置等:4、设置页眉页脚结尾:【Python与Office】... -
HTML页面自适应宽度的table表格
2021-06-10 07:05:08这篇文章主要介绍了HTML页面自适应宽度的table(表格),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 将所有列设置为固定宽度,显然是不能满足... -
td无法控制表格宽度,链接超过长度不换行的解决办法
2020-09-17 16:46:43table { table-layout:fixed; WORD-BREAK:break-all; } 锁死TD宽度 table-layout:fixed;...超过宽度自动换行 ...WORD-BREAK:break-all;...在table中设置 style="table-layout:fixed;word-break:break-all; -
DataTables固定表格宽度(设置横向滚动条)
2019-10-06 18:09:27当表格的列比较多的时候,可能就需要固定表格的宽度了,默认的100%宽已经不适应了。默认的100%宽要实现改变窗口大小也100%的话,在table元素上添加width="100%",至于css的100%... -
极速office(Word)插入的表格如何调整长和宽
2021-11-29 11:34:47有时,我们需要在Word插入表格,但是插入的表格默认的长宽不是自己想要的,那么自己能自定义吗?下面以最常用的极速办公极速office为例。 首先,选择需要调整长宽的单元格,如图: 然后,点击右侧工具栏的“表... -
选中Word中全部表格和修改表格列宽
2019-01-28 09:23:341.背景:当word中有很多表格需要修改时,逐个修改既费时又可能有遗漏。如果能同时选中所有表格,对所有表格同步进行修改,就能大大提高效率。 2.方法:通过Word中的宏命令,选中全部表格,并修改表格列宽。 3.选中... -
C#语言创建Word文件并设置格式
2022-02-28 09:52:26using System; using System.Collections.Generic; using System.Linq;...using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; namespace CSharpWord { class Program ... -
Python-docx读写Word文档(插入图片、表格,设置表格样式,章节,页眉页脚)
2021-02-09 12:07:38前言:上一篇博客介绍了 python-docx 模块如何 创建 word 文档、设置段落格式、字体格式等 ,本篇博客将对在日常使用 word 文档的其他操作进行介绍。主要内容有:1、插入图片、设置图片大小;2、插入表格、设置表格... -
word转换成XML表格高度、空格属性
2016-08-19 13:06:04word转换成XML表格高度属性 1、高度属性 2、空格属性如下