接上一篇,这是关于书签的操作.
/// <summary>
/// 打印商品
/// </summary>
/// <param name=”strusername”></param>
/// <param name=”strpackid”></param>
private void makewordfile(string strusername,string strpackid)
{
try
{
endoc ed = new endoc();
ed.dotpath = server.mappath(@”../product/一览表.doc”);
string path= server.mappath(@”../printproduct/”);
if(!directory.exists(path))
{
directory.createdirectory(path);
}
ed.openfile();
string strsqltext = “查询语句”; //在该处写查询语句
datatable mydatatable = new datatable();
mydatatable = mc.getdt(strsqltext); //数据库操作类,返回datatable类型数据
datatable dt = new datatable();
dt.columns.add(“id”,type.gettype(“system.int32”));
datacolumn[] pk = new datacolumn[1];
pk[0] = dt.columns[“id”];
dt.primarykey = pk;
dt.columns[“id”].autoincrement = true;
dt.columns[“id”].autoincrementseed = 1;
dt.columns[“id”].readonly = true;
dt.columns.add(“id”,type.gettype(“system.string”));
dt.columns.add(“name”,type.gettype(“system.string”));
dt.columns.add(“num”,type.gettype(“system.string”));
datarow temprow;
for (int i=0 ;i< mydatatable.rows.count;i++)
{
temprow = dt.newrow();
temprow[“”] = mydatatable.rows[i][“id”];
temprow[“name”] = mydatatable.rows[i][“name”];
temprow[“num”] = mydatatable.rows[i][“num”];
dt.rows.add(temprow);
}
object lblname = “providername”;
ed.writedata(ref lblname,”username”); //在书签处替换
ed.filltable(dt,2,4,2);
string strfilename = strusername + datetime.now.tostring(“yyyymmddhhmmss”) +”.doc”;
object filename = path + strfilename;
strfilename = “../printproduct/” + strfilename;
ed.saveas(ref filename);
ed.quit();
response.write(“<script language = javascript>window.open(“);
response.write(strfilename);
response.write(“)</script>”);
}
catch(exception ex)
{
response.write(“<script language = javascript>alert(“+ex.message+”);</script>”);
}
}
*****************************888
endoc类
using system;
using word;
using system.reflection;
using system.runtime.interopservices ;
using system.data ;
using system.configuration;
using system.io;
namespace schdeposit.funcclass
{
/// <summary>
/// endoc 的摘要说明。
/// </summary>
public class endoc
{
public endoc()
{
//
// todo: 在此处添加构造函数逻辑
//
}
[dllimport (“user32.dll”)]
public static extern int messagebox(int h,string m,string c,int type);
word.applicationclass app = new word.applicationclass();
object optional=missing.value;
object visible=true;
object savechanges = true;
object notsavechanges = false;
object docreadonly=true;
object originalformat = missing.value;
object routedocument = missing.value;
_document doc=null;
// fillctl f=new fillctl ();
private string dotpath;
public string dotpath
{
get
{
return dotpath;
}
set
{
dotpath=value;
}
}
private string getdata(string str)
{
if (str==null)
str=””;
else
str=str.trim();
return str;
}
#region ******打开文件*******
public bool openfile()
{
try
{
if (file.exists(dotpath)==true)
{
object filename = dotpath;
doc = app.documents.open(ref filename,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref visible);
return true;
}
else
messagebox(0,”文件不存在!” ,”error message box”,0);
return false;
}
catch (system.exception e)
{
throw new exception (e.message );
}
}
#endregion
#region ******写入数据*******
public void writedata(ref object lbl,string txt)
{//向标签lbl中写入数据txt
try
{
doc.bookmarks.item(ref lbl).select();
doc.application.selection.typetext(txt.trim());
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
throw new exception (e.message );
}
}
public void writedata(datatable dt)
{//将datatable的第一行数据顺序写入文档的标签
try
{
if (dt.rows.count >=1)
{
object[] lbl=getmark ();
if (lbl.length ==dt.columns.count)
for (int i=0;i<lbl.length;i++ )
writedata (ref lbl[i],dt.rows[0][i].tostring ());
else
messagebox(0, “长度不符” ,”error message box”,0);
}
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
throw new exception (e.message );
}
}
#endregion
#region ******向表格写入数据*******
/// <summary>
///
/// </summary>
/// <param name=”dt”>datatable</param>
/// <param name=”beginrow”>开始写入的起始行</param>
/// <param name=”cols”>填充的列数</param>
/// <param name=”tablesitem”>表格在word中的序号,从1开始</param>
/// <returns></returns>
public bool filltable(datatable dt,int beginrow,int cols,int tablesitem)
{
try
{
object row =1;
tablesitem=app.application.activedocument.tables.count;
int xb=0;
for (int i=0; i<dt.rows.count; i++)
{
app.application.activedocument.tables.item(tablesitem).cell (beginrow + i,1).select ();
app.application.selection.insertrowsbelow (ref row);
app.application.selection.text=getdata(dt.rows[xb][0].tostring());
for (int j=2; j<=cols; j++)
{
app.application.activedocument.tables.item(tablesitem).cell (beginrow + i,j).select ();
app.application.selection.text=getdata(dt.rows[xb][j-1].tostring());
}
xb++;
}
return true;
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
messagebox(0,e.message + “\r\n” + e.stacktrace ,”error message box”,0);
return false;
}
}
#endregion
#region ******创建表格*******
public bool createtable(int rownum,int colnum,string header)
{
try
{
char [] separtor1 = {;};
char [] separtor2 = {,};
string [] split1 = null;
string [] split2 = null;
split1 = header.split(separtor1);
word.table table=doc.tables.add(app.selection.range,rownum,colnum,ref optional,ref optional);
for(int i=1; i<=split1.length ;i++)
{//添加每一行
split2 = split1[i-1].split(separtor2);
for (int j=1; j<=split2.length; j++)
{//添加每一列
table.cell(i,j).range.text=split2[j-1].trim();
}
}
return true;
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
messagebox(0,e.message + “\r\n” + e.stacktrace ,”error message box”,0);
return false;
}
}
public bool contenttableadd(datatable dt)
{
try
{
app.selection.typeparagraph();
app.selection.typeparagraph();
app.selection.typeparagraph();
word.table table=doc.tables.add(app.selection.range,4,9,ref optional,ref optional);
table.cell(1,1).range.text=dt.rows[0][“content_id”].tostring();
return true;
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
messagebox(0,e.message + “\r\n” + e.stacktrace ,”error message box”,0);
return false;
}
}
#endregion
#region ******删除表格*******
public bool deltable(int num)
{
try
{
doc.tables.item(num).delete ();
return true;
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
messagebox(0,e.message + “\r\n” + e.stacktrace ,”error message box”,0);
return false;
}
}
#endregion
#region ******文件另存为*******
public void saveas(ref object filename)
{
try
{
doc.saveas(ref filename,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref optional);
}
catch (system.exception e)
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
throw new exception (e.message );
}
}
#endregion
#region ******关闭word*******
public void quit()
{
try
{
doc.close(ref notsavechanges, ref originalformat, ref routedocument);
app.quit(ref notsavechanges, ref originalformat, ref routedocument);
}
catch (system.exception e)
{
throw new exception (e.message );
}
}
public void appquit()
{
try
{
app.quit(ref notsavechanges, ref originalformat, ref routedocument);
}
catch (system.exception e)
{
throw new exception (e.message );
}
}
#endregion
private object[] getmark()
{
object j=null;
string str=””;
object []result=new object [doc.bookmarks.count];
for (int i=1;i<=doc.bookmarks.count;i++)
{
j=(object)i;
result[i-1]=doc.bookmarks.item(ref j).name;
str = str + doc.bookmarks.item(ref j).name + “,”;
}
return result;
}
}
}