asp.net简单的操作word

    首先导入COM组件中的Microsoft Word 11.0(视你的word版本而定) Object Library

    然后,新建一个word文档,然后再新建一个table插入数据 ,最后关闭,我*,还是贴代码吧。。。。。说的太难了

  

    ///<summary>
/// 创建新的word文档到服务器
///</summary>
///<param name="lieName">列名</param>
///<param name="dt">主要数据</param>
///<param name="rowCount">行数</param>
///<param name="cellCount">列数</param>
///<param name="plan">文件名</param>
///<param name="address">路径</param>
public string cWord(ArrayList lieName,DataTable dt,int rowCount,int cellCount,string plan,string address)
{
Word.Application Oword = new Word.Application();
Word.Document Odoc;
Object Nothing = System.Reflection.Missing.Value;
string wordName = plan + ".doc";
Object filename = address + wordName;
Odoc = Oword.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);

Word.Table newTable = Odoc.Tables.Add(Oword.Selection.Range,rowCount+1,cellCount,ref Nothing,ref Nothing);
for (int i = 1; i <= cellCount; i++)
{
newTable.Cell(1, i).Range.Text = lieName[i-1].ToString();
newTable.Cell(1, i).Range.Bold = 2;


}
newTable.Columns[1].Width = 50;

newTable.Columns[3].Width = 200;
for (int j = 0; j < rowCount; j++)
{
int sj = j + 1;
newTable.Cell(j+2, 1).Range.Text = sj.ToString(); ;
}
for (int m = 0; m < rowCount; m++)
{
for (int n = 0; n < dt.Columns.Count; n++)
{
newTable.Cell(m+2, n+2).Range.Text = dt.Rows[m][n].ToString();


}

}
Odoc.Content.Tables[1].Rows.Add(ref Nothing);
Odoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
Odoc.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);
Odoc.Close(ref Nothing,ref Nothing,ref Nothing);
Oword.Quit(ref Nothing, ref Nothing, ref Nothing);

return wordName;

}

 在这记下,方便以后温习 

posted on 2011-11-10 09:41  Demon Edge  阅读(521)  评论(0)    收藏  举报