http://www.codeproject.com/aspnet/AntiAuto.asp

解决方案 »

  1.   

    你要画什么表格?如果是一般的表格不需要这个Drawing呀!直接使用服务器Table或者使用Response.Write("<table>....................</table>");
    如果是要画一幅图片才会用到Drawing呀!
      

  2.   

    //Set For FOnt
    System.Drawing.Font fontLegend=new System.Drawing.Font("Verdana",11,FontStyle.Bold);
    System.Drawing.Font fontTitle=new System.Drawing.Font("Verdana",9);
    System.Drawing.Font fontHead=new System.Drawing.Font("Verdana",9,FontStyle.Bold);
    int width=489;//白色背景宽
    int height =264;//白色背景高
    const int blankspace=16;
    int titleHeight=fontLegend.Height+blankspace;
    string ImgPath=Server.MapPath(".")+"\\AccountImage\\"+"test.gif";
    Bitmap objbitmap=new Bitmap(width,height);
    Graphics objgraphics=Graphics.FromImage(objbitmap);
    objgraphics.FillRectangle(new SolidBrush(Color.White),0,0,width,height);
    string title="Papers Table From "+StartDate+" To "+EndDate;
    SolidBrush blackbrush=new SolidBrush(Color.Black);
    StringFormat stringFormat=new StringFormat();
    stringFormat.Alignment=StringAlignment.Center;
    stringFormat.LineAlignment=StringAlignment.Center;
    //Draw Grid
    int xHeight=titleHeight+fontTitle.Height+10;
    int xxHeight=fontTitle.Height+10;
    Pen objPen=new Pen(Color.Black,1);
    objgraphics.DrawString(title,fontLegend,blackbrush,new Rectangle(0,0,width,titleHeight),stringFormat);
    objgraphics.DrawRectangle(objPen,5,titleHeight,width-10,height-titleHeight-6);
    int i=0;
    for(i=1;i<=8;i++)
    {
    objgraphics.DrawLine(objPen,5,xHeight,width-5,xHeight);
    xHeight+=xxHeight;
    }
    int xwidth=width/3;
    objgraphics.DrawLine(objPen,5+xwidth,titleHeight,5+xwidth,height-6);
    objgraphics.DrawLine(objPen,5+xwidth*2,titleHeight,5+xwidth*2,height-6);
    //Get Data From DataBase
    string strConnection = "server="+ConfigurationSettings.AppSettings["ServerName"];
    strConnection += ";uid="+ConfigurationSettings.AppSettings["UserName"];
    strConnection += ";password="+ConfigurationSettings.AppSettings["Password"];
    strConnection += ";initial catalog="+ConfigurationSettings.AppSettings["Database"];
    SqlConnection MyConnection = new SqlConnection(strConnection);
    string strCommand="select PaperState,count(distinct PID) as co from [tb_paper] where cast(UploadDate as datetime) between  '"+StartDate+"' and '"+EndDate+"' Group by PaperState";
    SqlCommand Comm=new SqlCommand(strCommand,MyConnection);
    MyConnection.Open();
    SqlDataReader dr=Comm.ExecuteReader();
    string[] leftString=new string[7];
    long[] Num=new long[7];
    string[] Percent=new string[7];
    i=0;
    long Total=0;
    while(dr.Read())
    {
    leftString[i]=GetState(dr["PaperState"].ToString());
    Num[i]=long.Parse(dr["co"].ToString());
    Total+=Num[i];
    i++;
    }
    for(i=0;i<7;i++)
    {
    double tempPer=(double)Num[i]/(double)Total;
    Percent[i]=tempPer.ToString("##.##%");
    }
    //Begin to Draw String
    //Draw Table Head
    string temp="Number";
    objgraphics.DrawString(temp,fontHead,blackbrush,new Rectangle(5+xwidth,titleHeight,xwidth,xxHeight),stringFormat);
    temp="Percent";
    objgraphics.DrawString(temp,fontHead,blackbrush,new Rectangle(5+xwidth*2,titleHeight,xwidth,xxHeight),stringFormat);
    //Draw Table Left
    for(i=0;i<7;i++)
    objgraphics.DrawString(leftString[i],fontTitle,blackbrush,new Rectangle(5,xxHeight*(i+1)+titleHeight,xwidth,xxHeight),stringFormat);
    //Draw Number 
    for(i=0;i<7;i++)
    objgraphics.DrawString(Num[i].ToString(),fontTitle,blackbrush,new Rectangle(5+xwidth,xxHeight*(i+1)+titleHeight,xwidth,xxHeight),stringFormat);
    //Draw Percent
    for(i=0;i<7;i++)
    objgraphics.DrawString(Percent[i],fontTitle,blackbrush,new Rectangle(5+xwidth*2,xxHeight*(i+1)+titleHeight,xwidth,xxHeight),stringFormat);
    //Draw Foot
    temp="Total";
    objgraphics.DrawString(temp,fontHead,blackbrush,new Rectangle(5,xxHeight*8+titleHeight,xwidth,xxHeight),stringFormat);
    objgraphics.DrawString(Total.ToString(),fontHead,blackbrush,new Rectangle(5+xwidth,xxHeight*8+titleHeight,xwidth,xxHeight),stringFormat);
    objgraphics.DrawString("100%",fontHead,blackbrush,new Rectangle(5+xwidth*2,xxHeight*8+titleHeight,xwidth,xxHeight),stringFormat);
    //Save
    objbitmap.Save(ImgPath,System.Drawing.Imaging.ImageFormat.Gif);
    objgraphics.Dispose();
    objbitmap.Dispose();
    string ImgPath2="AccountImage/test.gif";
    string ImgTag="<img src='"+ImgPath2+"'>";
    ChartHolder.Controls.Add(new LiteralControl(ImgTag));
    //