刚好前段时间弄过。我把大致的代码粘上你看下吧。是C#的。和你说下思路吧,先在WORD中画好要打印的表,并且设置好,横向的可以在“文件”->“页面设置”里设置。然后把要绑定数据库的部分插入书签。打印的时候调用设计好的那个WORD文件把数据库的内容绑定到书签就行了。首先要在程序中引入COM组件中的Microsoft Word 11。安装的word中要包括.net编程组件和Interop(默认是不安装的)。public void bindAndPrint(){ //绑定Work和数据库Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object templateFile = Application.StartupPath + @"\word模板.doc";try
{
app = new Microsoft.Office.Interop.Word.ApplicationClass();
doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
try
{
foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
{
bm.Select();
string item = bm.Name;
if (item.Equals("title")) //word中的书签名"title"
{
bm.Range.Text = title; //title为要替换书签的变量, //即可以将数据库中的值读入title中
}
}
}
catch
{ } //打印 doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, this.Text);
} //结束word进程 finally
{
object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
if (doc != null) doc.Close(ref saveChange, ref missing, ref missing);
if (app != null) app.Quit(ref missing, ref missing, ref missing);
} break;} 希望能帮到你!
温馨提示:内容为网友见解,仅供参考