📄 form1.cs
字号:
//
this.开学日期.CustomFormat = "M月d日";
this.开学日期.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.开学日期.Location = new System.Drawing.Point(104, 64);
this.开学日期.Name = "开学日期";
this.开学日期.Size = new System.Drawing.Size(104, 23);
this.开学日期.TabIndex = 11;
//
// label11
//
this.label11.Location = new System.Drawing.Point(216, 128);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(88, 24);
this.label11.TabIndex = 6;
this.label11.Text = "打印日期:";
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// 打印日期
//
this.打印日期.CustomFormat = "M月d日";
this.打印日期.Location = new System.Drawing.Point(312, 128);
this.打印日期.Name = "打印日期";
this.打印日期.Size = new System.Drawing.Size(120, 23);
this.打印日期.TabIndex = 11;
//
// 模板
//
this.模板.Location = new System.Drawing.Point(312, 160);
this.模板.Name = "模板";
this.模板.Size = new System.Drawing.Size(168, 23);
this.模板.TabIndex = 15;
this.模板.Text = "";
//
// label7
//
this.label7.Location = new System.Drawing.Point(176, 128);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(16, 23);
this.label7.TabIndex = 18;
this.label7.Text = "元";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label8
//
this.label8.Location = new System.Drawing.Point(176, 160);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(16, 23);
this.label8.TabIndex = 18;
this.label8.Text = "元";
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// 注册日期
//
this.注册日期.CustomFormat = "M月d日";
this.注册日期.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.注册日期.Location = new System.Drawing.Point(312, 96);
this.注册日期.Name = "注册日期";
this.注册日期.Size = new System.Drawing.Size(120, 23);
this.注册日期.TabIndex = 11;
//
// label13
//
this.label13.Location = new System.Drawing.Point(216, 96);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(88, 24);
this.label13.TabIndex = 6;
this.label13.Text = "注册日期:";
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label14
//
this.label14.Location = new System.Drawing.Point(216, 192);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(88, 24);
this.label14.TabIndex = 16;
this.label14.Text = "数据库:";
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// 数据库
//
this.数据库.Location = new System.Drawing.Point(312, 192);
this.数据库.Name = "数据库";
this.数据库.Size = new System.Drawing.Size(168, 23);
this.数据库.TabIndex = 15;
this.数据库.Text = "";
//
// ofd
//
this.ofd.Filter = "Word模板文件|*.dot";
this.ofd.RestoreDirectory = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 264);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "开始打印";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// conn
//
this.conn.ConnectionString = "Data Source=\"F:\\gao\\c#_Word\\WindowsApplication1\\data.mdb\";Provider=\"Microsoft.Jet" +
".OLEDB.4.0\"";
//
// cmd
//
this.cmd.Connection = this.conn;
//
// da
//
this.da.SelectCommand = this.cmd;
//
// button4
//
this.button4.Location = new System.Drawing.Point(264, 264);
this.button4.Name = "button4";
this.button4.TabIndex = 6;
this.button4.Text = "关于";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(360, 264);
this.button5.Name = "button5";
this.button5.TabIndex = 7;
this.button5.Text = "退出";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
this.ClientSize = new System.Drawing.Size(560, 301);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.Name = "Form1";
this.Tag = "";
this.Text = "通知书打印 无常作品 2005-01-28";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}
private Microsoft.Office.Interop.Word.ApplicationClass App = null; //Word应用程序对象
private Microsoft.Office.Interop.Word.Document doc = null; //Word文档对象
//书签
private System.String [] bmsComm = {"放假日期","开学日期","课本费","学费","住宿费","打印日期","班主任姓名","班主任电话","注册日期"};
private System.String [] bmsStu = {"姓名","早操迟到","早操缺勤","早读迟到","早读缺勤","课堂迟到","课堂缺勤","卫生合格","卫生不合格"};
private System.String [] bmsKC = {"课程名称1","课程名称2","课程名称3","课程名称4","课程名称5","课程名称6","课程名称7"};
private System.String [] bmsCJ = {"成绩1","成绩2","成绩3","成绩4","成绩5","成绩6","成绩7"};
private System.Data.DataSet dsStu = new DataSet();
private System.Data.DataSet dsScore = new DataSet();
/// <summary>
/// 用指定字符串替换书签内容
/// </summary>
/// <param name="bms">书签集合对象</param>
/// <param name="bmName">书签名</param>
/// <param name="tag">替换字符串</param>
private void replace( Microsoft.Office.Interop.Word.Bookmarks bms, String bmName, String target)
{
//这样效率真TMD的低,哪位有好点的方法教下我
foreach( Microsoft.Office.Interop.Word.Bookmark bm in bms )
{
if( bm.Name.Equals( bmName ) )
{
//System.Windows.Forms.MessageBox.Show( bm.Name + "OK");
bm.Select();
bm.Range.Text = target;
break;
}
}
}
/// <summary>
/// 返回指定控件的Text值
/// </summary>
/// <param name="ControlName">控件名</param>
/// <returns>返回控件的Text值</returns>
private String getControlText( String ControlName)
{
//这样效率真TMD的低,哪位有好点的方法教下我
foreach(Control objCtl in this.groupBox1.Controls )
{
if( objCtl.Name.Equals( ControlName ) )
{
return objCtl.Text;
}
}
return "";
}
/// <summary>
/// 获取学生和成绩数据
/// </summary>
private bool getData()
{
//这里的就不用说了吧,应该都看得懂了
String connStr ="Provider=\"Microsoft.Jet.OLEDB.4.0\";Data Source="+this.数据库.Text;
conn.ConnectionString = connStr;
try
{
conn.Open();
}
catch(System.Exception e)
{
System.Windows.Forms.MessageBox.Show("打开数据库出错。\n请确定数据库文件是否存在。\n错误信息:" +e.ToString() );
return false;
}
this.dsScore.Clear();
this.dsStu.Clear();
cmd.CommandText = "select * from 学生";
da.Fill( this.dsStu );
cmd.CommandText = "select * from 成绩";
da.Fill( this.dsScore );
//这里记得要close
//暑假做应科的网上教务系统时有些地方没有close,结果用几个小时系统就挂了
//打开Sqlserver有上百个活动连接,用了2个小时把DataAccess层代码查一遍
//把conn都close后系统才正常
conn.Close();
return true;
}
//选择模板
private void button1_Click(object sender, System.EventArgs e)
{
ofd.Filter = "Word模板文件|*.dot";
if( ofd.ShowDialog() == DialogResult.OK )
{
this.模板.Text = ofd.FileName;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//数据库是否存在
if( this.getData() )
{
System.Data.DataRow dr;
System.Data.DataView dv= this.dsScore.Tables[0].DefaultView;
for( int i=0; i< this.dsStu.Tables[0].Rows.Count; i++ )
{
//建立新文档
object missing = System.Reflection.Missing.Value;
if( App == null )
App = new Microsoft.Office.Interop.Word.ApplicationClass();
App.Visible = true;
object oTemplate = this.模板.Text.Trim();
doc = App.Documents.Add(ref oTemplate, ref missing,ref missing, ref missing);
doc.Activate();
Microsoft.Office.Interop.Word.Bookmarks bms = doc.Bookmarks;
//替换开学日期等内容
foreach( String s in bmsComm)
{
this.replace( bms, s, this.getControlText( s ) );
}
dr = dsStu.Tables[0].Rows[i];
//替换学生纪律情况等
foreach( System.Data.DataColumn dc in dsStu.Tables[0].Columns )
{
this.replace( bms, dc.Caption, dr[ dc.Caption].ToString() );
}
//替换成绩
int n = 1;
dv.RowFilter = "学号=" + dr["学号"].ToString();
foreach(DataRowView row in dv)
{
this.replace( bms,"课程名称"+n.ToString(),row["课程名称"].ToString());
this.replace( bms,"成绩"+n.ToString(),row["成绩"].ToString());
n++;
}
//使用默认打印机打印文档
//C#不提供默认参数真是麻烦
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);
//关闭文档而不保存
object SaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
missing = System.Reflection.Missing.Value;
doc.Close( ref SaveChanges, ref missing, ref missing);
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
//选择数据库
ofd.Filter= "Access数据库(*.mdb)|*.mdb";
if( this.ofd.ShowDialog() == DialogResult.OK )
{
this.数据库.Text = ofd.FileName.Trim();
}
}
private void button5_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button4_Click(object sender, System.EventArgs e)
{
//关于窗口
Form2 f = new Form2();
f.ShowDialog();
f.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -