在C#中使用NPOI,设定第一行单元格宽度。
CT_TcPr m_Pr = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "900";//单元格宽 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().tcW = new CT_TblWidth(); table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.w = "5500";//单元格宽 table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa; 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().GetPList()[0].AddNewR().AddNewT().Value = “内容”;
表格宽度为8300,可是效果却没有达到指定的宽度。
解决方案
5
没这么玩过,但是8300也太大了点吧
25
在Word中绘制表格示例
//选择文档保存
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = //定义missing字段并赋值
System.Reflection.Missing.Value;
private object G_str_path;//定义文件保存路径字段
private FolderBrowserDialog G_FolderBrowserDialog;//定义文件夹浏览对话框字段
private void btn_Select_Click(object sender, EventArgs e)
{
G_FolderBrowserDialog =//创建浏览文件夹对象
new FolderBrowserDialog();
DialogResult P_DialogResult = //浏览文件夹
G_FolderBrowserDialog.ShowDialog();
if (P_DialogResult == DialogResult.OK)//确认已经选择文件夹
{
btn_New.Enabled = true;//启用新建按钮
txt_path.Text = //显示选择路径
G_FolderBrowserDialog.SelectedPath;
}
}
//创建格式文档
private void btn_New_Click(object sender, EventArgs e)
{
G_ToolProgressBar.Minimum = 1;//设置进度条最小值
G_ToolProgressBar.Maximum = //设置进度条最大值
int.Parse(txt_row.Text)+1;
btn_New.Enabled = false;//停用新建按钮
ThreadPool.QueueUserWorkItem(//使用线程池
(P_temp) =>//使用lambda表达式
{
G_wa = new Word.Application();//创建Word应用程序对象
Word.Document P_wd = G_wa.Documents.Add(//建立新文档
ref G_missing, ref G_missing, ref G_missing, ref G_missing);
Word.Range P_Range = P_wd.Paragraphs[1].Range;//得到文档范围对象
object P_DefaultTable = //创建表格参数对象
Word.WdDefaultTableBehavior.wdWord8TableBehavior;
object P_AutoFit = //创建表格参数对象
Word.WdAutoFitBehavior.wdAutoFitWindow;
Word.Table P_WordTable = P_Range.Tables.Add(//向文档中添加表格
P_Range,
int.Parse(txt_row.Text),
int.Parse(txt_column.Text),
ref P_DefaultTable, ref P_AutoFit);
for (int i = 1; i < int.Parse(txt_row.Text) + 1; i++)
{
for (int j = 1; j < int.Parse(txt_column.Text) + 1; j++)
{
P_WordTable.Cell(i, j).Range.Text =//使用双层循环向表格中添加数据
string.Format(“{0}行 {1}列”, i.ToString(), j.ToString());
Thread.Sleep(10);//线程挂起10毫秒
}
this.Invoke(//调用窗体线程
(MethodInvoker)(() => //使用Lambda表达式
{
G_ToolProgressBar.Value = i + 1;//设置进度信息
}));
}
G_str_path = string.Format(//计算文件保存路径
@”{0}\{1}”, G_FolderBrowserDialog.SelectedPath,
DateTime.Now.ToString(“yyyy年M月d日h时s分m秒fff毫秒”) + “.doc”);
P_wd.SaveAs(//保存Word文件
ref G_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
((Word._Application)G_wa.Application).Quit(//退出应用程序
ref G_missing, ref G_missing, ref G_missing);
this.Invoke(//开始执行窗体线程
(MethodInvoker)(() =>//使用lambda表达式
{
btn_Display.Enabled = true;//启用显示按钮
MessageBox.Show(“成功创建Word文档!”, “提示!”);
}));
});
}
//显示文档
private void btn_Display_Click(object sender, EventArgs e)
{
G_wa = //创建应用程序对象
new Microsoft.Office.Interop.Word.Application();
G_wa.Visible = true;//将文档设置为可见
Word.Document P_Document = G_wa.Documents.Open(//打开Word文档
ref G_str_path, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing);
}
//选择文档保存
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = //定义missing字段并赋值
System.Reflection.Missing.Value;
private object G_str_path;//定义文件保存路径字段
private FolderBrowserDialog G_FolderBrowserDialog;//定义文件夹浏览对话框字段
private void btn_Select_Click(object sender, EventArgs e)
{
G_FolderBrowserDialog =//创建浏览文件夹对象
new FolderBrowserDialog();
DialogResult P_DialogResult = //浏览文件夹
G_FolderBrowserDialog.ShowDialog();
if (P_DialogResult == DialogResult.OK)//确认已经选择文件夹
{
btn_New.Enabled = true;//启用新建按钮
txt_path.Text = //显示选择路径
G_FolderBrowserDialog.SelectedPath;
}
}
//创建格式文档
private void btn_New_Click(object sender, EventArgs e)
{
G_ToolProgressBar.Minimum = 1;//设置进度条最小值
G_ToolProgressBar.Maximum = //设置进度条最大值
int.Parse(txt_row.Text)+1;
btn_New.Enabled = false;//停用新建按钮
ThreadPool.QueueUserWorkItem(//使用线程池
(P_temp) =>//使用lambda表达式
{
G_wa = new Word.Application();//创建Word应用程序对象
Word.Document P_wd = G_wa.Documents.Add(//建立新文档
ref G_missing, ref G_missing, ref G_missing, ref G_missing);
Word.Range P_Range = P_wd.Paragraphs[1].Range;//得到文档范围对象
object P_DefaultTable = //创建表格参数对象
Word.WdDefaultTableBehavior.wdWord8TableBehavior;
object P_AutoFit = //创建表格参数对象
Word.WdAutoFitBehavior.wdAutoFitWindow;
Word.Table P_WordTable = P_Range.Tables.Add(//向文档中添加表格
P_Range,
int.Parse(txt_row.Text),
int.Parse(txt_column.Text),
ref P_DefaultTable, ref P_AutoFit);
for (int i = 1; i < int.Parse(txt_row.Text) + 1; i++)
{
for (int j = 1; j < int.Parse(txt_column.Text) + 1; j++)
{
P_WordTable.Cell(i, j).Range.Text =//使用双层循环向表格中添加数据
string.Format(“{0}行 {1}列”, i.ToString(), j.ToString());
Thread.Sleep(10);//线程挂起10毫秒
}
this.Invoke(//调用窗体线程
(MethodInvoker)(() => //使用Lambda表达式
{
G_ToolProgressBar.Value = i + 1;//设置进度信息
}));
}
G_str_path = string.Format(//计算文件保存路径
@”{0}\{1}”, G_FolderBrowserDialog.SelectedPath,
DateTime.Now.ToString(“yyyy年M月d日h时s分m秒fff毫秒”) + “.doc”);
P_wd.SaveAs(//保存Word文件
ref G_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
((Word._Application)G_wa.Application).Quit(//退出应用程序
ref G_missing, ref G_missing, ref G_missing);
this.Invoke(//开始执行窗体线程
(MethodInvoker)(() =>//使用lambda表达式
{
btn_Display.Enabled = true;//启用显示按钮
MessageBox.Show(“成功创建Word文档!”, “提示!”);
}));
});
}
//显示文档
private void btn_Display_Click(object sender, EventArgs e)
{
G_wa = //创建应用程序对象
new Microsoft.Office.Interop.Word.Application();
G_wa.Visible = true;//将文档设置为可见
Word.Document P_Document = G_wa.Documents.Open(//打开Word文档
ref G_str_path, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing);
}
10
private void SetCellWidth(XWPFTable tbl,int rowIndex,int colIndex, int width)
{
var cell = tbl.GetRow(rowIndex).GetCell(colIndex);
cell.GetCTTc().AddNewTcPr().AddNewTcW().w = width.ToString();
}
{
var cell = tbl.GetRow(rowIndex).GetCell(colIndex);
cell.GetCTTc().AddNewTcPr().AddNewTcW().w = width.ToString();
}