Ejemplo n.º 1
0
        /// <summary>
        /// 测试Hot Patch破解方式是否成功
        /// 检验 X 次, 查看是否含有未注册的Worksheet出现
        /// </summary>
        /// <returns></returns>
        public static string TestAsposeCellsHotPatch(string pathTemplate = null, int totalTestCount = 30)
        {
            if (pathTemplate.IsNullOrEmpty())
            {
                pathTemplate = @"C:\Test\TestAspose{0}.xlsx";
            }
            string path       = pathTemplate.FormatWith(0);
            string exportPath = string.Empty;

            List <string> headList = new List <string>();

            headList.Add("HLD");
            headList.Add("HSN");
            headList.Add("HSW");

            Random random = new Random();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < totalTestCount; i++)
            {
                if (i % 100 == 0)
                {
                    GC.Collect();
                }

                Aspose.Cells.Workbook wb = null;

                if (System.IO.File.Exists(path) == false)
                {
                    wb = new Aspose.Cells.Workbook();
                }
                else
                {
                    wb = new Aspose.Cells.Workbook(path);
                }

                if (wb != null)
                {
                    bool isLicensed = wb.IsLicensed;
                    if (isLicensed == false)
                    {
                        sb.AppendLine("Run Time {0} : isLicensed = false;".FormatWith(i));
                    }

                    if (wb.Worksheets.Count > 1)
                    {
                        sb.AppendLine("Run Time {0} : Worksheets Count = {1};".FormatWith(i, wb.Worksheets.Count));
                    }

                    var ws    = wb.Worksheets[0];
                    var cell0 = ws.Cells[i, 0];
                    cell0.Value = i;

                    var cell1 = ws.Cells[i, 1];
                    cell1.Value = "{0}ABC".FormatWith(i);

                    var cell2 = ws.Cells[i, 2];

                    int randomIndex = random.Next(3);
                    cell2.Value = "{0}{1}".FormatWith(headList[randomIndex], random.Next(1000000).ToString().PadLeft(9, '0'));

                    var    cell3   = ws.Cells[i, 3];
                    string formula = "=CHOOSE(MATCH(MID(C" + (i + 1).ToString() + ",1,3),{\"HLD\",\"HSN\",\"HSW\"}),\"123HLD\",\"123HSN\",\"123HSW\")";
                    cell3.SetFormula(formula, headList[randomIndex]);

                    var cell4 = ws.Cells[i, 4];
                    cell4.SetFormula(formula, headList[randomIndex]);

                    exportPath = pathTemplate.FormatWith(i + 1);
                    wb.Save(exportPath);
                    wb.Dispose();

                    using (Aspose.Cells.Workbook checkFormula = new Aspose.Cells.Workbook(exportPath))
                    {
                        var checkCell3 = checkFormula.Worksheets[0].Cells[i, 3];
                        var checkCell4 = checkFormula.Worksheets[0].Cells[i, 4];

                        if (checkCell4.Value == null)
                        {
                            sb.AppendLine("Run Time {0} : cell4 value is null, Expect [{1}]".FormatWith(i, headList[randomIndex]));
                        }
                        else if (checkCell4.Value.ToString().EndsWith(headList[randomIndex]) == false)
                        {
                            sb.AppendLine("Run Time {0} : cell4 value [{1}], Expect [{2}]".FormatWith(i, checkCell4.StringValue, headList[randomIndex]));
                        }

                        checkFormula.Dispose();
                    }

                    path = exportPath;
                }
            }

            string errorMsg = sb.ToString();

            return(errorMsg);
        }