C# 报表绘制,打印与下载

-----------------------------------------页面html代码

----------------------绘制报表的代码

   private string displayTable()
    {
        if (drop_period.SelectedValue == "")
        {
            return "";
        }
        获取基础数据
        SqlParameter[] parameter = new SqlParameter[1];
        parameter[0] = new SqlParameter("@TeramID", drop_period.SelectedValue);
        DataTable dt_BaseData = DataAccess.GetDataSet("proc_BookProcurementPlan", parameter).Tables[0];
        校区信息
        StringBuilder strSqlbuilder = new StringBuilder();
        strSqlbuilder.Append("select schoolname  from puSchoolArea");
        DataTable dt_SchoolArea = new DataAccess().QueryTable(strSqlbuilder.ToString()).Tables[0];
        班级信息
        StringBuilder strSqlbuilder_class = new StringBuilder();
        strSqlbuilder_class.Append("select class_name,schoolarea,classmatenumber  from puClassName");
        DataTable dt_class = new DataAccess().QueryTable(strSqlbuilder_class.ToString()).Tables[0];
        课程班级教师信息
        StringBuilder strSqlbuilder_ClassCorse = new StringBuilder();
        strSqlbuilder_ClassCorse.Append("select Classnum,Classname,c_code as CourseCose,C_name as CourseName,term from ViewTeachingPlan");
        DataTable dt_ClassCorse = new DataAccess().QueryTable(strSqlbuilder_ClassCorse.ToString()).Tables[0];
        绘制表格
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.Append("<div id='div_print' style=\"vertical-align:middle; text-align:left;margin-left:auto; margin-right:auto;\" >");
        ///库存信息
        StringBuilder strSqlbuilder_Storage = new StringBuilder();
        strSqlbuilder_Storage.Append("select  * from View_SchoolAreaBook");
        DataTable dt_SchoolAreaBook = new DataAccess().QueryTable(strSqlbuilder_Storage.ToString()).Tables[0];
        /// 标题
        strBuilder.Append("<div style='width:100%;text-align:center;padding:3px'><b>");
        if (drop_period.SelectedValue != "")
        {
            strBuilder.Append(drop_period.SelectedItem.Text + "学年教材采购计划表");
            strBuilder.Append("</b></h3></div>");
        }
        #region  编号版本
        StringBuilder strSqlbuilder_Param = new StringBuilder();
        strSqlbuilder_Param.Append("select  *  from TeacherWorkPara");
        DataTable dt_TeacherWorkPara = new DataAccess().QueryTable(strSqlbuilder_Param.ToString()).Tables[0];
        if (dt_TeacherWorkPara.Rows.Count == 0)
        {
            strBuilder.Append("<div>");
            strBuilder.Append("<table width='100%'>");
            strBuilder.Append("<tr>");
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}</td>", "编号:");
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}</td>", "版本:");
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}</td>", "流水号:");
            strBuilder.Append("</tr>");
            strBuilder.Append("<table>");
            strBuilder.Append("</div>");
        }
        else
        {
            strBuilder.Append("<div>");
            strBuilder.Append("<table width='100%'>");
            strBuilder.Append("<tr>");
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}{1}</td>", "编号:", dt_TeacherWorkPara.Rows[0]["s1"].ToString());
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}{1}</td>", "版本:", dt_TeacherWorkPara.Rows[0]["s2"].ToString());
            strBuilder.AppendFormat("<td align='left' style='width:33%;'>{0}{1}</td>", "流水号:", DateTime.Now.ToString("MM-yyyy-dd"));
            strBuilder.Append("</tr>");
            strBuilder.Append("<table>");
            strBuilder.Append("</div>");
        }
        #endregion
        ///报表主体
        strBuilder.Append("<table bordercolor='black' cellpadding='3' cellspacing='0' border='1' style='border:solid 1px gray;border-collapse:collapse;width:auto; margin-left:auto; margin-right:auto;'>");
        strBuilder.Append("<tr>");
        strBuilder.Append("<td  >课程</>");
        strBuilder.Append("<td  >选用教材</>");
        strBuilder.Append("<td  >出版社</>");
        foreach (DataRow row in dt_SchoolArea.Rows)
        {
            strBuilder.Append("<td >" + row["schoolname"].ToString() + "使用班级</><td>使用人数(人)</td>");
        }
        strBuilder.Append("<td>使用人数(人)</>");
        strBuilder.Append("<td>使用总人数(人)</>");
        strBuilder.Append("<td>库存教材(本)</>");
        strBuilder.Append("<td>应定教材(本)</>");
        strBuilder.Append("<td>订购单位</>");
        strBuilder.Append("</tr>");
        foreach (DataRow row_base in dt_BaseData.Rows)
        {
            strBuilder.Append("<tr>");
            //课程
            strBuilder.AppendFormat("<td>{0}</td>", row_base["CourseName"].ToString());
            //选用教材
            String str = row_base["BookName"].ToString() + "<br/>" + row_base["Price"].ToString() + "<br/>" + row_base["ISBN"].ToString();
            strBuilder.AppendFormat("<td>{0}</td>", str);
            //出版社
            ///总人数
            strBuilder.AppendFormat("<td>{0}</td>", row_base["PublishName"].ToString());
            int totalnum = 0;
            foreach (DataRow row_schoolarea in dt_SchoolArea.Rows)
            {
                ///根据校区,课程,学期,查出班级信息
                DataRow[] rowcol_c = dt_ClassCorse.Select("term='" + drop_period.SelectedValue + "' and CourseCose='" + row_schoolarea["CourseCode,"].ToString() + "'");
                ///获取班级编号列表
                StringBuilder strSqlbuilder_classids = new StringBuilder();
                foreach (DataRow row_class in rowcol_c)
                {
                    strSqlbuilder_classids.Append("'" + row_class["Classnum"].ToString() + "',");
                }
                String ids = "";
                if (strSqlbuilder_classids.Length != 0)
                {
                    ids = strSqlbuilder_classids.ToString();
                    ids = ids.Substring(0, ids.Length - 1);
                }
                if (ids == "")
                {
                    continue;
                }
                DataRow[] rowcol = dt_class.Select("schoolarea='" + row_schoolarea["code"].ToString() + "'");
                StringBuilder classbuilder = new StringBuilder();
                int classlevelnum = 0;
                foreach (DataRow row_class in rowcol)
                {
                    totalnum += Convert.ToInt32(row_class["classmatenumber"]);
                    classlevelnum += Convert.ToInt32(row_class["classmatenumber"]);
                    classbuilder.Append(row_class["class_name"].ToString() + "<br/>");
                }
                //使用班级
                strBuilder.AppendFormat("<td>{0}</td>", classbuilder.ToString());
                //使用人数
                strBuilder.AppendFormat("<td>{0}</td>", classlevelnum.ToString());
            }
            //使用总数
            strBuilder.AppendFormat("<td>{0}</td>", totalnum.ToString());
            //库存教材
            int booknum = 0;
            DataRow[] rowcolnum = dt_SchoolAreaBook.Select("BookNum='" + row_base["BookNum"].ToString() + "'");
            foreach (DataRow row_bookstorage in rowcolnum)
            {
                booknum += Convert.ToInt32(row_bookstorage["Num"].ToString());
            }
            strBuilder.AppendFormat("<td>{0}</td>", booknum);
            //应定教材
            strBuilder.AppendFormat("<td>{0}</td>", (totalnum - booknum).ToString());
            //订购单位
            strBuilder.AppendFormat("<td>{0}</td>", row_base["SupplierName "].ToString());
            strBuilder.Append("</tr>");
        }
        strBuilder.Append("</table>");
        strBuilder.Append("</div>");
        return strBuilder.ToString();
    }

 

------生成Excel并下载

   void CreateExcel() 
    {
        string fileName = "教材采购计划表.xls";
        Response.Clear();
        HttpContext.Current.Response.Write("<html xmlns:x='urn:schemas-microsoft-com:office:excel'>");
        HttpContext.Current.Response.Write("<head>");
        HttpContext.Current.Response.Write("<!--[if gte mso ]><xml>");
        HttpContext.Current.Response.Write("<x:ExcelWorkbook>");
        HttpContext.Current.Response.Write("<x:ExcelWorksheets>");
        HttpContext.Current.Response.Write("<x:ExcelWorksheet>");
        HttpContext.Current.Response.Write("<x:Name>" + "教材采购计划表" + "</x:Name>");
        HttpContext.Current.Response.Write("<x:WorksheetOptions>");
        HttpContext.Current.Response.Write("<x:Print>");
        HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>");
        HttpContext.Current.Response.Write("</x:Print>");
        HttpContext.Current.Response.Write("</x:WorksheetOptions>");
        HttpContext.Current.Response.Write("</x:ExcelWorksheet>");
        HttpContext.Current.Response.Write("</x:ExcelWorksheets>");
        HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
        HttpContext.Current.Response.Write("</xml>");
        HttpContext.Current.Response.Write("<![endif]--> ");
        HttpContext.Current.Response.Write("</head>");
        HttpContext.Current.Response.Write("<body>");
        Response.Buffer = true;
        Response.Charset = "GB2312";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
        Response.ContentType = "application/ms-execl";//设置输出文件类型为excel文件。 
        EnableViewState = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        oStringWriter.WriteLine(lbl_displayTable.Text);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        oStringWriter.WriteLine();
        oStringWriter.Close();
        string filePath = HttpContext.Current.Server.MapPath("..") + fileName;
        DownFile(fileName, filePath);//下载页面
        Response.Write(oStringWriter.ToString());
        HttpContext.Current.Response.Write("</body>");
        HttpContext.Current.Response.Write("</html>");
        HttpContext.Current.Response.End();
    }
    bool DownFile(string fileName, string fullPath)
    {
        try
        {
            HttpContext.Current.Response.ContentType = "application/octet-stream";

            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
            System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
            long fLen = fs.Length;
            int size = 102400;//每100K同时下载数据 
            byte[] readData = new byte[size];//指定缓冲区的大小 
            if (size > fLen) size = Convert.ToInt32(fLen);
            long fPos = 0;
            bool isEnd = false;
            while (!isEnd)
            {
                if ((fPos + size) > fLen)
                {
                    size = Convert.ToInt32(fLen - fPos);
                    readData = new byte[size];
                    isEnd = true;
                }
                fs.Read(readData, 0, size);//读入一个压缩块 
                HttpContext.Current.Response.BinaryWrite(readData);
                fPos += size;
            }
            fs.Close();
            System.IO.File.Delete(fullPath);
            return true;
        }
        catch
        {
            return false;
        }
    }


 


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值