Browse Source

特殊报表Excel开发

callm 5 years ago
parent
commit
d351f12a29
2 changed files with 479 additions and 0 deletions
  1. 470 0
      UAS_MES_ODLF/DataOperate/ExcelHandler.cs
  2. 9 0
      UAS_MES_ODLF/UAS_MES_ODLF.csproj

+ 470 - 0
UAS_MES_ODLF/DataOperate/ExcelHandler.cs

@@ -6,6 +6,9 @@ using NPOI.SS.UserModel;
 using NPOI.HSSF.Util;
 using NPOI.SS.Formula.Eval;
 using System.Text;
+using NPOI.SS.Util;
+using System.Drawing;
+using System.Collections.Generic;
 
 namespace UAS_MES_NEW.DataOperate
 {
@@ -32,6 +35,26 @@ namespace UAS_MES_NEW.DataOperate
             return filePath;
         }
 
+        /// <summary>
+        /// 导出Excel,返回文件在客户端的路径
+        /// </summary>
+        public string ExportExcel_BAIDU(DataTable dt, DateTime begindate, int DateNum, string FolderPath)
+        {
+            //创建一个内存流,用来接收转换成Excel的内容
+            MemoryStream ms;
+            ms = DataTableToExcel_BAIDU(dt, begindate, DateNum);
+            //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
+            string filePath = @FolderPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
+            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
+            byte[] data = ms.ToArray();
+            fs.Write(data, 0, data.Length);
+            fs.Flush();
+            //释放当前Excel文件,否则打开文件的时候会显示文件被占用
+            ms.Dispose();
+            fs.Dispose();
+            return filePath;
+        }
+
         /// <summary>
         /// 导入Excel
         /// </summary>
@@ -237,5 +260,452 @@ namespace UAS_MES_NEW.DataOperate
             book.Write(ms);
             return ms;
         }
+
+        /// <summary>
+        /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
+        /// </summary>
+        /// <param name="DataTable"></param>
+        /// <returns></returns>
+        public MemoryStream DataTableToExcel_BAIDU(DataTable DataTable, DateTime begindate, int DateNum)
+        {
+            string[] Step = new[] { "1-MT1", "2-MT2", "3-MMI", "4-RSA(耦合)", "5-AUD(曲线)", "6-THD(自播自录)", "7-SCK(验号)" };
+            string[] StepCode = new[] { "B_MT1", "B_MT2", "B_MMI", "B_RSA", "B_AUD", "B_THD", "B_SN", "B_OUTLOOK" };
+            string[] Kind = new[] { "测试数", "不良数", "误测数", "FPY", "RPY" };
+            string[] TotalKind = new[] { "总投入数", "总不良数", "FPY", "RPY" };
+            string[] OutLook = new[] { "测试数", "不良数", "FPY" };
+            MemoryStream ms = new MemoryStream();
+            //创建一个Book,相当于一个Excel文件
+            HSSFWorkbook book = new HSSFWorkbook();
+
+            ICellStyle NONE = book.CreateCellStyle();
+            NONE.VerticalAlignment = VerticalAlignment.CENTER;
+            NONE.Alignment = HorizontalAlignment.CENTER;
+            NONE.BorderBottom = BorderStyle.THIN;
+            NONE.BorderLeft = BorderStyle.THIN;
+            NONE.BorderRight = BorderStyle.THIN;
+            NONE.BorderTop = BorderStyle.THIN;
+
+            ICellStyle TAN = book.CreateCellStyle();
+            TAN.VerticalAlignment = VerticalAlignment.CENTER;
+            TAN.Alignment = HorizontalAlignment.CENTER;
+            TAN.FillForegroundColor = HSSFColor.TAN.index;
+            TAN.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            TAN.BorderBottom = BorderStyle.THIN;
+            TAN.BorderLeft = BorderStyle.THIN;
+            TAN.BorderRight = BorderStyle.THIN;
+            TAN.BorderTop = BorderStyle.THIN;
+
+            ICellStyle PALE_BLUE = book.CreateCellStyle();
+            PALE_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
+            PALE_BLUE.Alignment = HorizontalAlignment.CENTER;
+            PALE_BLUE.FillForegroundColor = HSSFColor.PALE_BLUE.index;
+            PALE_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            PALE_BLUE.BorderBottom = BorderStyle.THIN;
+            PALE_BLUE.BorderLeft = BorderStyle.THIN;
+            PALE_BLUE.BorderRight = BorderStyle.THIN;
+            PALE_BLUE.BorderTop = BorderStyle.THIN;
+
+            ICellStyle LIME = book.CreateCellStyle();
+            LIME.VerticalAlignment = VerticalAlignment.CENTER;
+            LIME.Alignment = HorizontalAlignment.CENTER;
+            LIME.FillForegroundColor = HSSFColor.LIME.index;
+            LIME.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            LIME.BorderBottom = BorderStyle.THIN;
+            LIME.BorderLeft = BorderStyle.THIN;
+            LIME.BorderRight = BorderStyle.THIN;
+            LIME.BorderTop = BorderStyle.THIN;
+
+            ICellStyle LEMON_CHIFFON = book.CreateCellStyle();
+            LEMON_CHIFFON.VerticalAlignment = VerticalAlignment.CENTER;
+            LEMON_CHIFFON.Alignment = HorizontalAlignment.CENTER;
+            LEMON_CHIFFON.FillForegroundColor = HSSFColor.LEMON_CHIFFON.index;
+            LEMON_CHIFFON.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            LEMON_CHIFFON.BorderBottom = BorderStyle.THIN;
+            LEMON_CHIFFON.BorderLeft = BorderStyle.THIN;
+            LEMON_CHIFFON.BorderRight = BorderStyle.THIN;
+            LEMON_CHIFFON.BorderTop = BorderStyle.THIN;
+
+            ICellStyle GOLD = book.CreateCellStyle();
+            GOLD.VerticalAlignment = VerticalAlignment.CENTER;
+            GOLD.Alignment = HorizontalAlignment.CENTER;
+            GOLD.FillForegroundColor = HSSFColor.GOLD.index;
+            GOLD.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            GOLD.BorderBottom = BorderStyle.THIN;
+            GOLD.BorderLeft = BorderStyle.THIN;
+            GOLD.BorderRight = BorderStyle.THIN;
+            GOLD.BorderTop = BorderStyle.THIN;
+
+            ICellStyle LIGHT_GREEN = book.CreateCellStyle();
+            LIGHT_GREEN.VerticalAlignment = VerticalAlignment.CENTER;
+            LIGHT_GREEN.Alignment = HorizontalAlignment.CENTER;
+            LIGHT_GREEN.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
+            LIGHT_GREEN.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            LIGHT_GREEN.BorderBottom = BorderStyle.THIN;
+            LIGHT_GREEN.BorderLeft = BorderStyle.THIN;
+            LIGHT_GREEN.BorderRight = BorderStyle.THIN;
+            LIGHT_GREEN.BorderTop = BorderStyle.THIN;
+            LIGHT_GREEN.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
+
+            ICellStyle LIGHT_CORNFLOWER_BLUE = book.CreateCellStyle();
+            LIGHT_CORNFLOWER_BLUE.VerticalAlignment = VerticalAlignment.CENTER;
+            LIGHT_CORNFLOWER_BLUE.Alignment = HorizontalAlignment.CENTER;
+            LIGHT_CORNFLOWER_BLUE.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;
+            LIGHT_CORNFLOWER_BLUE.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            LIGHT_CORNFLOWER_BLUE.BorderBottom = BorderStyle.THIN;
+            LIGHT_CORNFLOWER_BLUE.BorderLeft = BorderStyle.THIN;
+            LIGHT_CORNFLOWER_BLUE.BorderRight = BorderStyle.THIN;
+            LIGHT_CORNFLOWER_BLUE.BorderTop = BorderStyle.THIN;
+            LIGHT_CORNFLOWER_BLUE.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
+
+
+            ICellStyle GREY_25_PERCENT = book.CreateCellStyle();
+            GREY_25_PERCENT.VerticalAlignment = VerticalAlignment.CENTER;
+            GREY_25_PERCENT.Alignment = HorizontalAlignment.CENTER;
+            GREY_25_PERCENT.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
+            GREY_25_PERCENT.FillPattern = FillPatternType.SOLID_FOREGROUND;
+            GREY_25_PERCENT.BorderBottom = BorderStyle.THIN;
+            GREY_25_PERCENT.BorderLeft = BorderStyle.THIN;
+            GREY_25_PERCENT.BorderRight = BorderStyle.THIN;
+            GREY_25_PERCENT.BorderTop = BorderStyle.THIN;
+
+            //Excel中的Sheet
+            ISheet sheet = book.CreateSheet("sheet1");
+            IRow row = sheet.CreateRow(0);
+            ICell cell = row.CreateCell(0);
+            //画第一行的抬头
+            cell.SetCellValue("组装制程品质数据");
+            cell.CellStyle = NONE;
+            CellRangeAddress region = new CellRangeAddress(0, 0, 0, DateNum + 1);
+            sheet.AddMergedRegion(region);
+            //第一行的日期标题
+            row = sheet.CreateRow(1);
+            cell = row.CreateCell(0);
+            cell.CellStyle = NONE;
+            cell.SetCellValue("站点");
+            cell = row.CreateCell(1);
+            cell.SetCellValue("类别");
+            cell.CellStyle = NONE;
+            for (int i = 0; i < DateNum; i++)
+            {
+                cell = row.CreateCell(i + 2);
+                cell.SetCellValue(begindate.AddDays(i).ToString("MM/dd"));
+                cell.CellStyle = NONE;
+            }
+            //画第一列的工序名称和第二列的类别
+            //总良率数据
+            row = sheet.CreateRow(2);
+            cell = row.CreateCell(0);
+            cell.SetCellValue("总良率");
+            cell.CellStyle = LEMON_CHIFFON;
+            region = new CellRangeAddress(2, 5, 0, 0);
+            sheet.AddMergedRegion(region);
+            //总良率的统计数据
+            for (int i = 0; i < TotalKind.Length; i++)
+            {
+                row = sheet.GetRow(i + 2);
+                if (row == null)
+                {
+                    row = sheet.CreateRow(i + 2);
+                }
+                cell = row.CreateCell(1);
+                cell.SetCellValue(TotalKind[i]);
+                cell.CellStyle = LEMON_CHIFFON;
+                switch (i)
+                {
+                    case 0:
+                        cell.CellStyle = LIME;
+                        break;
+                    case 1:
+                        cell.CellStyle = TAN;
+                        break;
+                    case 2:
+                        cell.CellStyle = LIGHT_GREEN;
+                        break;
+                    case 3:
+                        cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
+                        break;
+                    default:
+                        break;
+                }
+            }
+
+            //中间的设备测试工序
+            for (int i = 0; i < Step.Length; i++)
+            {
+                //除去前面6行
+                int rowindex = 6 + i * 5;
+                row = sheet.CreateRow(rowindex);
+                cell = row.CreateCell(0);
+                cell.SetCellValue(Step[i]);
+                cell.CellStyle = PALE_BLUE;
+                region = new CellRangeAddress(rowindex, rowindex + 4, 0, 0);
+                sheet.AddMergedRegion(region);
+                for (int j = rowindex; j < rowindex + 5; j++)
+                {
+                    row = sheet.GetRow(j);
+                    if (row == null)
+                    {
+                        row = sheet.CreateRow(j);
+                    }
+                    cell = row.CreateCell(1);
+                    cell.SetCellValue(Kind[j - rowindex]);
+                    switch (j - rowindex)
+                    {
+                        case 0:
+                            cell.CellStyle = GREY_25_PERCENT;
+                            break;
+                        case 1:
+                            cell.CellStyle = TAN;
+                            break;
+                        case 2:
+                            cell.CellStyle = GOLD;
+                            break;
+                        case 3:
+                            cell.CellStyle = LIGHT_GREEN;
+                            break;
+                        case 4:
+                            cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
+                            break;
+                        default:
+                            break;
+                    }
+                }
+            }
+            //最后一行外观数据
+            row = sheet.CreateRow(41);
+            cell = row.CreateCell(0);
+            cell.SetCellValue("8-外观");
+            cell.CellStyle = PALE_BLUE;
+            region = new CellRangeAddress(41, 43, 0, 0);
+            sheet.AddMergedRegion(region);
+
+            //外观的统计数据
+            for (int i = 0; i < OutLook.Length; i++)
+            {
+                row = sheet.GetRow(41 + i);
+                if (row == null)
+                {
+                    row = sheet.CreateRow(41 + i);
+                }
+                cell = row.CreateCell(1);
+                cell.SetCellValue(OutLook[i]);
+                cell.CellStyle = PALE_BLUE;
+                switch (i)
+                {
+                    case 0:
+                        cell.CellStyle = GREY_25_PERCENT;
+                        break;
+                    case 1:
+                        cell.CellStyle = TAN;
+                        break;
+                    case 2:
+                        cell.CellStyle = LIGHT_GREEN;
+                        break;
+                    default:
+                        break;
+                }
+            }
+            sheet.SetColumnWidth(0, 3700);
+
+            for (int i = 0; i < DateNum; i++)
+            {
+                double TotalFPY = 0;
+                double TotalRPY = 0;
+                double TotalNG = 0;
+                double TotalIN = 0;
+                for (int j = 0; j < StepCode.Length; j++)
+                {
+                    int rowindex = 6 + j * 5;
+                    DataTable dt = PublicMethod.BaseUtil.filterDataTable(DataTable, "sp_date='" + begindate.AddDays(i).ToString("yyyy-MM-dd") + "' and 工序编号='" + StepCode[j] + "'");
+                    if (StepCode[j] != "B_OUTLOOK")
+                    {
+                        for (int k = rowindex; k < rowindex + 5; k++)
+                        {
+                            row = sheet.GetRow(k);
+                            if (row == null)
+                            {
+                                row = sheet.CreateRow(k);
+                            }
+                            cell = row.CreateCell(i + 2);
+                            switch (k - rowindex)
+                            {
+                                case 0:
+                                    double 测试数;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
+                                        {
+                                            cell.SetCellValue(测试数);
+                                            if (StepCode[j] == "B_MT1")
+                                            {
+                                                TotalIN = 测试数;
+                                            }
+                                        }
+                                    }
+                                    cell.CellStyle = GREY_25_PERCENT;
+                                    break;
+                                case 1:
+                                    double 不良数;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
+                                        {
+                                            cell.SetCellValue(不良数);
+                                            TotalNG = TotalNG + 不良数;
+                                        }
+                                    }
+                                    cell.CellStyle = TAN;
+                                    break;
+                                case 2:
+                                    double 误测数;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["误测数"].ToString(), out 误测数))
+                                        {
+                                            cell.SetCellValue(误测数);
+                                        }
+                                    }
+                                    cell.CellStyle = GOLD;
+                                    break;
+                                case 3:
+                                    double FPY;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
+                                        {
+                                            cell.SetCellValue(FPY);
+                                            //累计所有FPY
+                                            if (TotalFPY == 0)
+                                            {
+                                                TotalFPY = FPY;
+                                            }
+                                            else
+                                            {
+                                                TotalFPY = TotalFPY * FPY;
+                                            }
+                                        }
+                                    }
+                                    cell.CellStyle = LIGHT_GREEN;
+                                    break;
+                                case 4:
+                                    double RPY;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["RPY"].ToString(), out RPY))
+                                        {
+                                            cell.SetCellValue(RPY);
+                                            //累计所有RPY
+                                            if (TotalRPY == 0)
+                                            {
+                                                TotalRPY = RPY;
+                                            }
+                                            else
+                                            {
+                                                TotalRPY = TotalRPY * RPY;
+                                            }
+                                        }
+                                    }
+                                    cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
+                                    break;
+                                default:
+                                    break;
+                            }
+                        }
+                    }
+                    else
+                    {
+                        for (int k = rowindex; k < rowindex + 3; k++)
+                        {
+                            row = sheet.GetRow(k);
+                            if (row == null)
+                            {
+                                row = sheet.CreateRow(k);
+                            }
+                            cell = row.CreateCell(i + 2);
+                            switch (k - rowindex)
+                            {
+                                case 0:
+                                    double 测试数;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["测试数"].ToString(), out 测试数))
+                                        {
+                                            cell.SetCellValue(测试数);
+                                            if (StepCode[j] == "B_MT1")
+                                            {
+                                                TotalIN = 测试数;
+                                            }
+                                        }
+                                    }
+                                    cell.CellStyle = GREY_25_PERCENT;
+                                    break;
+                                case 1:
+                                    double 不良数;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["不良数"].ToString(), out 不良数))
+                                        {
+                                            cell.SetCellValue(不良数);
+                                            TotalNG = TotalNG + 不良数;
+                                        }
+                                    }
+                                    cell.CellStyle = TAN;
+                                    break;
+                                case 2:
+                                    double FPY;
+                                    if (dt.Rows.Count > 0)
+                                    {
+                                        if (double.TryParse(dt.Rows[0]["FPY"].ToString(), out FPY))
+                                        {
+                                            cell.SetCellValue(FPY);
+                                            //累计所有FPY
+                                            if (TotalFPY == 0)
+                                            {
+                                                TotalFPY = FPY;
+                                            }
+                                            else
+                                            {
+                                                TotalFPY = TotalFPY * FPY;
+                                            }
+                                        }
+                                    }
+                                    cell.CellStyle = LIGHT_GREEN;
+                                    break;
+                                default:
+                                    break;
+                            }
+                        }
+                    }
+                }
+
+                //设置最上方的总计数量
+                row = sheet.GetRow(2);
+                cell = row.CreateCell(i + 2);
+                cell.CellStyle = LIME;
+                cell.SetCellValue(TotalIN);
+
+                row = sheet.GetRow(3);
+                cell = row.CreateCell(i + 2);
+                cell.CellStyle = TAN;
+                cell.SetCellValue(TotalNG);
+
+                row = sheet.GetRow(4);
+                cell = row.CreateCell(i + 2);
+                cell.CellStyle = LIGHT_GREEN;
+                cell.SetCellValue(TotalFPY);
+
+                row = sheet.GetRow(5);
+                cell = row.CreateCell(i + 2);
+                cell.CellStyle = LIGHT_CORNFLOWER_BLUE;
+                cell.SetCellValue(TotalRPY);
+            }
+            for (int i = 0; i < sheet.PhysicalNumberOfRows; i++)
+            {
+                sheet.GetRow(i).Height = 300;
+            }
+            //将book的内容写入内存流中返回
+            book.Write(ms);
+            return ms;
+        }
     }
 }

+ 9 - 0
UAS_MES_ODLF/UAS_MES_ODLF.csproj

@@ -850,6 +850,12 @@
     <Compile Include="FunctionCode\Query\Query_ProductTraceBack.Designer.cs">
       <DependentUpon>Query_ProductTraceBack.cs</DependentUpon>
     </Compile>
+    <Compile Include="FunctionCode\Query\Query_DateRate.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="FunctionCode\Query\Query_DateRate.Designer.cs">
+      <DependentUpon>Query_DateRate.cs</DependentUpon>
+    </Compile>
     <Compile Include="FunctionCode\Query\Query_SpecialReport.cs">
       <SubType>Form</SubType>
     </Compile>
@@ -1399,6 +1405,9 @@
     <EmbeddedResource Include="FunctionCode\Query\Query_ProductTraceBack.resx">
       <DependentUpon>Query_ProductTraceBack.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="FunctionCode\Query\Query_DateRate.resx">
+      <DependentUpon>Query_DateRate.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="FunctionCode\Query\Query_SpecialReport.resx">
       <DependentUpon>Query_SpecialReport.cs</DependentUpon>
     </EmbeddedResource>