如何在C#中设置字表中的列宽 [英] how to set columns width in word table from C#

查看:210
本文介绍了如何在C#中设置字表中的列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我开发了一个基于桌面的应用程序来创建word文件。

我的代码如下:

Hi,
I have developed a desk top based application that create word file.
my code is below:

 Word.Application wrdApp;
        Word._Document wrdDoc;
        Word.Table wrdTable;
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
<pre> private void button2_Click(object sender, EventArgs e)
       {

           wrdApp = new Word.Application();
           wrdApp.Visible = false;
           wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);
          
           Word.Selection wrdSelection;
           Word.MailMerge wrdMailMerge;
           wrdDoc.Select();
           wrdSelection = wrdApp.Selection;
           wrdMailMerge = wrdDoc.MailMerge;

        
           //merge
          // Word._Document oDataDoc;
           int iCount;

           Object oName = "D:\\ddd.doc";
           // Insert a new table with 9 rows and 4 columns.
           
         Word.Table  wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, 9, 4,
               ref oMissing, ref oMissing);
         
           Object oHeader = "'   ','   ','  ','  ' ";
            // Set the column widths.
           wrdTable.Columns[1].SetWidth(300, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[2].SetWidth(500, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[3].SetWidth(400, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[4].SetWidth(100, Word.WdRulerStyle.wdAdjustSameWidth);
	
           wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,
           ref oMissing, ref oHeader, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing);

           // Open the file to insert data.
           wrdDoc = wrdApp.Documents.Open(ref oName, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing);

           for (iCount = 1; iCount <= 2; iCount++)
           {
               wrdDoc.Tables[1].Rows.Add(ref oMissing);
           }

           // Fill in the data.
           FillRow(wrdDoc, 2, "I.D. Number          : ", "" + txtReceipt.Text + "", "Date:", "" + dtDatePicker.Text + "");
           FillRow(wrdDoc, 3, "Name of the patient  :", "" + txtPatientName.Text + "", "Age :", "" + txtAge.Text + "");
           FillRow(wrdDoc, 4, "Referred by          :",""+txtRefDocter.Text+"","Sex:",""+txtSex.Text+"");
           
           
           // Save and close the file.
           
           wrdDoc.Save();
           wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
       }









and

private void FillRow(Word._Document oDoc, int Row, string Text1,string Text2,string Text3,string Text4)
        {
            // Insert the data into the specific cell.
            oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(Text1);
            oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(Text2);
            oDoc.Tables[1].Cell(Row, 3).Range.InsertAfter(Text3);
            oDoc.Tables[1].Cell(Row, 4).Range.InsertAfter(Text4);
           // oDoc.Tables[1].Cell(Row, 5).Range.InsertAfter(Text5);
        }





我的word文件:

显示





my word file:
shows

M____	                M____1	  M___    M___1
I.D. Number          : 	sfdsfdf	  Date:	  07/15/2010
Name of the patient  :	sfdfds	  Age :	  sfdfd
Referred by          :	sfdfdf	  Sex:	  sfsfsd 





问题:

表格单元格中的列宽度不起作用,我的单词文件标题M _...就像这样我不需要这个但如果关闭标题然后会出现很多标题并且我的现有文档被删除但我强烈需要它,因为只有这个表格才会被插入。

请帮助我,我正在深入解决。

任何帮助都要提前感谢。

Masud



Problem:
columns width in table cell isn't work and my word file header M_... like this I don't need this but if close header then a lot of header will appear and my existing document was erased but i strongly need it bec only this table will be inserted.
Please help me,I am in deep fix.
Any help thanks in advance.
Masud

推荐答案

如果只是表格宽度,这就是我所做的尝试和错误。最后的结果是我得到了这个。



If just table Width, this is what i have did try and error. The final result is I got this.

          wrdApp = new Word.Application();
          wrdApp.Visible = false;
          wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);

          //Range
          Word.Range wrdtableRange = wrdDoc.Paragraphs.Add(ref oMissing).Range;
          wrdtableRange.Tables.Add(wrdtableRange, 9,1, ref oMissing, ref oMissing);  
            
          //Table - It will only get the last table found for this Word.Range
          Word.Table wrdTable = wrdtableRange.Tables[wrdtableRange.Tables.Count];
           
          wrdTable.AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed;
          wrdTable.AllowAutoFit = false;
      
//Below are just test values (you can adjust it if you like.)
//The first column width
wrdTable.Columns.Add(accpt_table.Columns[1]).SetWidth(wrdApp.Application.CentimetersToPoints(5f), Word.WdRulerStyle.wdAdjustNone);

//The second column width
wrdTable.Columns.Add(accpt_table.Columns[2]).SetWidth(wrdApp.Application.CentimetersToPoints(2.8f), Word.WdRulerStyle.wdAdjustNone);

//The third column width
wrdTable.Columns.Add(accpt_table.Columns[3]).SetWidth(wrdApp.Application.CentimetersToPoints(3.2f), Word.WdRulerStyle.wdAdjustNone);

//Then set fourth column width            
wrdTable.Columns[4].SetWidth(wrdApp.Application.CentimetersToPoints(4f), Word.WdRulerStyle.wdAdjustNone);

// If you want to see it without opening it just make it to true
wrdApp.Visible = true;


这篇关于如何在C#中设置字表中的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆