📄 frmdataout.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace 毕业生信息管理系统
{
/// <summary>
/// frmDataOut 的摘要说明。
/// </summary>
public class frmDataOut : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RadioButton rad1;
private System.Windows.Forms.RadioButton rad2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private OleDbConnection objSqlConn;
private DataSet objdataSet;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private OleDbDataAdapter objOleDbDataAdapter;
public frmDataOut()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmDataOut));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.rad2 = new System.Windows.Forms.RadioButton();
this.rad1 = new System.Windows.Forms.RadioButton();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
this.groupBox1.Controls.Add(this.pictureBox2);
this.groupBox1.Controls.Add(this.pictureBox1);
this.groupBox1.Controls.Add(this.rad2);
this.groupBox1.Controls.Add(this.rad1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(264, 152);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox2.BackgroundImage")));
this.pictureBox2.Location = new System.Drawing.Point(24, 24);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(48, 48);
this.pictureBox2.TabIndex = 3;
this.pictureBox2.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(24, 80);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(48, 48);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// rad2
//
this.rad2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.rad2.ForeColor = System.Drawing.Color.Blue;
this.rad2.Location = new System.Drawing.Point(96, 88);
this.rad2.Name = "rad2";
this.rad2.Size = new System.Drawing.Size(160, 40);
this.rad2.TabIndex = 1;
this.rad2.Text = "以Excel电子表格式输出";
//
// rad1
//
this.rad1.Checked = true;
this.rad1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.rad1.ForeColor = System.Drawing.Color.Blue;
this.rad1.Location = new System.Drawing.Point(96, 24);
this.rad1.Name = "rad1";
this.rad1.Size = new System.Drawing.Size(144, 40);
this.rad1.TabIndex = 0;
this.rad1.TabStop = true;
this.rad1.Text = "以Word文档格式输出";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.Color.Red;
this.button1.Location = new System.Drawing.Point(24, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 3;
this.button1.Text = "导出";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button2.ForeColor = System.Drawing.Color.Red;
this.button2.Location = new System.Drawing.Point(160, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 4;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// frmDataOut
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255)));
this.ClientSize = new System.Drawing.Size(280, 206);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "frmDataOut";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "数据导出";
this.Load += new System.EventHandler(this.frmDataOut_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void frmDataOut_Load(object sender, System.EventArgs e)
{
string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;";
strConnection+=@"Data Source=Student.mdb";
objSqlConn=new OleDbConnection(strConnection);
user myUser = user.InitUser();
if(myUser.isPublicUser == false)
{
this.button1.Enabled=false;
}
if(myUser.isPublicUser==true)
{
this.button1.Enabled=true;
}
}
private void button1_Click(object sender, System.EventArgs e) //Word表和Excel表输出按钮
{
if(rad1.Checked)
{
objdataSet=new DataSet();
objOleDbDataAdapter=new OleDbDataAdapter();
objOleDbDataAdapter.SelectCommand=new OleDbCommand("Select * from Message",objSqlConn);
objOleDbDataAdapter.Fill(objdataSet,"Message");
objSqlConn.Open();
Object Nothing=System.Reflection.Missing.Value;
Word.Application WordApp=new Word.ApplicationClass(); //创建Word程序的对象
Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);//创建Word行的对象
WordDoc.Paragraphs.First.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,objdataSet.Tables["Message"].Rows.Count+1,12,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="学生编号"; table.Cell(1,7).Range.Text="婚姻状况"; //创建Word的表头
table.Cell(1,2).Range.Text="姓名"; table.Cell(1,8).Range.Text="政治面貌";
table.Cell(1,3).Range.Text="民族"; table.Cell(1,9).Range.Text="身份证号";
table.Cell(1,4).Range.Text="年龄"; table.Cell(1,10).Range.Text="籍贯";
table.Cell(1,5).Range.Text="文化程度"; table.Cell(1,11).Range.Text="入学类型";
table.Cell(1,6).Range.Text="所学专业"; table.Cell(1,12).Range.Text="其他专长";
for (int i=0;i<objdataSet.Tables["Message"].Rows.Count;i++ )
{
table.Cell(i+2,1).Range.Text=objdataSet.Tables["Message"].Rows[i]["学生编号"].ToString(); //Word表的数据
table.Cell(i+2,2).Range.Text=objdataSet.Tables["Message"].Rows[i]["姓名"].ToString();
table.Cell(i+2,3).Range.Text=objdataSet.Tables["Message"].Rows[i]["民族"].ToString();
table.Cell(i+2,4).Range.Text=objdataSet.Tables["Message"].Rows[i]["年龄"].ToString();
table.Cell(i+2,5).Range.Text=objdataSet.Tables["Message"].Rows[i]["文化程度"].ToString();
table.Cell(i+2,6).Range.Text=objdataSet.Tables["Message"].Rows[i]["所学专业"].ToString();
table.Cell(i+2,7).Range.Text=objdataSet.Tables["Message"].Rows[i]["婚姻状况"].ToString();
table.Cell(i+2,8).Range.Text=objdataSet.Tables["Message"].Rows[i]["政治面貌"].ToString();
table.Cell(i+2,9).Range.Text=objdataSet.Tables["Message"].Rows[i]["身份证号"].ToString();
table.Cell(i+2,10).Range.Text=objdataSet.Tables["Message"].Rows[i]["籍贯"].ToString();
table.Cell(i+2,11).Range.Text=objdataSet.Tables["Message"].Rows[i]["入学类型"].ToString();
table.Cell(i+2,12).Range.Text=objdataSet.Tables["Message"].Rows[i]["其他专长"].ToString();
}
WordDoc.Close(ref Nothing,ref Nothing,ref Nothing); //保存关闭
WordApp.Quit(ref Nothing,ref Nothing,ref Nothing); //停止
objSqlConn.Close();
}
else
{
try
{
objSqlConn.Open();
string s="select 姓名,学生编号,民族,年龄,文化程度,婚姻状况,政治面貌,身份证号,籍贯,入学类型,所学专业,其他专长,联系电话,电子邮箱,邮政编码,毕业学校,家庭住址 from Message";
OleDbCommand cmd=new OleDbCommand (s,objSqlConn);
OleDbDataReader dr=cmd.ExecuteReader ();
Excel.Application xlApp = new Excel.Application();
if(xlApp==null)
{
MessageBox.Show ("不能打开 Excel !");
return;
}
xlApp.Application.Workbooks.Add(true);
int row=2,fieldcount;
fieldcount=dr.FieldCount; //获取当前列数
for(int col=0;col<fieldcount;col++)
xlApp.Cells[1,col+1]=dr.GetName(col);//获取指定列的名称
while (dr.Read ())
{
for(int col=0;col<fieldcount;col++)
xlApp.Cells[row,col+1]=dr.GetValue(col).ToString();//写入Excel表
row++;
}
xlApp.Visible =true; //显示为真
xlApp=null;
}
catch(Exception ex )
{
MessageBox.Show (ex.Message );
}
finally
{
objSqlConn.Close();
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -