phpword如何单独设置word边框颜色怎么设置

[简单]poi word2007设置表格边框样式
时间: 02:17:56
&&&& 阅读:643
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&??????? 直接上代码:
import java.io.FileOutputS
import java.math.BigI
import java.util.ArrayL
import java.util.L
import org.apache.poi.xwpf.usermodel.BreakT
import org.apache.poi.xwpf.usermodel.XWPFD
import org.apache.poi.xwpf.usermodel.XWPFP
import org.apache.poi.xwpf.usermodel.XWPFT
import org.apache.poi.xwpf.usermodel.XWPFTableC
import org.apache.poi.xwpf.usermodel.XWPFTableR
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTB
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTS
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTT
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblB
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblW
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STB
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STM
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblW
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
public class POI_表格边框相关_S2_Test {
public static void main(String[] args) throws Exception {
POI_表格边框相关_S2_Test t=new POI_表格边框相关_S2_Test();
XWPFDocument document = new XWPFDocument();
t.createSimpleTableWithBdColor(document);
t.addNewPage(document, BreakType.TEXT_WRAPPING);
t.createSimpleTableNormal(document);
t.addNewPage(document, BreakType.TEXT_WRAPPING);
t.createSimpleTableWithNotBd(document);
t.saveDocument(document, "f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx");
//表格自定义边框 请忽略这么丑的颜色样式,主要说明可以自定义样式
void createSimpleTableWithBdColor(XWPFDocument doc) throws Exception {
List&String& columnList = new ArrayList&String&();
columnList.add("序号");
columnList.add("姓名信息|姓甚|名谁");
columnList.add("名刺信息|籍贯|营生");
XWPFTable table = doc.createTable(2,5);
CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
CTBorder hBorder=borders.addNewInsideH();
hBorder.setVal(STBorder.Enum.forString("dashed"));
hBorder.setSz(new BigInteger("1"));
hBorder.setColor("0000FF");
CTBorder vBorder=borders.addNewInsideV();
vBorder.setVal(STBorder.Enum.forString("dotted"));
vBorder.setSz(new BigInteger("1"));
vBorder.setColor("00FF00");
CTBorder lBorder=borders.addNewLeft();
lBorder.setVal(STBorder.Enum.forString("double"));
lBorder.setSz(new BigInteger("1"));
lBorder.setColor("3399FF");
CTBorder rBorder=borders.addNewRight();
rBorder.setVal(STBorder.Enum.forString("single"));
rBorder.setSz(new BigInteger("1"));
rBorder.setColor("F2B11F");
CTBorder tBorder=borders.addNewTop();
tBorder.setVal(STBorder.Enum.forString("thick"));
tBorder.setSz(new BigInteger("1"));
tBorder.setColor("C3599D");
CTBorder bBorder=borders.addNewBottom();
bBorder.setVal(STBorder.Enum.forString("wave"));
bBorder.setSz(new BigInteger("1"));
bBorder.setColor("BF6BCC");
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
CTJc cTJc=tblPr.addNewJc();
cTJc.setVal(STJc.Enum.forString("center"));
tblWidth.setW(new BigInteger("8000"));
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow firstRow=
XWPFTableRow secondRow=
XWPFTableCell firstCell=
XWPFTableCell secondCell=
for(int i=0;i&2;i++){
firstRow=table.getRow(i);
firstRow.setHeight(380);
for(int j=0;j&5;j++){
firstCell=firstRow.getCell(j);
setCellText(firstCell, "测试", "FFFFC9", 1600);
firstRow=table.insertNewTableRow(0);
secondRow=table.insertNewTableRow(1);
firstRow.setHeight(380);
secondRow.setHeight(380);
for(String str:columnList){
if(str.indexOf("|") == -1){
firstCell=firstRow.addNewTableCell();
secondCell=secondRow.addNewTableCell();
createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);
createVSpanCell(secondCell, "", "CCCCCC", 1600,null);
String[] strArr=str.split("\\|");
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);
for(int i=1;i&strArr.length-1;i++){
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, "","CCCCCC",1600,null);
for(int i=1;i&strArr.i++){
secondCell=secondRow.addNewTableCell();
setCellText(secondCell, strArr[i], "CCCCCC", 1600);
//表格正常边框
void createSimpleTableNormal(XWPFDocument doc) throws Exception {
List&String& columnList = new ArrayList&String&();
columnList.add("序号");
columnList.add("姓名信息|姓甚|名谁");
columnList.add("名刺信息|籍贯|营生");
XWPFTable table = doc.createTable(2,5);
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
CTJc cTJc=tblPr.addNewJc();
cTJc.setVal(STJc.Enum.forString("center"));
tblWidth.setW(new BigInteger("8000"));
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow firstRow=
XWPFTableRow secondRow=
XWPFTableCell firstCell=
XWPFTableCell secondCell=
for(int i=0;i&2;i++){
firstRow=table.getRow(i);
firstRow.setHeight(380);
for(int j=0;j&5;j++){
firstCell=firstRow.getCell(j);
setCellText(firstCell, "测试", "FFFFC9", 1600);
firstRow=table.insertNewTableRow(0);
secondRow=table.insertNewTableRow(1);
firstRow.setHeight(380);
secondRow.setHeight(380);
for(String str:columnList){
if(str.indexOf("|") == -1){
firstCell=firstRow.addNewTableCell();
secondCell=secondRow.addNewTableCell();
createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);
createVSpanCell(secondCell, "", "CCCCCC", 1600,null);
String[] strArr=str.split("\\|");
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);
for(int i=1;i&strArr.length-1;i++){
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, "","CCCCCC",1600,null);
for(int i=1;i&strArr.i++){
secondCell=secondRow.addNewTableCell();
setCellText(secondCell, strArr[i], "CCCCCC", 1600);
//表格无边框
void createSimpleTableWithNotBd(XWPFDocument doc) throws Exception {
List&String& columnList = new ArrayList&String&();
columnList.add("序号");
columnList.add("姓名信息|姓甚|名谁");
columnList.add("名刺信息|籍贯|营生");
XWPFTable table = doc.createTable(2,5);
CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
CTBorder hBorder=borders.addNewInsideH();
hBorder.setVal(STBorder.Enum.forString("none"));
hBorder.setSz(new BigInteger("1"));
hBorder.setColor("0000FF");
CTBorder vBorder=borders.addNewInsideV();
vBorder.setVal(STBorder.Enum.forString("none"));
vBorder.setSz(new BigInteger("1"));
vBorder.setColor("00FF00");
CTBorder lBorder=borders.addNewLeft();
lBorder.setVal(STBorder.Enum.forString("none"));
lBorder.setSz(new BigInteger("1"));
lBorder.setColor("3399FF");
CTBorder rBorder=borders.addNewRight();
rBorder.setVal(STBorder.Enum.forString("none"));
rBorder.setSz(new BigInteger("1"));
rBorder.setColor("F2B11F");
CTBorder tBorder=borders.addNewTop();
tBorder.setVal(STBorder.Enum.forString("none"));
tBorder.setSz(new BigInteger("1"));
tBorder.setColor("C3599D");
CTBorder bBorder=borders.addNewBottom();
bBorder.setVal(STBorder.Enum.forString("none"));
bBorder.setSz(new BigInteger("1"));
bBorder.setColor("F7E415");
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
CTJc cTJc=tblPr.addNewJc();
cTJc.setVal(STJc.Enum.forString("center"));
tblWidth.setW(new BigInteger("8000"));
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow firstRow=
XWPFTableRow secondRow=
XWPFTableCell firstCell=
XWPFTableCell secondCell=
for(int i=0;i&2;i++){
firstRow=table.getRow(i);
firstRow.setHeight(380);
for(int j=0;j&5;j++){
firstCell=firstRow.getCell(j);
setCellText(firstCell, "测试", "FFFFC9", 1600);
firstRow=table.insertNewTableRow(0);
secondRow=table.insertNewTableRow(1);
firstRow.setHeight(380);
secondRow.setHeight(380);
for(String str:columnList){
if(str.indexOf("|") == -1){
firstCell=firstRow.addNewTableCell();
secondCell=secondRow.addNewTableCell();
createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);
createVSpanCell(secondCell, "", "CCCCCC", 1600,null);
String[] strArr=str.split("\\|");
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);
for(int i=1;i&strArr.length-1;i++){
firstCell=firstRow.addNewTableCell();
createHSpanCell(firstCell, "","CCCCCC",1600,null);
for(int i=1;i&strArr.i++){
secondCell=secondRow.addNewTableCell();
setCellText(secondCell, strArr[i], "CCCCCC", 1600);
void setCellText(XWPFTableCell cell,String text, String bgcolor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
//cell.setColor(bgcolor);
CTTcPr ctPr = cttc.addNewTcPr();
CTShd ctshd = ctPr.addNewShd();
ctshd.setFill(bgcolor);
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cell.setText(text);
public void createHSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
cell.setColor(bgcolor);
cellPr.addNewHMerge().setVal(stMerge);
cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);
public void createVSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
cell.setColor(bgcolor);
cellPr.addNewVMerge().setVal(stMerge);
cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);
public void addNewPage(XWPFDocument document,BreakType breakType){
XWPFParagraph xp = document.createParagraph();
xp.createRun().addBreak(breakType);
public void saveDocument(XWPFDocument document,String savePath) throws Exception{
FileOutputStream fos = new FileOutputStream(savePath);
document.write(fos);
fos.close();
??? 结果如下:
??? ??? 有关单元格合并的内容,可以参考我的另一篇文章:
??? 全文完。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:http://oycg.iteye.com/blog/2152008
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!php如何实现word文档上传_php吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:151,763贴子:
php如何实现word文档上传收藏
php实现word文档上传,求代码,谢谢了,很着急用
php,签约有保障,提供推荐到大企业实习,五重保障成就PHP开发梦.免费申请试学,课后1对1辅导,毕业学员薪资高于普通程序员2000元.
PHP怎么上传图片。就怎么上传word文档。代码是一样的。关键函数:move_upload_file()
&html&&body&&form action="upload_file.php" method="post"
enctype="multipart/form-data"&&label for="file"&Filename:&/label& &input
type="file" name="file" id="file" /& &br /&&input type="submit" name="submit" value="Submit" /&&/form&&/body&&/html&
&?phpif ((($_FILES ["file"] ["type"] == "image/gif") || ($_FILES ["file"] ["type"] == "image/jpeg") || ($_FILES ["file"] ["type"] == "image/pjpeg")) && ($_FILES ["file"] ["size"] & 20000)) {
if ($_FILES ["file"] ["error"] & 0) {
echo "Return Code: " . $_FILES ["file"] ["error"] . "&br /&";
echo "Upload: " . $_FILES ["file"] ["name"] . "&br /&";
echo "Type: " . $_FILES ["file"] ["type"] . "&br /&";
echo "Size: " . ($_FILES ["file"] ["size"] / 1024) . " Kb&br /&";
echo "Temp file: " . $_FILES ["file"] ["tmp_name"] . "&br /&";
if (file_exists ( "upload/" . $_FILES ["file"] ["name"] )) {
echo $_FILES ["file"] ["name"] . " already exists. ";
move_uploaded_file ( $_FILES ["file"] ["tmp_name"], "upload/" . $_FILES ["file"] ["name"] );
echo "Stored in: " . "upload/" . $_FILES ["file"] ["name"];
echo "Invalid file";}?&
$_FILES 多维数组的应用......
登录百度帐号PHP怎么编辑Word - PHP当前位置:& &&&PHP怎么编辑WordPHP怎么编辑Wordwww.MyException.Cn&&网友分享于:&&浏览:36次PHP如何编辑Word?
如图所示,已有Word模板,希望用PHP调用该Word模板,将相关栏目的内容填写完后生成新的文档并保存至当前目录,该如何实现?
------解决方案--------------------http://phpword.codeplex.com/
这儿有完整的源代码,包括实例和文档
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有PHPWord中文手册_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
PHPWord中文手册
&&我翻译的phpword的开发者手册,给大家提供点参考
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
还剩30页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢当前位置:
PHP&Word&PHPWord&模板
本文介绍PHP中使用PHPWord组件来操作Word文档模板,通过读取一个已设置好的Word文档模板,来替换掉模板中的部分指定部分,从而实现输出一个新的Word文档。
先看使用PHPWord的模板操作功能的例子,我使用的是ThinkPHP框架,使用Vendor来包含类。Word文档的模板文件放在Res目录下,生成的新的Word文档放到/Upload/m/目录下。
//使用Vendor包含PHPWord类
Vendor('PHPWord.PHPWord');
//初始化PHPWord对象
$PHPWord = new PHPWord();
//载入模板文件
$document = $PHPWord-&loadTemplate('./Res/template1.docx');
//替换Word模板文件中的一处内容,这里如果要显示中文,则必须使用iconv函数
$document-&setValue('name', iconv('utf-8', 'GB2312//IGNORE', $oMember['m_real_name']));
$document-&setValue('penName', iconv('utf-8', 'GB2312//IGNORE',$oMember['m_pen_name']));
//替换Word模板文件中的一处内容,不使用中文的setValue
$document-&setValue('email', $oMember['m_email']);
$filename = '/Upload/m/m-i-'.time().'.docx';
//保存Word文档
$document-&save('.'.$filename);
//输出下载Word文档
header("Location: ".$filename);
从上面的例子可以看到,使用PHPWord的模板功能,主要流程如下:
1、包含PHPWord
2、初始化PHPWord对象
3、载入模板
4、替换模板中的指定串
6、输出下载(这一步是可选的)
关于PHP使用PHPWord组件操作Word文档模板,本文就介绍这么多,希望对您有所帮助!
文章的版权
洪哥写文章很苦逼,如果本文对您略有帮助,点击感兴趣的广告支持洪哥!万分感谢!本文属于“洪哥笔记”原创文章,转载请注明来源地址:
关注与收藏
如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D!
&&( 7:40:06)
&&( 7:43:22)
&&( 7:33:43)
&&( 7:04:09)
&&( 7:02:40)
&&( 7:31:57)
&&( 9:08:19)
&&( 7:05:58)
&&( 7:48:14)
&&( 7:30:07)
热门点击文章
洪哥推荐文章
友情链接:}

我要回帖

更多关于 word边框颜色怎么设置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信