Ejemplo n.º 1
0
        private void ReadCurrencie_OnClick(object sender, RoutedEventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                var excelEdit = new ExcelEdit();
                excelEdit.Open(AppDomain.CurrentDomain.BaseDirectory + "Currencie.xlsx");
                var sheet    = excelEdit.GetSheet("default");
                var rowCount = sheet.UsedRange.Rows.Count;
                for (int i = 1; i <= rowCount; i++) //
                {
                    if (sheet.Rows[i] == null)
                    {
                        continue;
                    }

                    var curNameList = sheet.Cells[i, "A"].Value.ToString().Split('_');
                    foreach (var s in curNameList)
                    {
                        if (!bibiList.Contains(s))
                        {
                            bibiList.Add(s);
                        }
                    }
                }

                _mainModel.CurrencieNum = bibiList.Count;
                bibiList = bibiList.OrderBy(x => x.ToLower()).ToList();

                _mainModel.Imported = true;
                _mainModel.UpdateState(_mainModel.State);
            });
        }
Ejemplo n.º 2
0
        public static int getDataInRow <T>(KeyValuePair <string, object> param)
        {
            Dictionary <string, string> fileinfo = getFileInfo <T>();
            ExcelEdit excel = new ExcelEdit();
            Dictionary <string, int> location = getColumn <T>();
            string fileName  = fileinfo["ExcelName"];
            string sheetName = fileinfo["SheetName"];
            string str       = System.Windows.Forms.Application.StartupPath;

            if (!File.Exists(str + @"\" + fileName))
            {
                return(0);
            }
            excel.Open(str + @"\" + fileName);
            excel.ws = excel.GetSheet(sheetName);
            int rows = excel.ws.UsedRange.CurrentRegion.Rows.Count;
            int crow = 0;

            for (int i = 2; i < rows + 1; i++)
            {
                Range titleRange = excel.ws.Range[excel.ws.Cells[i, location[param.Key]], excel.ws.Cells[i, location[param.Key]]];//选中标题
                if (titleRange.Value2 != null)
                {
                    var gpt = typeof(T).GetProperties();
                    var mt  = gpt.FirstOrDefault(c => c.Name == param.Key);
                    if (mt != null)
                    {
                        object obj = new object();
                        if (titleRange.Value != null)
                        {
                            Type    tp    = mt.PropertyType;
                            dynamic value = titleRange.Value;
                            switch (param.Key)
                            {
                            case "CreateDate":
                                tp    = typeof(DateTime);
                                value = DateTime.Now;
                                break;
                            }
                            obj = Convert.ChangeType(value, tp);
                        }
                        else
                        {
                            obj = titleRange.Value;
                        }
                        if (obj.Equals(param.Value))
                        {
                            crow = i;
                            break;
                        }
                    }
                }
            }
            excel.Close();
            return(crow);
        }
Ejemplo n.º 3
0
        private void executeToExcel(Record record)
        {
            string path = Application.StartupPath + ConfigurationManager.AppSettings["excelPath"].ToString();

            File.Copy(path, Application.StartupPath + @"\test.xlsx", true);
            ExcelEdit excelOperator = new ExcelEdit();

            excelOperator.Open(Application.StartupPath + @"\test.xlsx");
            Microsoft.Office.Interop.Excel.Worksheet sheet1 = excelOperator.GetSheet("Sheet1");
            sheet1.Cells[4, 5].Value = record.Operator;
            sheet1.Cells[4, 8].Value = record.CreateTime.ToString("yyyy-MM-dd");
            sheet1.Cells[6, 2].Value = record.SeriesNum;
            sheet1.Cells[6, 5].Value = record.ProName;
            sheet1.Cells[6, 8].Value = record.Result == 1 ? "合格" : "不合格";
            for (int i = 0; i < record.RecordDetailList.Count; i++)
            {
                sheet1.Cells[i + 11, 2].Value = record.RecordDetailList[i].Index;
                sheet1.Cells[i + 11, 3].Value = record.RecordDetailList[i].TestValue + " N·m";
                sheet1.Cells[i + 11, 4].Value = record.RecordDetailList[i].Standard + " N·m";
                sheet1.Cells[i + 11, 5].Value = record.RecordDetailList[i].Difference;
                sheet1.Cells[i + 11, 6].Value = record.RecordDetailList[i].Percent + "%";
                sheet1.Cells[i + 11, 7].Value = record.Result == 1 ? "合格" : "不合格";
                sheet1.Cells[i + 11, 8].Value = record.RecordDetailList[i].Upper + " N·m";
                sheet1.Cells[i + 11, 9].Value = record.RecordDetailList[i].Lower + " N·m";
            }
            if (ConfigurationManager.AppSettings["printMode"].ToString().Equals("preview"))
            {
                sheet1.PrintPreview();
            }
            else if (ConfigurationManager.AppSettings["printMode"].ToString().Equals("print"))
            {
                sheet1.PrintOutEx();
            }

            excelOperator.Save();
            excelOperator.Close();
            GC.Collect();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 从文件获取数据集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public static void getListFromFile <T>()
        {
            string className = typeof(T).Name;
            Dictionary <string, string> fileinfo = getFileInfo <T>();
            string   fileName   = fileinfo["ExcelName"];
            string   sheetName  = fileinfo["SheetName"];
            List <T> Sourcelist = new List <T>();

            if (!PubulicData.sourceData.Keys.Contains(className))
            {
                PubulicData.sourceData.Add(className, Sourcelist);
            }
            else
            {
                PubulicData.sourceData[className] = Sourcelist;
            }
            ExcelEdit excel = new ExcelEdit();
            Dictionary <string, int> location = getColumn <T>();
            string str = System.Windows.Forms.Application.StartupPath;

            if (!File.Exists(str + @"\" + fileName))
            {
                return;
            }
            excel.Open(str + @"\" + fileName);
            excel.ws = excel.GetSheet(sheetName);
            int rows = excel.ws.UsedRange.CurrentRegion.Rows.Count;

            for (int i = 2; i < rows + 1; i++)
            {
                T bd = Activator.CreateInstance <T>();;
                foreach (string j in location.Keys)
                {
                    Range titleRange = excel.ws.Range[excel.ws.Cells[i, location[j]], excel.ws.Cells[i, location[j]]];//选中标题
                    if (titleRange.Value2 != null)
                    {
                        var gpt = typeof(T).GetProperties();
                        var mt  = gpt.FirstOrDefault(c => c.Name == j);
                        if (mt != null)
                        {
                            object obj = new object();
                            if (titleRange.Value != null)
                            {
                                Type    tp    = mt.PropertyType;
                                dynamic value = titleRange.Value;
                                switch (j)
                                {
                                case "CreateDate":
                                    tp    = typeof(DateTime);
                                    value = DateTime.Now;
                                    break;
                                }
                                obj = Convert.ChangeType(value, tp);
                            }
                            else
                            {
                                obj = titleRange.Value;
                            }
                            mt.SetValue(bd, obj);
                        }
                    }
                }
                Sourcelist.Add(bd);
            }
            excel.Close();
        }
Ejemplo n.º 5
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();
        }