/添加两个引用
using Microsoft.Office.Interop.Word;
using System.Data;
//红色是需要(根据个人情况设置)的
//l蓝色是调用数据库中查询得来的!
//我的书签定义为T1~T5,F1,D1,E00~E67
//鼠标点击事件如下!
protected void imgexport_Click(object sender, ImageClickEventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = null;//应用程序
Microsoft.Office.Interop.Word.DocumentClass doc = null;//文档
try
{
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = Server.MapPath("..") + "/Report/科技创新项目申报书.doc";//模板路径
object objDocType = WdDocumentType.wdTypeDocument;
doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//InlineShape shape = appWord.Selection.InlineShapes.AddPicture(@"F:\Picture\_DSC1602.JPG", ref Nothing, ref Nothing, ref Nothing);//文档中的图形对象
//第一步 读取数据,填充数据集
string ItemID = "1";(我要通过它来进行查询,这里是写死了,可以根据自己需要进行修改)
ScienceManage.BLL.ItemInfo itemInfoBll = new ScienceManage.BLL.ItemInfo();
DataSet ds = itemInfoBll.SelectItemInfo(ItemID);
//第二步 给书签赋值
//给书签赋值
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
SetBookMarksValue(doc, "T1", ds.Tables[0].Rows[0]["ItemName"].ToString());
SetBookMarksValue(doc, "T2", ds.Tables[0].Rows[0]["CompleteCmp"].ToString());
SetBookMarksValue(doc, "T3", DateTime.Parse(ds.Tables[0].Rows[0]["EditorDate"].ToString()).ToString("yyyy-MM-dd"));//显示日期仅显示年月日
SetBookMarksValue(doc, "T4", ds.Tables[0].Rows[0]["DutyPerson"].ToString());
SetBookMarksValue(doc, "T5", DateTime.Parse(ds.Tables[0].Rows[0]["EditorDate"].ToString()).ToString("yyyy-MM-dd"));
SetBookMarksValue(doc, "F1", ds.Tables[0].Rows[0]["ItemMoney"].ToString());
}
//项目进度
ScienceManage.BLL.Progress progressBll = new ScienceManage.BLL.Progress();
ds = progressBll.SelectProgress(ItemID);
string pro = "";
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
for (int m = 0; m < ds.Tables[0].Rows.Count - 1; m++)
{
pro += (m + 1).ToString() + "、" + ds.Tables[0].Rows[m]["PName"].ToString() + "\r\n";//多条数据定义一个书签时可以这样显示!
}
pro += ds.Tables[0].Rows.Count.ToString() + "、" + ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["PName"].ToString();
}
SetBookMarksValue(doc, "D1", pro);
//表格中赋值
//将书签赋值为空,另生成书签!
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 8; j++)
{
object str = "E" + i.ToString() + j.ToString();//定义的书签格式例如 E00;通过i,j来控制行列!
object ran = doc.Bookmarks.get_Item(ref str).Range;
doc.Bookmarks.get_Item(ref str).Range.Text = "";
doc.Bookmarks.Add(str.ToString(), ref ran);
}
}
ScienceManage.BLL.MemberInfo memberinfoBll = new ScienceManage.BLL.MemberInfo();
ds = memberinfoBll.SelectMemberInfoItemID(ItemID);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (i > 6)//我这里限制只输出7行!
break;
SetBookMarksValue(doc, "E" + i.ToString() + "0", (i + 1).ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "1", ds.Tables[0].Rows[i]["Name"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "2", ds.Tables[0].Rows[i]["Age"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "3", ds.Tables[0].Rows[i]["Duty"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "4", ds.Tables[0].Rows[i]["Title"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "5", ds.Tables[0].Rows[i]["Speciality"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "6", ds.Tables[0].Rows[i]["Work"].ToString());
SetBookMarksValue(doc, "E" + i.ToString() + "7", ds.Tables[0].Rows[i]["Department"].ToString());
}
}
//文档中插入表格(可以动态生成表格,但我还没想出来怎么往表格中添加数据!)
//doc.Bookmarks.get_Item(ref obtable).Range.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 12, 3, ref Nothing, ref Nothing);
//////Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 7, 8, ref Nothing, ref Nothing);//(7,8控制行列!)
//////newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
//////newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
//给文档的最后一行再添加内容
//////doc.Paragraphs.Last.Range.Text = "";
//第三步 生成word
object filename = Server.MapPath("..") + "/Temp/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
catch (System.Exception ex)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = ex.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}
private void SetBookMarksValue(Microsoft.Office.Interop.Word.DocumentClass doc, object name, string value)
{
doc.Bookmarks.get_Item(ref name).Range.Text = value;
}
}