using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
///CommonFunction 的摘要说明
/// </summary>
public partial class CommonFunction
{
    /// <summary>
    /// 基本报表
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_XR(string image)
    {

        System.Web.HttpContext web = System.Web.HttpContext.Current;
        string str;

        //if (table == null) return;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        int table_Columns_Count = 5;
        string table_TableName = "标题";
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + table_Columns_Count + "\">" + table_TableName + "</td>");
        web.Response.Write("<tr>");
        web.Response.Write("<tr>");
        //SPC.WebUI.PageBase.UrlBase
        str = "<td   align=\"center\" valign=\"middle\" colspan=\"" + table_Columns_Count + "\">" +
            "<img alt=\"\" src=\"F:/MES系统工作/UA格特拉克项目/B5ASpc2010/SPC_Web/TempImages/chart_a78fa46a868743a494aaba7f3ed8c456_2.png\" />" + "</td>";
        //str = "<td   align=\"center\" valign=\"middle\" colspan=\"" + table_Columns_Count + "\">" +
        //    "<img alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/TempImages/chart_a78fa46a868743a494aaba7f3ed8c456_2.png\" />" + "</td>";
        //str = "<td   align=\"center\" valign=\"middle\" colspan=\"" + table_Columns_Count + "\">" +
        //    "<img alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/TempImages/ChartPic_#SEQ(300,3)\" />" + "</td>";

        string in_str = "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\" \" />";
        str = "<td   align=\"center\" valign=\"middle\" colspan=\"" + table_Columns_Count + "\">" +
       in_str + "</td>";
        web.Response.Write(str);
        web.Response.Write("<tr>");
        ////输出表字段
        //for (int i = 0; i < table.Columns.Count; i++)
        //{
        //    web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");
        //}
        //foreach (DataRow dataRow in table.Rows)
        //{
        //    web.Response.Write("<tr>");

        //    for (int i = 0; i < table.Columns.Count; i++)
        //    {
        //        if (Convert.ToInt32(dataRow["合格标志"]) == 0)
        //        {
        //            web.Response.Write("<td  style =\" background-color:Red\">" + dataRow[i] + "</td>");
        //        }
        //        else
        //        {
        //            web.Response.Write("<td>" + dataRow[i] + "</td>");
        //        }
        //    }
        //    web.Response.Write("</tr>");

        //}
        web.Response.Write("</table>");

        web.Response.End();
    }

    //2012_04_17 chenghai
    /// <summary>
    /// 基本报表_图片
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_tupian(string path, string tupian_name)
    {


        int cloumn = 1;
        int with = 800;
        int height = 600;
        string str;
        System.Web.HttpContext web = System.Web.HttpContext.Current;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + cloumn + "\">" + tupian_name + "</td>");
        web.Response.Write("</tr>");
        web.Response.Write("<tr>");

        str = "<td   align=\"center\" valign=\"middle\" >" +
       // "<img   alt=\"\" src=\""  + "/Chart_excel/" + path + "\"   width=\" " + with + " \" height= \" " + height + " \"  />" + "</td>";
        "<img   alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/Chart_excel/" + path + "\"   width=\" " + with + " \" height= \" " + height + " \"  />" + "</td>";

        web.Response.Write(str);
        web.Response.Write("</tr>");
        //输出表字段
        web.Response.Write("</table>");

        web.Response.End();



    }
    //2012_04_17 chenghai
    /// <summary>
    /// 基本报表_图片_数据
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_tupian_and_data(string path, DataTable table)
    {
        //导出excel 时 每个字段宽度 统一 设置为100
        int excel_biaoge_width = 100;
        //图片的高度
        int height = 400;
        //图片所占用的excel表的行数
        int excel_tupian_gaodu_hangshu = 22;
        string str;
        if (table == null) return;
        System.Web.HttpContext web = System.Web.HttpContext.Current;


        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\">" + table.TableName + "</td>");
        web.Response.Write("</tr>");
        web.Response.Write("<tr>");

        //string webPath = SPC.WebUI.PageBase.UrlBase;
        //webPath = webPath.Remove(webPath.Length - 1, 1);

        str = "<td   align=\"center\" valign=\"middle\" rowspan=\"" + excel_tupian_gaodu_hangshu + "\" colspan=\"" + table.Columns.Count + "\">" +
        "<img   alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/Chart_excel/" + path + "\"   width=\" " + excel_biaoge_width * table.Columns.Count + " \" height= \" " + height + " \"  />" + "</td>";

        web.Response.Write(str);
        web.Response.Write("</tr>");
        for (int i = 0; i < excel_tupian_gaodu_hangshu; i++)
        {
            web.Response.Write("<tr>");

        }
        //输出表字段

        for (int i = 0; i < table.Columns.Count; i++)
        {
            web.Response.Write("<td  width=\"" + excel_biaoge_width + "\"  align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");
        }
        foreach (DataRow dataRow in table.Rows)
        {
            web.Response.Write("<tr>");

            for (int i = 0; i < table.Columns.Count; i++)
            {
                web.Response.Write("<td>" + dataRow[i] + "</td>");
            }
            web.Response.Write("</tr>");

        }
        web.Response.Write("</table>");

        web.Response.End();

    }
    /// <summary>
    /// 基本报表
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_Query2(DataTable table)
    {

        System.Web.HttpContext web = System.Web.HttpContext.Current;
        string str;
        if (table == null) return;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        string tableName;
        tableName = table.TableName;
        ///////////////////////////////////////////
        //图标
        web.Response.Write("<table>");
        web.Response.Write("<tr>");
        str = "<td>" +
            //"<img alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/images/report_icon.jpg\" />" + "</td>";
        "<img alt=\"\" src=\""  + "/images/report_icon.jpg\" />" + "</td>";
        web.Response.Write(str);
        web.Response.Write("</tr>");
        ///////////////////////////////////////////
        //表头
        web.Response.Write("<tr>");
        web.Response.Write("<td    align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\">" + table.TableName + "</td>");
        web.Response.Write("</tr>");
        web.Response.Write("</table>");

        web.Response.Write("<table border=1>");

        //输出表字段
        for (int i = 0; i < table.Columns.Count; i++)
        {
            web.Response.Write("<td  align=\"center\" valign=\"middle\">" + GetColumnName_Ver(table.Columns[i].ColumnName) + "</td>");
        }
        string columnName;
        foreach (DataRow dataRow in table.Rows)
        {
            web.Response.Write("<tr>");

            for (int i = 0; i < table.Columns.Count; i++)
            {
                columnName = table.Columns[i].ColumnName;
                switch (tableName)
                {
                    case "不合格质量数据报表":
                        switch (columnName)
                        {
                            case "数据":
                                web.Response.Write("<td  style =\" background-color:Red\">" + dataRow[i] + "</td>");
                                break;
                            default:
                                web.Response.Write("<td>" + dataRow[i] + "</td>");
                                break;
                        }
                        break;
                    case "工序能力":
                        switch (columnName)
                        {
                            case "能力状况":
                                switch (Convert.ToInt32(dataRow["能力状况"]))
                                {
                                    case 1:
                                        web.Response.Write("<td  style =\" background-color:Green\">" + "" + "</td>");
                                        break;
                                    case 2:
                                        web.Response.Write("<td  style =\" background-color:Yellow\">" + "" + "</td>");
                                        break;
                                    case 3:
                                        web.Response.Write("<td  style =\" background-color:Red\">" + "" + "</td>");
                                        break;
                                }
                                break;
                            default:
                                web.Response.Write("<td>" + dataRow[i] + "</td>");
                                break;
                        }
                        break;
                    default:
                        switch (columnName)
                        {
                            case "工序状况":
                                //工序状况=0 不合格（NOK）；=1 合格（OK）
                                if (Convert.ToInt32(dataRow["工序状况"]) == 0)
                                {
                                    web.Response.Write("<td  style =\" background-color:Red\">" + "NOK" + "</td>");
                                }
                                else
                                {
                                    web.Response.Write("<td>" + "OK" + "</td>");
                                }
                                break;
                            case "总合格标志":
                                //工序状况=0 合格（OK）；=1 合格（NOK）
                                if (Convert.ToInt32(dataRow["总合格标志"]) == 0)
                                {
                                    web.Response.Write("<td>" + "OK" + "</td>");
                                }
                                else
                                {
                                    web.Response.Write("<td  style =\" background-color:Red\">" + "NOK" + "</td>");
                                }
                                break;
                            default:
                                web.Response.Write("<td>" + dataRow[i] + "</td>");
                                break;

                        }
                        break;
                }



            }
            web.Response.Write("</tr>");

        }
        web.Response.Write("</table>");

        web.Response.End();
    }
    /// <summary>
    /// 字段名称各个字符中间增加回车，以保证文本竖着书写。
    /// </summary>
    /// <param name="columnName"></param>
    /// <returns></returns>
    private static string GetColumnName_Ver(string columnName)
    {
        string columnName_tr;
        columnName_tr = "";
        for (int i = 0; i < columnName.Length; i++)
        {
            columnName_tr = columnName_tr + columnName[i] + "<br>";
        }
        return columnName_tr;
    }
    /// <summary>
    /// 基本报表
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_Query(DataTable table)
    {

        System.Web.HttpContext web = System.Web.HttpContext.Current;

        if (table == null) return;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\">" + table.TableName + "</td>");
        web.Response.Write("<tr>");
        //输出表字段
        for (int i = 0; i < table.Columns.Count; i++)
        {
            web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");
        }
        foreach (DataRow dataRow in table.Rows)
        {
            web.Response.Write("<tr>");

            for (int i = 0; i < table.Columns.Count; i++)
            {             
                //if (Convert.ToInt32(dataRow["合格标志"]) == 0)   --20140919 wt 
                if (Convert.ToString(dataRow["合格标志"]) == "不合格")
                {
                    web.Response.Write("<td  style =\" background-color:Red ;vnd.ms-excel.numberformat:@\">" + dataRow[i] + "</td>");
                }
                else
                {        
                    //去除科学计数法     
                    web.Response.Write("<td style='vnd.ms-excel.numberformat:@'>" + dataRow[i] + "</td>");
                }
            }
            web.Response.Write("</tr>");

        }
        web.Response.Write("</table>");

        web.Response.End();
    }
    /// <summary>
    /// 2012-07-19-chenghai基本报表_称重 （kg）
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable_QualityData_Query_kg(DataTable table)
    {

        System.Web.HttpContext web = System.Web.HttpContext.Current;

        if (table == null) return;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\">" + table.TableName + "</td>");
        web.Response.Write("<tr>");
        //输出表字段
        for (int i = 0; i < table.Columns.Count; i++)
        {
            if (i == 2 || i == 3 || i == 4 || i == 6 || i == 7 )
            {
                web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "(kg)" + "</td>");

            }
            else
            {
                if (i == 5)
                {
                    web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "(L)" + "</td>");
                }
                else 
                { 
                    web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");  
                }
             }
        }
        foreach (DataRow dataRow in table.Rows)
        {
            web.Response.Write("<tr>");

            for (int i = 0; i < table.Columns.Count; i++)
            {
                //if (Convert.ToInt32(dataRow["合格标志"]) == 0)   --20140919 wt 
                if (Convert.ToString(dataRow["合格标志"]) == "不合格")
                {
                    web.Response.Write("<td  style =\" background-color:Red\">" + dataRow[i] + "</td>");
                }
                else
                {
                    web.Response.Write("<td>" + dataRow[i] + "</td>");
                }
            }
            web.Response.Write("</tr>");

        }
        web.Response.Write("</table>");

        web.Response.End();
    }
    /// <summary>
    /// 基本报表
    /// </summary>
    /// <param name="table"></param>
    public static void ExcelTable(DataTable table)
    {

        System.Web.HttpContext web = System.Web.HttpContext.Current;

        if (table == null) return;
        web.Response.Clear();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";
        //表头
        web.Response.Write("<table border=1>");
        web.Response.Write("<tr>");
        web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\">" + table.TableName + "</td>");
        web.Response.Write("<tr>");
        //输出表字段
        for (int i = 0; i < table.Columns.Count; i++)
        {
            web.Response.Write("<td   align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");
        }
        foreach (DataRow dataRow in table.Rows)
        {
            web.Response.Write("<tr>");

            for (int i = 0; i < table.Columns.Count; i++)
            {
                web.Response.Write("<td>" + dataRow[i] + "</td>");
            }
            web.Response.Write("</tr>");

        }
        web.Response.Write("</table>");
  
        web.Response.End();
    }

    public static void ExcelTable_RecipeProof(int cutnum, int cutnum1, DataTable dt)
    {
        System.Web.HttpContext web = System.Web.HttpContext.Current;
        web.Response.ClearContent();
        web.Response.Charset = "GB2312";
        web.Response.Buffer = true;
        web.Response.ContentEncoding = System.Text.Encoding.UTF8;
        web.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
        HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
        web.Response.ContentType = "application/ms-excel";

        //SheetCount代表生成的 Sheet 数目。  
        for (int i = 0; i < 2; i++)
        {
            //计算该 Sheet 中的数据起始行和结束行。  
            int start = cutnum * i;
            int end = cutnum;
            if (i == 1) end = cutnum1 + cutnum;

            web.Response.Write(" < 配方校验:'Sheet" + (i + 1) + "'>");

            web.Response.Write("<table border=1>");
            web.Response.Write("<tr>");
            web.Response.Write("<td   align=\"center\" valign=\"middle\" colspan=\"" + dt.Columns.Count + "\">" + dt.TableName + "</td>");
            web.Response.Write("<tr>");
            //输出表字段
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                web.Response.Write("<td   align=\"center\" valign=\"middle\">" + dt.Columns[j].ColumnName + "</td>");
            }

            for (int j = start; j < end; j++)
            {
                web.Response.Write("<tr>");

                for (int r = 0; r < dt.Columns.Count; r++)
                {
                    web.Response.Write("<td>" + dt.Rows[j][r] + "</td>");
                }
                web.Response.Write("</tr>");

            }
            web.Response.Write("</table>");
            web.Response.Write("</Worksheet>");
            web.Response.Flush();
        }
        //web.Response.Write("</Workbook>");
        web.Response.End();
    }



}