Ejemplo n.º 1
0
        void PlanListWindow_Closed(object sender, EventArgs e)
        {
            PlanListEvaluateWindow planListWindow = sender as PlanListEvaluateWindow;

            if (planListWindow.DialogResult == true)
            {
                SaveFileDialog sDialog = new SaveFileDialog();
                sDialog.Filter = "Excel Files(*.xls)|*.xls";

                if (sDialog.ShowDialog() == true)
                {
                    try
                    {
                        string versionId = "文件编号:";
                        if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.FileId)
                        {
                            versionId += planListWindow.planExtraEntity.FileId;
                        }
                        versionId += " 计划版本:";
                        versionId += SelectProjectEntity.PlanVersionID;
                        string projectNameKey    = SelectProjectEntity.ProjectName + "        ";
                        string manufactureNumber = "生产令号:" + SelectProjectEntity.ManufactureNumber;

                        Workbook    workbook    = new Workbook();
                        ColumnModel columnModel = new ColumnModel();
                        foreach (PlanListViewModel item in planListWindow.planListViewModelList)
                        {
                            string projectNameName = projectNameKey + item.Title;

                            Worksheet worksheet = new Worksheet(item.Title);

                            Int16 RowCount = 0;
                            worksheet.Cells[RowCount++, 0] = new Cell(versionId);
                            worksheet.Cells[RowCount++, 0] = new Cell(projectNameName);
                            worksheet.Cells[RowCount++, 0] = new Cell(manufactureNumber);

                            int columnCount = 0;
                            foreach (string itemColumn in columnModel.List[item.ColumnModelIndex])
                            {
                                worksheet.Cells[RowCount, columnCount++] = new Cell(itemColumn);
                            }
                            ++RowCount;

                            foreach (PlanEntity planEntity in item.FilterPlanList)
                            {
                                columnCount = 0;
                                string value = Convert.ToString(planEntity.SequenceId);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.ComponentName);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.TaskDescription);

                                value = Convert.ToString(planEntity.Weight);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                if (planEntity.Score.HasValue)
                                {
                                    value = Convert.ToString(planEntity.Score.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (1 == item.ColumnModelIndex)
                                {
                                    if (planEntity.OrderDate.HasValue)
                                    {
                                        value = Convert.ToString(planEntity.OrderDate.Value);
                                        worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                    }
                                    ++columnCount;
                                }

                                value = Convert.ToString(planEntity.TargetDate);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                if (planEntity.TargetDateAdjustment1.HasValue)
                                {
                                    value = Convert.ToString(planEntity.TargetDateAdjustment1.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.TargetDateAdjustment2.HasValue)
                                {
                                    value = Convert.ToString(planEntity.TargetDateAdjustment2.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.AccomplishDate.HasValue)
                                {
                                    value = Convert.ToString(planEntity.AccomplishDate.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (null != planEntity.DepartmentName && string.Empty != planEntity.DepartmentName)
                                {
                                    worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.DepartmentName);
                                }
                                ++columnCount;

                                if (null != planEntity.Remark && string.Empty != planEntity.Remark)
                                {
                                    worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.Remark);
                                }
                                ++columnCount;

                                ++RowCount;
                            }

                            workbook.Worksheets.Add(worksheet);
                        }
                        Stream sFile = sDialog.OpenFile();
                        workbook.Save(sFile);

                        Message.InfoMessage("导出成功");
                    }
                    catch (Exception outputE)
                    {
                        string errorMessage = "导出文件失败:" + outputE.Message;
                        Message.ErrorMessage(errorMessage);
                    }
                }
            }
            (OnEvaluateSingle as DelegateCommand).RaiseCanExecuteChanged();
        }
        void PlanListWindow_Closed(object sender, EventArgs e)
        {
            PlanListEditWindow planListWindow = sender as PlanListEditWindow;
            if (planListWindow.DialogResult == true)
            {
                SaveFileDialog sDialog = new SaveFileDialog();
                sDialog.Filter = "Excel Files(*.xls)|*.xls";

                if (sDialog.ShowDialog() == true)
                {
                    try
                    {
                        string versionId = "文件编号:";
                        if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.FileId)
                        {
                            versionId += planListWindow.planExtraEntity.FileId;
                        }
                        versionId += " 计划版本:";
                        versionId += SelectProjectEntity.PlanVersionID;
                        string projectNameKey = SelectProjectEntity.ProjectName + "        ";
                        string manufactureNumber = "生产令号:" + SelectProjectEntity.ManufactureNumber;

                        Workbook workbook = new Workbook();
                        ColumnModel columnModel = new ColumnModel();
                        foreach (PlanListViewModel item in planListWindow.planListViewModelList)
                        {
                            string projectNameName = projectNameKey + item.Title;

                            Worksheet worksheet = new Worksheet(item.Title);

                            Int16 RowCount = 0;
                            worksheet.Cells[RowCount++, 0] = new Cell(versionId);
                            worksheet.Cells[RowCount++, 0] = new Cell(projectNameName);
                            worksheet.Cells[RowCount++, 0] = new Cell(manufactureNumber);

                            int columnCount = 0;
                            foreach (string itemColumn in columnModel.List[item.ColumnModelIndex])
                            {
                                worksheet.Cells[RowCount, columnCount++] = new Cell(itemColumn);
                            }
                            ++RowCount;

                            foreach (PlanEntity planEntity in item.PlanList)
                            {
                                columnCount = 0;
                                string value = Convert.ToString(planEntity.SequenceId);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.ComponentName);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.TaskDescription);

                                value = Convert.ToString(planEntity.Weight);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                if (planEntity.Score.HasValue)
                                {
                                    value = Convert.ToString(planEntity.Score.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (1 == item.ColumnModelIndex)
                                {
                                    if (planEntity.OrderDate.HasValue)
                                    {
                                        value = Convert.ToString(planEntity.OrderDate.Value);
                                        worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                    }
                                    ++columnCount;
                                }

                                value = Convert.ToString(planEntity.TargetDate);
                                worksheet.Cells[RowCount, columnCount++] = new Cell(value);

                                if (planEntity.TargetDateAdjustment1.HasValue)
                                {
                                    value = Convert.ToString(planEntity.TargetDateAdjustment1.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.TargetDateAdjustment2.HasValue)
                                {
                                    value = Convert.ToString(planEntity.TargetDateAdjustment2.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.AccomplishDate.HasValue)
                                {
                                    value = Convert.ToString(planEntity.AccomplishDate.Value);
                                    worksheet.Cells[RowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (null != planEntity.DepartmentName && string.Empty != planEntity.DepartmentName)
                                {
                                    worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.DepartmentName);
                                }
                                ++columnCount;

                                if (null != planEntity.Remark && string.Empty != planEntity.Remark)
                                {
                                    worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.Remark);
                                }
                                ++columnCount;

                                ++RowCount;
                            }

                            workbook.Worksheets.Add(worksheet);
                        }
                        Stream sFile = sDialog.OpenFile();
                        workbook.Save(sFile);

                        Message.InfoMessage("导出成功");
                    }
                    catch (Exception outputE)
                    {
                        string errorMessage = "导出文件失败:" + outputE.Message;
                        Message.ErrorMessage(errorMessage);
                    }
                }
            }
            (OnExportPlan as DelegateCommand).RaiseCanExecuteChanged();
        }
        void PlanListWindow_Closed(object sender, EventArgs e)
        {
            PlanListTraceWindow planListWindow = sender as PlanListTraceWindow;
            if (planListWindow.DialogResult == true)
            {
                SaveFileDialog sDialog = new SaveFileDialog();
                sDialog.Filter = "Excel Files(*.xls)|*.xls";

                if (sDialog.ShowDialog() == true)
                {
                    try
                    {
                        string versionId = "文件编号:";
                        if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.FileId)
                        {
                            versionId += planListWindow.planExtraEntity.FileId;
                        }
                        versionId += " 计划版本:";
                        if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.VersionId)
                        {
                            versionId += planListWindow.planExtraEntity.VersionId;
                        }

                        string projectNameKey = null == planListWindow.planExtraEntity ? "所有项目" :
                                                           null != SelectProjectEntity ? SelectProjectEntity.ProjectName : planListWindow.planListViewModelList[0].PlanList[0].ProjectName;
                        projectNameKey += "        ";
                        string manufactureNumber = "生产令号:";
                        if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.ManufactureNumber)
                        {
                            manufactureNumber += planListWindow.planExtraEntity.ManufactureNumber;
                        }

                        ColumnModel columnModel = new ColumnModel();
                        Dictionary<string, Worksheet> sheetDictionary = new Dictionary<string, Worksheet>();
                        Dictionary<string, int> rowCountDictionary = new Dictionary<string, int>();
                        Worksheet currentSheet = null;
                        foreach(PlanListViewModel model in planListWindow.planListViewModelList)
                        {
                            foreach (PlanEntity planEntity in model.FilterPlanList)
                            {
                                if ("设计完成节点" == planEntity.SheetName)
                                {
                                    planEntity.SheetName = "设计节点";
                                }
                                else if ("采购完成节点" == planEntity.SheetName)
                                {
                                    planEntity.SheetName = "采购节点";
                                }
                                else if ("生产完成节点" == planEntity.SheetName)
                                {
                                    planEntity.SheetName = "生产节点";
                                }
                                if (!sheetDictionary.Keys.Contains(planEntity.SheetName))
                                {
                                    currentSheet = new Worksheet(planEntity.SheetName);
                                    string projectNameName = projectNameKey + planEntity.SheetName;

                                    Int16 rowHeaderCount = 0;
                                    currentSheet.Cells[rowHeaderCount++, 0] = new Cell(versionId);
                                    currentSheet.Cells[rowHeaderCount++, 0] = new Cell(projectNameName);
                                    currentSheet.Cells[rowHeaderCount++, 0] = new Cell(manufactureNumber);

                                    int columnHeaderCount = 0;
                                    if (null == planListWindow.planExtraEntity)
                                    {
                                        currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("项目名称");
                                        currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("生产令号");
                                        currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("版本号");
                                    }

                                    int columnModelIndex = model.ColumnModelIndex;
                                    foreach (string itemColumn in columnModel.List[columnModelIndex])
                                    {
                                        currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell(itemColumn);
                                    }
                                    ++rowHeaderCount;

                                    sheetDictionary.Add(planEntity.SheetName, currentSheet);
                                    rowCountDictionary.Add(planEntity.SheetName, rowHeaderCount);
                                }
                                else
                                {
                                    currentSheet = sheetDictionary[planEntity.SheetName];
                                }

                                int rowCount = rowCountDictionary[planEntity.SheetName];
                                int columnCount = 0;

                                if (null == planListWindow.planExtraEntity)
                                {
                                    currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ProjectName);
                                    currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ManufactureNumber);
                                    currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.VersionId);
                                }

                                string value = Convert.ToString(planEntity.SequenceId);
                                currentSheet.Cells[rowCount, columnCount++] = new Cell(value);

                                currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ComponentName);
                                currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.TaskDescription);

                                value = Convert.ToString(planEntity.Weight);
                                currentSheet.Cells[rowCount, columnCount++] = new Cell(value);

                                if (planEntity.Score.HasValue)
                                {
                                    value = Convert.ToString(planEntity.Score.Value);
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                int currentColumnModelIndex = model.ColumnModelIndex;
                                if (1 == currentColumnModelIndex)
                                {
                                    if (planEntity.OrderDate.HasValue)
                                    {
                                        value = planEntity.OrderDate.Value.ToString(String.Format("yyyy-MM-dd"));
                                        currentSheet.Cells[rowCount, columnCount] = new Cell(value);
                                    }
                                    ++columnCount;
                                }

                                value = planEntity.TargetDate.ToString(String.Format("yyyy-MM-dd"));
                                currentSheet.Cells[rowCount, columnCount++] = new Cell(value);

                                if (planEntity.TargetDateAdjustment1.HasValue)
                                {
                                    value = planEntity.TargetDateAdjustment1.Value.ToString(String.Format("yyyy-MM-dd"));
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.TargetDateAdjustment2.HasValue)
                                {
                                    value = planEntity.TargetDateAdjustment2.Value.ToString(String.Format("yyyy-MM-dd"));
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (planEntity.AccomplishDate.HasValue)
                                {
                                    value = planEntity.AccomplishDate.Value.ToString(String.Format("yyyy-MM-dd"));
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(value);
                                }
                                ++columnCount;

                                if (null != planEntity.DepartmentName && string.Empty != planEntity.DepartmentName)
                                {
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(planEntity.DepartmentName);
                                }
                                ++columnCount;

                                if (null != planEntity.Remark && string.Empty != planEntity.Remark)
                                {
                                    currentSheet.Cells[rowCount, columnCount] = new Cell(planEntity.Remark);
                                }
                                ++columnCount;

                                ++rowCountDictionary[planEntity.SheetName];
                            }
                        }

                        if(sheetDictionary.Count > 0)
                        {
                            Workbook workbook = new Workbook();
                            foreach (KeyValuePair<string, Worksheet> kv in sheetDictionary)
                            {
                                if (null != planListWindow.planExtraEntity)
                                {
                                    PlanExtraEntity planExtraEntity = planListWindow.planExtraEntity;
                                    currentSheet = kv.Value;
                                    currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("编制依据:" + planExtraEntity.CompilationBasis);
                                    currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("第一次调整原因:" + planExtraEntity.ReasonAdjustment1);
                                    currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("第二次调整原因:" + planExtraEntity.ReasonAdjustment2);

                                    currentSheet.Cells[rowCountDictionary[kv.Key], 0] = new Cell("编制:" + planExtraEntity.CompileUserName);
                                    currentSheet.Cells[rowCountDictionary[kv.Key], 3] = new Cell("审核:" + planExtraEntity.ExamineUserName);
                                    currentSheet.Cells[rowCountDictionary[kv.Key]++, 5] = new Cell("批准:" + planExtraEntity.ApproveUserName);

                                    currentSheet.Cells[rowCountDictionary[kv.Key], 0] = new Cell("日期:" + planExtraEntity.CompileDate);
                                    currentSheet.Cells[rowCountDictionary[kv.Key], 3] = new Cell("日期:" + planExtraEntity.ExamineDate);
                                    currentSheet.Cells[rowCountDictionary[kv.Key]++, 5] = new Cell("日期:" + planExtraEntity.ApproveDate);
                                }

                                workbook.Worksheets.Add(kv.Value);
                            }

                            Stream sFile = sDialog.OpenFile();
                            workbook.Save(sFile);
                            Message.InfoMessage("导出成功");
                        }
                        else
                        {
                            Message.ErrorMessage("导出失败:无数据!");
                        }
                    }
                    catch (Exception outputE)
                    {
                        string errorMessage = "导出文件失败:" + outputE.Message;
                        Message.ErrorMessage(errorMessage);
                    }
                }
            }
            if (planListWindow.DialogResult.HasValue)
            {
                ObservableCollection<string> unFinishedProject = new ObservableCollection<string>();
                foreach (PlanListViewModel planListViewModel in planListWindow.planListViewModelList)
                {
                    foreach (PlanEntity planEntity in planListViewModel.PlanList)
                    {
                        if (null == planEntity.AccomplishDate && !unFinishedProject.Contains(planEntity.ManufactureNumber))
                        {
                            unFinishedProject.Add(planEntity.ManufactureNumber);
                        }
                    }
                }

                if (null != planListWindow.planExtraEntity)
                {
                    if(null != SelectProjectEntity)
                    {
                        SelectProjectEntity.AccomplishMark = (0 == unFinishedProject.Count ? 1 : 0);
                        SelectProjectEntity.DUpdate();
                    }
                }
                else
                {
                    foreach (ProjectEntity projectItem in ProjectList)
                    {
                        if (!string.IsNullOrEmpty(projectItem.PlanVersionID))
                        {
                            projectItem.AccomplishMark = unFinishedProject.Contains(projectItem.ManufactureNumber) ? 0 : 1;
                            projectItem.DUpdate();
                        }
                    }
                }
                SaveChanges();
            }
            (OnViewSinglePlan as DelegateCommand).RaiseCanExecuteChanged();
            (OnSetRemind as DelegateCommand).RaiseCanExecuteChanged();
            (OnViewAllPlan as DelegateCommand).RaiseCanExecuteChanged();
            (OnViewSingleHistory as DelegateCommand).RaiseCanExecuteChanged();
        }
        private int ExcelSheetValidation(Worksheet aSheet, 
                                        ref int aFirstValueRow, 
                                        ref string aManufactureName, 
                                        ref string aVersionId,
                                        ref string aFileId,
                                        ref string aRequirement, 
                                        ref Dictionary<string, int> aMatchedColumnDictionary)
        {
            aFirstValueRow = -1;
            ColumnModel columnModel = new ColumnModel();
            foreach (KeyValuePair<int, Row> rowPair in aSheet.Cells.Rows)
            {
                try
                {
                    if (rowPair.Value.LastColIndex < columnModel.MinSize - 1)
                    {
                        Cell cell = rowPair.Value.GetCell(rowPair.Value.FirstColIndex);
                        if (Cell.EmptyCell != cell)
                        {
                            string firstValue = cell.StringValue;
                            string manufactureNumberKey = "生产令号:";
                            string versionIdKey1 = "计划版本:";
                            string versionIdKey2 = "计划版本:";
                            string versionIdKey = string.Empty;
                            int pos = -1;
                            if (-1 != (pos = firstValue.IndexOf(versionIdKey1)))
                            {
                                versionIdKey = versionIdKey1;
                            }
                            else if( -1 != (pos = firstValue.IndexOf(versionIdKey2)))
                            {
                                versionIdKey = versionIdKey2;
                            }

                            if (versionIdKey.Count() > 0)
                            {
                                int versionPos = pos + versionIdKey.Length;
                                aVersionId = firstValue.Substring(versionPos);
                                aVersionId = aVersionId.Trim();
                                string fileIdKey = "文件编号:";
                                int fileIdStartPos = firstValue.IndexOf(fileIdKey);
                                if (-1 != fileIdStartPos)
                                {
                                    fileIdStartPos += fileIdKey.Length;
                                    aFileId = firstValue.Substring(fileIdStartPos, versionPos - fileIdStartPos - versionIdKey.Count());
                                    aFileId = aFileId.Trim();
                                }
                            }
                            else if (-1 != (pos = firstValue.IndexOf(manufactureNumberKey)) && pos < firstValue.Length)
                            {
                                pos += manufactureNumberKey.Length;
                                const char space = ' ';
                                while (space == firstValue[pos])
                                {
                                    ++pos;
                                }

                                int end = firstValue.IndexOf(space, pos);
                                aManufactureName = firstValue.Substring(pos, end - pos);
                                aManufactureName = aManufactureName.Trim();
                                aRequirement = firstValue.Substring(end);
                                aRequirement = aRequirement.Trim();
                            }
                        }
                        continue;
                    }
                }
                catch(Exception e)
                {
                    //empty line
                    continue;
                }
                foreach (ObservableCollection<string> item in columnModel.List)
                {
                    int matchCount = 0;
                    aMatchedColumnDictionary.Clear();
                    for (int column = rowPair.Value.FirstColIndex; column <= rowPair.Value.LastColIndex; ++column)
                    {
                        Cell cell = rowPair.Value.GetCell(column);
                        if (Cell.EmptyCell != cell && item.Contains(cell.StringValue))
                        {
                            aMatchedColumnDictionary.Add(cell.StringValue, column);
                            ++matchCount;
                        }
                    }
                    if (item.Count == matchCount)
                    {
                        aFirstValueRow = rowPair.Key + 1;
                        return columnModel.List.IndexOf(item);
                    }
                }
            }

            return -1;
        }