首页 新闻 会员 周边

C#操作word 插入表格并填充表格

0
悬赏园豆:20 [已关闭问题] 关闭于 2010-01-19 15:57
<pre>之前也做过将数据导出到Word的功能都可以实现,现在需要用代码向Word中插入表格并填充数据,表格是可以插入到Word中了但是在填充数据报异常了。</pre> <pre>关键代码如下:</pre> <pre>using System;</pre> <pre>using System.Collections.Generic;</pre> <pre>using System.Text;</pre> <pre>using System.Web;</pre> <pre>using System.Web.UI;</pre> <pre>using System.IO;</pre> <pre>using Microsoft.Office.Interop.Word;</pre> <pre></pre> <pre>//空字符 </pre> <pre>object missing = System.Reflection.Missing.Value; </pre> <pre> ApplicationClass wordApp = new ApplicationClass(); </pre> <pre>object docName = HttpContext.Current.Server.MapPath("~/print/zjxt.doc");//模板 </pre> <pre>//创建一个word实例 </pre> <pre>Document MyDoc = wordApp.Documents.Add(ref docName, ref missing, ref missing, ref missing); </pre> <pre>//显示word </pre> <pre>wordApp.Visible = true; </pre> <pre>MyDoc.Activate();</pre> <pre></pre> <pre>//文档中创建表格 </pre> <pre>Microsoft.Office.Interop.Word.Table zjcyTable = MyDoc.Tables.Add(MyDoc.Bookmarks.get_Item(ref zjcy).Range, 4, 5, ref missing, ref missing); </pre> <pre>//填充表格内容 </pre> <pre>for (int i = 0; i &lt; 4; i++) </pre> <pre>{ </pre> <pre>  C_zjcy c_zjcy = list2[i] as C_zjcy; </pre> <pre>  zjcyTable.Cell(i, 1).Range.Text = c_zjcy.Zjcy1.ToString();//------------------运行到这里时报异常了 </pre> <pre>  zjcyTable.Cell(i, 2).Range.Text = c_zjcy.Zjcy2.ToString(); </pre> <pre>  zjcyTable.Cell(i, 3).Range.Text = c_zjcy.Zjcy3.ToString(); </pre> <pre>  zjcyTable.Cell(i, 4).Range.Text = c_zjcy.Zjcy4.ToString(); </pre> <pre>  zjcyTable.Cell(i, 5).Range.Text = c_zjcy.Zjcy5.ToString(); </pre> <pre>}</pre> <pre></pre> <pre>前几次报异常时如下: </pre> <pre>  [服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))]</pre> <pre></pre> <pre>后来再报异常时就是这样了: </pre> <pre>  对象已被删除。 </pre> <pre>  说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导 致错误的出处的详细信息。 </pre> <pre>  异常详细信息: System.Runtime.InteropServices.COMException: 对象已被删除。 </pre>
今日的主页 今日 | 初学一级 | 园豆:10
提问于:2010-01-18 11:43
< >
分享
所有回答(4)
0

public void MakeUpPostCode(System.Collections.Generic.List<vAllCommonUser> data, string targetFullFileName)
{
Object Nothing
= System.Reflection.Missing.Value;
try
{
//取得Word文件保存路径
object filename = targetFullFileName;
//创建一个名为WordApp的组件对象
//Microsoft.Office.Interop.Word.Application WordApp = new ApplicationClass();
//网络上有写代码中直接使用Word.Application WordApp = new ApplicationClass(); 但我在实际编写中总是出现错误。
//创建一个名为WordDoc的文档对象
WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Activate();
WordDoc.PageSetup.LeftMargin
= 18;
WordDoc.PageSetup.RightMargin
= 18;
WordDoc.PageSetup.TopMargin
= 18;
WordDoc.PageSetup.BottomMargin
= 18;

//增加一表格
var rows = Convert.ToInt16(Math.Ceiling(Convert.ToDouble(data.Count) / 2));
Microsoft.Office.Interop.Word.Table table
= WordDoc.Tables.Add(WordApp.Selection.Range, rows, 2, ref Nothing, ref Nothing);
table.Rows.HeightRule
= WdRowHeightRule.wdRowHeightExactly;
table.Rows.Height
= 134;
//在表格第一单元格中添加自定义的文字内容

table.Columns[
1].Width = 279;
table.Columns[
2].Width = 279;
for (var i = 0; i < data.Count; i++)
{
EntryModel model
= new EntryModel();
var city
= "";
var county
= "";
if (data[i].COMP_Addr_City != null && data[i].COMP_Addr_City.Value != AppConst.DefaultGuid)
{
city
= model.getItem(data[i].COMP_Addr_City.Value).Name;
county
= model.getItem(data[i].COMP_Addr_County.Value).Name;
}
var row
= i == 0 ? 1 : (i % 2 == 1 ? (i + 1) / 2 : (i / 2) + 1);
table.Cell(row, (i
% 2) + 1).Range.Text = "" +
data[i].ContactZip
+ "\n" +
city
+county+ data[i].COMP_Addr + "\n" +
data[i].Company
+ " " + data[i].MemberID + "\n" +
(data[i].MemberKind
== 3 ? data[i].RealName : data[i].VIP1) + " " + data[i].VIP1Title + " 鈞啓\n" +
"聯絡人:" + data[i].Contact + " (內含會費到期通知)\n";
table.Cell(row, (i
% 2) + 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
}
//table.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth025pt;


//在文档空白地方添加文字内容
// WordDoc.Paragraphs.Last.Range.Text = "Wellcome To Aspxcn.Com";
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}


try
{
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Application.Quit(ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception e2)
{
}

}

 

这是以前写的一个创建word并添加表格的方法,你看看吧

Ou lei | 园豆:619 (小虾三级) | 2010-01-18 11:52
你好我仔细看过你的代码,没有什么区别。我这里报异常很奇怪,可以给指定书签的位置赋值也可以添加表格,就是在给这个添加的表格中赋值时就报异常了。我倒是觉着是不是我电脑的某个配置或者设置不符合要求,这个我是第一次做所以不怎么懂……如果方便的话还麻烦你能具体帮我分析一下谢谢
支持(0) 反对(0) 今日 | 园豆:10 (初学一级) | 2010-01-18 12:19
@lijia2010:其实这个我其实也不是很熟悉,这个是别人写的一个方法,可能帮不上你了。
支持(0) 反对(0) Ou lei | 园豆:619 (小虾三级) | 2010-01-18 13:16
@Ou lei: 好地谢谢
支持(0) 反对(0) 今日 | 园豆:10 (初学一级) | 2010-01-18 13:33
0

谢谢提供的代码!

xxx1013 | 园豆:205 (菜鸟二级) | 2010-01-18 13:25
1

表格的行和列索引都是从1开始的吧

sojuli | 园豆:105 (初学一级) | 2010-01-19 11:50

这个是正解,注意索引开始不是0

支持(0) 反对(0) 彩色超超 | 园豆:12 (初学一级) | 2014-08-17 21:34
0

创建word表格可以试试这个https://www.e-iceblue.cn/docjava/create-table-in-word-in-java.html

UnSoleil | 园豆:228 (菜鸟二级) | 2022-09-29 16:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册