Net开发日记1
前天几对于一个把数据库里的数据想写入一个Word文档,不知道怎么做,今天终于搞定,也可以不用Word组件,用流很方便,Main.aspx.cs代码如下:
namespace tiku
{
/// <summary>
/// main 的摘要说明。
/// </summary>
public class main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton shou;
protected System.Web.UI.WebControls.LinkButton auto;
protected System.Web.UI.WebControls.Panel plselect;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList ddlcourse;
protected System.Web.UI.WebControls.DropDownList ddldiff;
protected System.Web.UI.WebControls.DropDownList ddllx;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton select;
db db = new db();
SqlConnection con;
DataSet ds;
SqlDataAdapter sda;
string diff,lx,course,a_diff,a_course;
protected System.Web.UI.WebControls.Panel plshou;
protected System.Web.UI.WebControls.DataGrid dgsuanze;
protected System.Web.UI.WebControls.Button zujianshou;
protected System.Web.UI.WebControls.DataGrid dgtiankong;
protected System.Web.UI.WebControls.Panel plauto;
protected System.Web.UI.WebControls.DropDownList ddlauto_course;
protected System.Web.UI.WebControls.DropDownList ddlauto_diff;
protected System.Web.UI.WebControls.Button btn_aotu;
protected System.Web.UI.WebControls.DataGrid dgjianda;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (Session["log"]=="OK")
{
this.plselect.Visible=false;
this.plauto.Visible=false;
this.plshou.Visible=false;
}
else
{
Response.Redirect("error.aspx");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.select.Click += new System.EventHandler(this.select_Click);
this.shou.Click += new System.EventHandler(this.shou_Click);
this.auto.Click += new System.EventHandler(this.auto_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.zujianshou.Click += new System.EventHandler(this.zujianshou_Click);
this.btn_aotu.Click += new System.EventHandler(this.btn_aotu_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void BindToDGxuanze()
{
con=db.CreateCon();
sda=new SqlDataAdapter("select * from Ti where Tlx='选择'",con);
ds=new DataSet();
sda.Fill(ds,"xuanze");
this.dgsuanze.DataSource=ds.Tables["xuanze"];
this.dgsuanze.DataKeyField="TID";
this.dgsuanze.DataBind();
con.Close();
}
public void BindToTiankong()
{
con=db.CreateCon();
sda=new SqlDataAdapter("select * from Ti where Tlx='填空'",con);
ds= new DataSet();
sda.Fill(ds,"tian");
this.dgtiankong.DataSource=ds.Tables["tian"];
this.dgtiankong.DataKeyField="TID";
this.dgtiankong.DataBind();
con.Close();
}
public void BindToJianda()
{
con=db.CreateCon();
sda=new SqlDataAdapter("select * from Ti where Tlx='简答'",con);
ds = new DataSet();
sda.Fill(ds,"jian");
this.dgjianda.DataSource=ds.Tables["jian"];
this.dgjianda.DataKeyField="TID";
this.dgjianda.DataBind();
}
//手动组卷
private void shou_Click(object sender, System.EventArgs e)
{
if (Session["log"]=="OK")
{
this.plauto.Visible=false;
this.plselect.Visible=false;
this.plshou.Visible=true;
this.BindToDGxuanze();
this.BindToJianda();
this.BindToTiankong();
}
else
{
Response.Redirect("error.aspx");
}
}
//试题查询
private void select_Click(object sender, System.EventArgs e)
{
if (Session["log"]=="OK")
{
this.plselect.Visible=true;
this.plauto.Visible=false;
this.plshou.Visible=false;
}
else
{
Response.Redirect("error.aspx");
}
}
//智能组卷
private void auto_Click(object sender, System.EventArgs e)
{
if (Session["log"]=="OK")
{
this.plauto.Visible=true;
this.plselect.Visible=false;
this.plshou.Visible=false;
}
else
{
Response.Redirect("error.aspx");
}
}
//开始查询
private void Button1_Click(object sender, System.EventArgs e)
{
if (Session["log"]=="OK")
{
if (this.TextBox1.Text=="")
{
Response.Write("<script>alert('知识点不能为空!')</script>");
this.plselect.Visible=true;
}
else
{
if (this.ddlcourse.SelectedIndex==0||this.ddldiff.SelectedIndex==0||this.ddllx.SelectedIndex==0)
{
Response.Write("<script>alert('请选择条件!')</script>");
this.plselect.Visible=true;
}
else
{
con=db.CreateCon();
#region 课程选择
if (this.ddlcourse.SelectedIndex==1)
{
course="外科";
}
else if (this.ddlcourse.SelectedIndex==2)
{
course="内科";
}
else
{
course="五管科";
}
#endregion
#region 难度选择
if(this.ddldiff.SelectedIndex==1)
{
diff="易";
}
else if(this.ddldiff.SelectedIndex==2)
{
diff="中";
}
else
{
diff="难";
}
#endregion
#region 题型选择
if (this.ddllx.SelectedIndex==1)
{
lx="选择";
}
else if(this.ddllx.SelectedIndex==2)
{
lx="填空";
}
else
{
lx="简答";
}
#endregion
try
{
sda=new SqlDataAdapter("select * from Ti where Tlx='"+lx+"' and Diff='"+diff+"' and Course='"+course+"' and Main like '%"+this.TextBox1.Text+"%'",con);
DataSet dss= new DataSet();
sda.Fill(dss);
this.DataGrid1.DataSource=dss;
this.DataGrid1.DataKeyField="TID";
this.DataGrid1.DataBind();
con.Close();
this.plselect.Visible=true;
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
else
{
Response.Redirect("error.aspx");
}
}
//开始组卷
private void btn_aotu_Click(object sender, System.EventArgs e)
{
if (Session["log"]=="OK")
{
if (this.ddlauto_course.SelectedIndex==0||this.ddlauto_diff.SelectedIndex==0)
{
Response.Write("<script>alert('请选择条件!')</script>");
}
else
{
#region 课程选择
if (this.ddlauto_course.SelectedIndex==1)
{
a_course="外科";
}
else if (this.ddlauto_course.SelectedIndex==2)
{
a_course="内科";
}
else
{
a_course="五管科";
}
#endregion
#region 难度选择
if(this.ddlauto_diff.SelectedIndex==1)
{
a_diff="易";
}
else if(this.ddlauto_diff.SelectedIndex==2)
{
a_diff="中";
}
else
{
a_diff="难";
}
#endregion
con= db.CreateCon();
#region A卷
StreamWriter swA= new StreamWriter("d://试题.doc",false,Encoding.GetEncoding("gb2312"));
swA.WriteLine(" 南方医院影像诊断教研室自动卷A卷");
swA.Close();
StreamWriter swauto= new StreamWriter("d://试题.doc",true,Encoding.GetEncoding("gb2312"));
#region A卷选择
sda=new SqlDataAdapter("select top 20 * from Ti where Tlx='选择' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
ds= new DataSet();
sda.Fill(ds,"b");
swauto.WriteLine("一.选择题.");
try
{
swauto.WriteLine("1."+ds.Tables[0].Rows[0][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("2."+ds.Tables[0].Rows[1][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("3."+ds.Tables[0].Rows[2][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("4."+ds.Tables[0].Rows[3][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("5."+ds.Tables[0].Rows[4][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("6."+ds.Tables[0].Rows[5][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("7."+ds.Tables[0].Rows[6][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("8."+ds.Tables[0].Rows[7][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("9."+ds.Tables[0].Rows[8][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("10."+ds.Tables[0].Rows[9][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("11."+ds.Tables[0].Rows[10][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("12."+ds.Tables[0].Rows[11][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("13."+ds.Tables[0].Rows[12][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("14."+ds.Tables[0].Rows[13][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("15."+ds.Tables[0].Rows[14][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("16."+ds.Tables[0].Rows[15][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("17."+ds.Tables[0].Rows[16][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("18."+ds.Tables[0].Rows[17][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("19."+ds.Tables[0].Rows[18][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("20."+ds.Tables[0].Rows[19][4].ToString());
}
catch
{
}
#endregion
#region A卷填空
swauto.WriteLine("二.填空题.");
SqlDataAdapter sdaA=new SqlDataAdapter("select top 10 * from Ti where Tlx='填空' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
DataSet dsdA= new DataSet();
sdaA.Fill(dsdA,"b");
try
{
swauto.WriteLine("1."+dsdA.Tables["b"].Rows[0][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("2."+dsdA.Tables["b"].Rows[1][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("3."+dsdA.Tables["b"].Rows[2][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("4."+dsdA.Tables["b"].Rows[3][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("5."+dsdA.Tables["b"].Rows[4][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("6."+dsdA.Tables["b"].Rows[5][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("7."+dsdA.Tables["b"].Rows[6][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("8."+dsdA.Tables["b"].Rows[7][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("9."+dsdA.Tables["b"].Rows[8][4].ToString());
}
catch
{
}
try
{
swauto.WriteLine("10."+dsdA.Tables["b"].Rows[9][4].ToString());
}
catch
{
}
#endregion
#region A卷简答
SqlDataAdapter sdaAA=new SqlDataAdapter("select top 5 * from Ti where Tlx='简答' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
DataSet dsaAA=new DataSet();
sdaAA.Fill(dsaAA);
swauto.WriteLine("三.简答题.");
try
{
swauto.WriteLine("1."+dsaAA.Tables["b"].Rows[0][4].ToString());
}
catch
{
}
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
try
{
swauto.WriteLine("2."+dsaAA.Tables["b"].Rows[1][4].ToString());
}
catch
{
}
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
try
{
swauto.WriteLine("3."+dsaAA.Tables["b"].Rows[2][4].ToString());
}
catch
{
}
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
try
{
swauto.WriteLine("4."+dsaAA.Tables["b"].Rows[3][4].ToString());
}
catch
{
}
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
try
{
swauto.WriteLine("5."+dsaAA.Tables["b"].Rows[4][4].ToString());
}
catch
{
}
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
swauto.WriteLine("");
#endregion
swauto.Close();
// Response.Redirect("down.aspx?id=a.doc");
#endregion
this.plauto.Visible=true;
#region B卷
StreamWriter swB= new StreamWriter("d://自动卷B.doc",false,Encoding.GetEncoding("gb2312"));
swB.WriteLine(" 南方医院影像诊断教研室自动卷B卷");
swB.Close();
StreamWriter swautoB= new StreamWriter("d://试题.doc",true,Encoding.GetEncoding("gb2312"));
#region B卷选择
SqlDataAdapter sdaB=new SqlDataAdapter("select top 20 * from Ti where Tlx='选择' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
DataSet dscB= new DataSet();
sdaB.Fill(dscB,"c");
swautoB.WriteLine("一.选择题.");
try
{
swautoB.WriteLine("1."+dscB.Tables["c"].Rows[0][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("2."+dscB.Tables["c"].Rows[1][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("3."+dscB.Tables["c"].Rows[2][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("4."+dscB.Tables["c"].Rows[3][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("5."+dscB.Tables["c"].Rows[4][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("6."+dscB.Tables["c"].Rows[5][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("7."+dscB.Tables["c"].Rows[6][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("8."+dscB.Tables["c"].Rows[7][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("9."+dscB.Tables["c"].Rows[8][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("10."+dscB.Tables["c"].Rows[9][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("11."+dscB.Tables["c"].Rows[10][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("12."+dscB.Tables["c"].Rows[11][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("13."+dscB.Tables["c"].Rows[12][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("14."+dscB.Tables["c"].Rows[13][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("15."+dscB.Tables["c"].Rows[14][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("16."+dscB.Tables["c"].Rows[15][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("17."+dscB.Tables["c"].Rows[16][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("18."+dscB.Tables["c"].Rows[17][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("19."+dscB.Tables["c"].Rows[18][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("20."+dscB.Tables["c"].Rows[19][4].ToString());
}
catch
{
}
#endregion
#region B卷填空
SqlDataAdapter sdaBB=new SqlDataAdapter("select top 10 * from Ti where Tlx='填空' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
DataSet dsdB= new DataSet();
sdaBB.Fill(dsdB,"c");
swautoB.WriteLine("二.填空题.");
try
{
swautoB.WriteLine("1."+dsdB.Tables["c"].Rows[0][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("2."+dsdB.Tables["c"].Rows[1][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("3."+dsdB.Tables["c"].Rows[2][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("4."+dsdB.Tables["c"].Rows[3][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("5."+dsdB.Tables["c"].Rows[4][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("6."+dsdB.Tables["c"].Rows[5][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("7."+dsdB.Tables["c"].Rows[6][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("8."+dsdB.Tables["c"].Rows[7][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("9."+dsdB.Tables["c"].Rows[8][4].ToString());
}
catch
{
}
try
{
swautoB.WriteLine("10."+dsdB.Tables["c"].Rows[9][4].ToString());
}
catch
{
}
#endregion
#region B卷简答
SqlDataAdapter sdaVB=new SqlDataAdapter("select top 5 * from Ti where Tlx='简答' and Diff='"+a_diff+"' and Course='"+a_course+"' order by newid()",con);
DataSet dsaB=new DataSet();
sdaVB.Fill(dsaB,"c");
con.Close();
swautoB.WriteLine("三.间答题.");
try
{
swautoB.WriteLine("1."+dsaB.Tables["c"].Rows[0][4].ToString());
}
catch
{
}
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
try
{
swautoB.WriteLine("2."+dsaB.Tables["c"].Rows[1][4].ToString());
}
catch
{
}
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
try
{
swautoB.WriteLine("3."+dsaB.Tables["c"].Rows[2][4].ToString());
}
catch
{
}
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
try
{
swautoB.WriteLine("4."+dsaB.Tables["c"].Rows[3][4].ToString());
}
catch
{
}
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
try
{
swautoB.WriteLine("5."+dsaB.Tables["c"].Rows[4][4].ToString());
}
catch
{
}
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
swautoB.WriteLine("");
#endregion
swautoB.Close();
Response.Redirect("down.aspx?id=a.doc");
#endregion
}
}
else
{
Response.Redirect("error.aspx");
}
}
private void zujianshou_Click(object sender, System.EventArgs e)//手动卷
{
StreamWriter sw= new StreamWriter("d://试题.doc",false, Encoding.GetEncoding("gb2312"));
sw.WriteLine(" 南方医院影像诊断教研室手动卷");
sw.Close();
string xuanze,tiankong,jianda;
string str=",";
char[] st= str.ToCharArray();
DataSet dsxuanze= new DataSet();
StreamWriter swshou= new StreamWriter("d://试题.doc",true, Encoding.GetEncoding("gb2312"));
swshou.WriteLine("一.选择题");
int b=0;
foreach (System.Web.UI.WebControls.DataGridItem dl in this.dgsuanze.Items)
{
System.Web.UI.WebControls.CheckBox chkxuanze=(System.Web.UI.WebControls.CheckBox)dl.FindControl("CheckBox1");
if (chkxuanze.Checked)
{
b=b+1;
xuanze=dl.Cells[0].Text;
con=db.CreateCon();
int id=int.Parse(xuanze);
SqlDataAdapter sda1= new SqlDataAdapter("select * from Ti where TID="+id,con);
sda1.Fill(dsxuanze,"xuan");
swshou.WriteLine(b+"."+dsxuanze.Tables["xuan"].Rows[0][4].ToString());
con.Close();
}
}
swshou.Close();
DataSet ds0= new DataSet();
swshou= new StreamWriter("d://试题.doc",true, Encoding.GetEncoding("gb2312"));
swshou.WriteLine("二.填空题");
int i=0;
foreach (System.Web.UI.WebControls.DataGridItem dl in this.dgtiankong.Items)
{
System.Web.UI.WebControls.CheckBox chktiankong=(System.Web.UI.WebControls.CheckBox)dl.FindControl("CheckBox2");
if (chktiankong.Checked)
{ i=i+1;
tiankong=dl.Cells[0].Text;
con = db.CreateCon();
int id2 = int.Parse(tiankong);
SqlDataAdapter sda2= new SqlDataAdapter("select * from Ti where TID="+id2,con);
sda2.Fill(ds0,"tian");
swshou.WriteLine(i+"."+ds0.Tables["tian"].Rows[0][4].ToString());
con.Close();
}
}
swshou.Close();
DataSet ds1= new DataSet();
swshou= new StreamWriter("d://试题.doc",true, Encoding.GetEncoding("gb2312"));
swshou.WriteLine("三.解答题");
int j=0;
foreach (System.Web.UI.WebControls.DataGridItem dl in this.dgjianda.Items)
{
System.Web.UI.WebControls.CheckBox chkjianda=(System.Web.UI.WebControls.CheckBox)dl.FindControl("CheckBox3");
if (chkjianda.Checked)
{
j=j+1;
jianda=dl.Cells[0].Text;
con = db.CreateCon();
int id3 = int.Parse(jianda);
sda= new SqlDataAdapter("select * from Ti where TID="+id3,con);
sda.Fill(ds1,"jian");
swshou.WriteLine(j+"."+ds1.Tables["jian"].Rows[0][4].ToString());
swshou.WriteLine("");
swshou.WriteLine("");
swshou.WriteLine("");
swshou.WriteLine("");
swshou.WriteLine("");
swshou.Close();
con.Close();
}
}
swshou.Close();
Response.Redirect("down.aspx?id=a.doc");
this.plshou.Visible=true;
}
}
}
只要能想就能做!