您的位置:首页 > 编程语言 > ASP

asp.net 生成word 动态的填充表格(Microsoft的自带组件)

2011-09-07 19:28 435 查看
用Microsoft的Word组件生成word搞了好久终于做出来,做个记录。

1、引用com组件,要细心不然不易发现

2、创建文件夹涉及权限在config里面加上<identity impersonate="true" userName="计算机账户名" password="密码">

看代码

//索引之间的差额

const int INDEX_SPREAD = 1;

//生成表格中按lstForm的Count生成Row,而lstForm中有前五项放在两Row里面

//所以定义表格时,Row减去Count与Row的差额

const int COUNT_ROW = 3;

//表格列数

const int TABLE_COLUMS = 6;

//是否成功

bool tag = true;

//记录list中索引与word添加表格起始索引的差值与list索引之和

int j = 0;

//文件路径

string Path = "C:\\Documents and Settings\\Administrator\\桌面\\experiment";

//文档名称

string name = "experiment.doc";

//文件路径

object filename = null;

//判断路径是否已存在

if (!Directory.Exists(Path))

{

//创建文件所在目录

Directory.CreateDirectory(Path);

//文件保存路径

filename = Path + "\\" + name;

}

else

{

if (!File.Exists(Path + "\\" + name))

{

name = "experiment1.doc";

filename = Path + "\\" + name;

}

else

{

//HandleCustom("已存在同名文档,如需保存请先删除或转移文档!", false);

return false;

}

}

//try

//{

Object Nothing = System.Reflection.Missing.Value;

//创建Word文档

Microsoft.Office.Interop.Word.ApplicationClass WordApp;

WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//添加页眉

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;

//跳出页眉设置

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

//WordApp.Selection.Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

WordApp.Selection.TypeText("电子商务应用实验报告");

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

//WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

//换行

// count = 14;

//object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;

//WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);

//WordApp.Selection.TypeParagraph();//插入段落

//设置文档的行间距

//WordApp.Selection.ParagraphFormat.LineSpacing = 15f;

//文档对齐方式

WordDoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

//文档中创建表格

Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, lstForm.Count - COUNT_ROW - INDEX_SPREAD, TABLE_COLUMS, ref Nothing, ref Nothing);

//设置表格样式

newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

//设置table的列宽

newTable.Columns[1].Width = 70f;

newTable.Columns[2].Width = 75f;

newTable.Columns[3].Width = 70f;

newTable.Columns[4].Width = 75f;

newTable.Columns[5].Width = 65f;

newTable.Columns[6].Width = 65f;

// 在for循环中使用1,2,3,4,5,j取当前lstForm的Count加上INDEX_SPREAD(差额)作为绘制Row的参数

//count<=5的项放在前两个Row里面,故用1,2,3,4,5判断一下

for (int i = 0; i < lstForm.Count - INDEX_SPREAD; i++)

{

FormsEntity objForm = (FormsEntity)lstForm[i];

j = i + INDEX_SPREAD;

//实验报告基本信息

//姓名

if (j == 1)

{

newTable.Cell(1, 1).Range.Text = objForm.FormName;

newTable.Cell(1, 2).Range.Text = objForm.FormContent;

}

//学号

if (j == 2)

{

newTable.Cell(1, 3).Range.Text = objForm.FormName;

newTable.Cell(1, 4).Range.Text = objForm.FormContent;

}

//班级

if (j == 3)

{

newTable.Cell(1, 5).Range.Text = objForm.FormName;

newTable.Cell(1, 6).Range.Text = objForm.FormContent;

}

//实验编号

if (j == 4)

{

newTable.Cell(2, 1).Range.Text = objForm.FormName;

newTable.Cell(2, 2).Range.Text = objForm.FormContent;

//合并单元格 单元格的列数按照合并后的列数计算

newTable.Cell(2, 2).Merge(newTable.Cell(2, 3));

}

//实验名称

if (j == 5)

{

newTable.Cell(2, 3).Range.Text = objForm.FormName;

newTable.Cell(2, 4).Range.Text = objForm.FormContent;

//合并单元格

newTable.Cell(2, 4).Merge(newTable.Cell(2, 5));

}

//实验报告内容

if (j > 5)

{

//

newTable.Cell(j - COUNT_ROW, 1).Range.Text = objForm.FormName + ": " + objForm.FormContent;

newTable.Cell(j - COUNT_ROW, 1).Merge(newTable.Cell(j - COUNT_ROW, 6));

}

}

//文件保存

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);

WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

return tag;

//}

//catch (Exception ex)

//{

// return false;

//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: