NPOI中设定单元格宽度怎么办

夫人的泡泡鱼 2014-10-13 11:15:19
这个文档中的第2张表格,需要设定第一行第2单元个宽度大一点,可是无论宽度值为多少,均没有效果。

 table = m_Docx.CreateTable(1, 6);//创建1行6列表
m_CTTbl = m_Docx.Document.body.GetTblArray()[1];//获得文档第二张表
m_CTTblPr = m_CTTbl.AddNewTblPr();
m_CTTblPr.AddNewTblW().w = "8500"; //表宽
m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa;
m_CTTblPr.tblpPr = new CT_TblPPr();//表定位
m_CTTblPr.tblpPr.tblpX = "0";//表左上角坐标
m_CTTblPr.tblpPr.tblpY = "0";
m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center;//若不为“Null”,则优先tblpX,即表由tblpXSpec定位
m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.Null;//若不为“Null”,则优先tblpY,即表由tblpYSpec定位
m_CTTblPr.tblpPr.leftFromText = (ulong)180;
m_CTTblPr.tblpPr.rightFromText = (ulong)180;
m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text;
m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page;

////表1行6列充值:
#region
CT_TcPr m_Pr = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
m_Pr.tcW = new CT_TblWidth();
m_Pr.tcW.w = "50";//单元格宽
m_Pr.tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(0).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(0).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "序号";


table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(1).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth();
table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.w = "8450";//单元格宽
table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(1).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "内容";

table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth();
table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr().tcW.w = "0";//单元格宽
table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(2).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(2).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "1";

table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth();
table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr().tcW.w = "0";//单元格宽
table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(3).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(3).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "2";

table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth();
table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr().tcW.w = "0";//单元格宽
table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(4).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(4).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "3";

table.GetRow(0).GetCell(5).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth();
table.GetRow(0).GetCell(5).GetCTTc().AddNewTcPr().tcW.w = "0";//单元格宽
table.GetRow(0).GetCell(5).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
table.GetRow(0).GetCell(5).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
table.GetRow(0).GetCell(5).GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
table.GetRow(0).GetCell(5).GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "4";
...全文
1800 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_33915341 2016-02-03
  • 打赏
  • 举报
回复
1440twip=25.4mm=72pt(磅point)=96px(像素pixel) 你的单位是twip,不是像素,值设置大一点。
zhangxiaomin19921 2015-12-25
  • 打赏
  • 举报
回复
这个可以用npoi的方法。 sheet.SetColumnWidth(1,20*256); 第一个参数表示从第1列开始,设置指定的宽度“20*256”,表示20px
liynet 2015-02-20
  • 打赏
  • 举报
回复
table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW = new CT_TblWidth(); table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.w = "8450";//单元格宽 table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa; 问题在这三行代码里,.AddNewTcPr()每次都追加一个新的TcPr对象到单元格中,覆盖前一行的设置,你追加后只有最后一行的设置有效,正确做法是 CT_TcPr ctPr = cttc.AddNewTcPr(); //添加TcPr ctPr.tcW = new CT_TblWidth(); ctPr.tcW.w = "100";//单元格宽 ctPr.tcW.type = ST_TblWidth.dxa;
gateway18 2014-12-25
  • 打赏
  • 举报
回复
请问你的问题解决了吗,我也遇到这个问题。
夫人的泡泡鱼 2014-10-13
  • 打赏
  • 举报
回复
devmiao 2014-10-13
  • 打赏
  • 举报
回复
w?width吧。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧