C#中的水晶报表好用,但是需要注册码。这里介绍C#调用word直接打印,既方便,又漂亮。
开发指南:
1. 必须安装office的.net开发包。
2. 在工程的reference中添加com组件:Microsoft Word 11.0 Object Library
打印的代码如下:
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object templateFile = Application.StartupPath + @”表单模版.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(“in_time”))
{
bm.Range.Text = table.inTime == null ? “” : table.inTime.ToString();
}
else if (item.Equals(“car_id”))
{
bm.Range.Text = table.carID == null ? “” : table.carID.ToString();
}
}
}
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);
}