Example #1
0
        public void Process(int startindex, float ratio)
        {
            StartIndex = startindex;
            Ratio      = ratio;
            Log("查找文件...");
            List <string> files = new List <string>();

            GetAllFiles(Url, ref files);
            if (files.Count == 0)
            {
                Log("没有找到文件,结束");
                return;
            }

            Log("查找小样文件...");

            List <FileInfo> fileinfos  = new List <FileInfo>();
            int             totalindex = StartIndex;

            foreach (var f in files)
            {
                FileInfo fi;
                if (ParseFileName(Path.GetFileNameWithoutExtension(f), out fi))
                {
                    fi.Url    = f;
                    fi.Number = totalindex.ToString() + "号";
                    fileinfos.Add(fi);
                    totalindex++;
                }
            }

            if (fileinfos.Count == 0)
            {
                Log("没有找到米样文件,结束");
                return;
            }

            Log("找到" + fileinfos.Count.ToString() + "个小样文件,开始重命名...");

            foreach (var item in fileinfos)
            {
                string newname = item.Number + "-" + item.ReguName + Path.GetExtension(item.Url);
                Rename(item.Url, newname);
            }

            Log("重命名结束...");

            Log("导出Excel文件...");
            {
                ExcelEdit ed = new ExcelEdit();
                ed.Create();
                int index = 1;

                ed.SetCellValue("Sheet1", index, 1, "图号");
                ed.SetCellValue("Sheet1", index, 2, "描述");
                index++;
                foreach (var item in fileinfos)
                {
                    ed.SetCellValue("Sheet1", index, 1, item.Number);

                    string disc = item.Format + item.Disc + "-";
                    foreach (var fn in item.FaceNameCount)
                    {
                        disc += fn.Item1.Item1 + "(" + fn.Item2.ToString() + ")个、";
                    }
                    disc.TrimEnd('、');

                    ed.SetCellValue("Sheet1", index, 2, disc);

                    index++;
                }
                string excelname = Url.Substring(Url.LastIndexOf('\\') + 1);
                ExcelUrl1 = Url + "\\小样" + excelname + "1.xlsx";
                ed.SaveAs(ExcelUrl1);
                ed.Close();
            }

            {
                int nCount = Config.Settings.lstFaceInfo.Count;
                List <Tuple <string, string, List <int> > > sortfacenamecount = new List <Tuple <string, string, List <int> > >();
                for (int i = 0; i < nCount; i++)
                {
                    sortfacenamecount.Add(new Tuple <string, string, List <int> >(Config.Settings.lstFaceInfo[i].Index.ToString() + "#", Config.Settings.lstFaceInfo[i].FaceName, new List <int>()));
                }

                foreach (var item in fileinfos)
                {
                    foreach (var fn in item.FaceNameCount)
                    {
                        var res = sortfacenamecount.Where(s => s.Item1 == fn.Item1.Item2);
                        if (res.Count() != 0)
                        {
                            res.ElementAt(0).Item3.Add(fn.Item2);
                        }
                        else
                        {
                            Log("文件" + item.Url + "的面料类型" + fn.Item1 + "没有定义");
                        }
                    }
                }

                ExcelEdit ed = new ExcelEdit();
                ed.Create();
                int index = 1;

                ed.SetCellValue("Sheet1", index, 1, "面料编号");
                ed.SetCellValue("Sheet1", index, 2, "面料名称");
                ed.SetCellValue("Sheet1", index, 3, "个数");
                ed.SetCellValue("Sheet1", index, 4, "实际米数");
                index++;



                foreach (var item in sortfacenamecount)
                {
                    if (item.Item3.Count == 0)
                    {
                        continue;
                    }

                    ed.SetCellValue("Sheet1", index, 1, item.Item1);
                    ed.SetCellValue("Sheet1", index, 2, item.Item2);

                    int totalcount = 0;
                    foreach (var fn in item.Item3)
                    {
                        totalcount += fn;
                    }

                    ed.SetCellValue("Sheet1", index, 3, totalcount);
                    ed.SetCellValue("Sheet1", index, 4, totalcount * Ratio);
                    index++;
                }
                string excelname = Url.Substring(Url.LastIndexOf('\\') + 1);
                ExcelUrl2 = Url + "\\小样" + excelname + "2.xlsx";
                ed.SaveAs(ExcelUrl2);
                ed.Close();
            }

            Log("导出Excel文件结束");

            {
                if (MessageBox.Show("是否更新库存表?", "更新", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    bool b0 = System.IO.File.Exists(ExcelUrl2);
                    if (!b0)
                    {
                        Log("找不到小样表文件" + ExcelUrl2 + ",更新失败");
                        return;
                    }

                    string stockexcel = Url + "\\库存.xlsx";
                    bool   b2         = System.IO.File.Exists(stockexcel);
                    if (!b2)
                    {
                        Log("找不到库存表文件" + stockexcel + "更新失败");
                        return;
                    }

                    try
                    {
                        if (!CreateStock.Create(stockexcel, "", ExcelUrl2))
                        {
                            Log("更新库存表失败!请检查文件内容有效性!");
                        }
                        else
                        {
                            Log("更新库存表成功!");
                        }
                    }
                    catch (Exception)
                    {
                        Log("更新库存表失败!请检查文件内容有效性!");
                    }
                }
            }
        }
Example #2
0
        private void ProcessExcel()
        {
            Log("导出Excel文件...");
            ExcelEdit ed = new ExcelEdit();

            ed.Create();
            int index = 1;

            ed.SetCellValue("Sheet1", index, 1, "图序号范围");
            ed.SetCellValue("Sheet1", index, 2, "面料编号");
            ed.SetCellValue("Sheet1", index, 3, "面料名称");
            ed.SetCellValue("Sheet1", index, 4, "总米数");
            ed.SetCellValue("Sheet1", index, 5, "实际米数");
            index++;
            // 面料名称、面料编号、规则名列表、文件路径列表、米数列表、图号列表
            foreach (var item in FacePathTuples)
            {
                if (item.Item3.Count == 0)
                {
                    continue;
                }
                string strnumrange = item.Item6.Count >= 2 ? item.Item6.First() + "-" + item.Item6.Last() : item.Item6[0];
                ed.SetCellValue("Sheet1", index, 1, strnumrange);
                double totalmeter = 0.0;
                foreach (var fi in item.Item5)
                {
                    totalmeter += fi;
                }
                ed.SetCellValue("Sheet1", index, 2, item.Item2);
                ed.SetCellValue("Sheet1", index, 3, item.Item1);
                ed.SetCellValue("Sheet1", index, 4, totalmeter);
                ed.SetCellValue("Sheet1", index, 5, item.Item5.Count * Ratio + totalmeter);
                index++;
            }
            string excelname = Url.Substring(Url.LastIndexOf('\\') + 1);

            ExcelUrl = Url + "\\" + excelname + ".xlsx";
            ed.SaveAs(ExcelUrl);
            ed.Close();
            Log("导出Excel文件结束");

            if (MessageBox.Show("是否更新库存表?", "更新", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                bool b0 = System.IO.File.Exists(ExcelUrl);
                if (!b0)
                {
                    Log("找不到样表文件" + ExcelUrl + "更新失败");
                    return;
                }
                string stockexcel = Url + "\\库存.xlsx";
                bool   b2         = System.IO.File.Exists(stockexcel);
                if (!b2)
                {
                    Log("找不到库存表文件" + stockexcel + "更新失败");
                    return;
                }

                try
                {
                    if (!CreateStock.Create(stockexcel, ExcelUrl, ""))
                    {
                        Log("更新库存表失败!请检查文件内容有效性!");
                    }
                    else
                    {
                        Log("更新库存表成功!");
                    }
                }
                catch (Exception)
                {
                    Log("更新库存表失败!请检查文件内容有效性!");
                }
            }
        }
Example #3
0
        /// <summary>
        /// 保存数据到文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceData"></param>
        /// <param name="fileName"></param>
        /// <param name="sheetName"></param>
        public static void saveDataToFile <T>(List <T> sourceData)
        {
            Dictionary <string, string> fileinfo = getFileInfo <T>();
            string    fileName  = fileinfo["ExcelName"];
            string    sheetName = fileinfo["SheetName"];
            ExcelEdit excel     = new ExcelEdit();
            string    str       = System.Windows.Forms.Application.StartupPath;
            Worksheet ws        = null;

            if (!Directory.Exists(str))
            {
                Directory.CreateDirectory(str);
            }
            if (!File.Exists(str + @"\" + fileName))
            {
                excel.mFilename = str + @"\" + fileName;
                excel.Create();
                excel.AddSheet(sheetName);
            }
            else
            {
                excel.Open(str + @"\" + fileName);
            }
            if (excel.GetSheet(sheetName) == null)
            {
                ws = excel.AddSheet(sheetName);
            }
            else
            {
                ws = excel.GetSheet(sheetName);
            }
            int y = 2;

            foreach (T item in sourceData)
            {
                int x = 1;
                foreach (PropertyInfo obj in item.GetType().GetProperties())
                {
                    object value = null;
                    if (y == 2)
                    {
                        value = obj.Name.ToString();
                        excel.SetCellValue(ws, 1, x, value);
                    }
                    value = item.GetType().GetProperty(obj.Name.ToString()).GetValue(item);
                    Range titleRange = ws.Range[ws.Cells[y, x],
                                                ws.Cells[y, x]];              //选中标题
                    titleRange.HorizontalAlignment = XlHAlign.xlHAlignCenter; //水平居中
                    if (titleRange.Value != null && value != null && value.Equals(titleRange.Value))
                    {
                        x++;
                        continue;
                    }
                    else
                    {
                        excel.SetCellValue(ws, y, x, value);
                        x++;
                    }
                }
                y++;
            }
            excel.wb.RefreshAll();
            if (File.Exists(str + @"\" + fileName))
            {
                excel.Save();
            }
            else
            {
                excel.SaveAs(str + @"\" + fileName);
            }
            excel.Close();
        }