示例#1
0
        /// <summary>
        /// 导出为键值对
        /// </summary>
        private void ToKvExcel(bool isArray, string regexStr, params Func <string, bool>[] predicate)
        {
            var dtArray  = new List <System.Data.DataTable>();
            var dtObject = new List <DataTable>();

            Regex regex = string.IsNullOrEmpty(regexStr) ? null : new Regex(regexStr);

            CheckPath(getSelectExtension).ForEach((file, index) =>
            {
                Console.WriteLine(" is now : " + file);
                var dts = import(file).Where(p => p != null).ToList();
                if (dts.Count <= 0)
                {
                    return;
                }
                dts.ForEach(dt =>
                {
                    if (File.Exists(InputPath))
                    {
                        dt.TableName = Path.GetFileName(file);
                    }
                    else if (Directory.Exists(InputPath))
                    {
                        dt.TableName = file.Replace(InputPath, "");
                    }
                    else
                    {
                        throw new Exception();
                    }

                    //if (dt.IsArray)
                    if (isArray)
                    {
                        var list = ExcelUtils.ConvertToRowsList(dt)
                                   .Select(p => {
                            bool result = false;
                            foreach (var item in p)
                            {
                                if (regex != null && CheckMatches(regex, item.ToString()))
                                {
                                    continue;
                                }
                                result = true;
                                break;
                            }
                            return(new { p, result });
                        })
                                   .Select(p => p.result)
                                   .ToList();

                        //返回符合正则表达式的列
                        var header = dt.GetHeaderList().Where((p, i) => (i == 0 || list[i])).ToList();

                        var resdt = dt.DefaultView.ToTable(false, header.ToArray());

                        if (header.Count > 1)
                        {
                            dtArray.Add(resdt);
                        }
                    }
                    else
                    {
                        var lt  = dt.ToListTable();
                        lt.Rows = lt.Rows
                                  .ToDictionary(p => p, p => string.Join("", p.Select(q => q.ToString()).ToArray()))
                                  .Where(p => predicate.Length == 0 || predicate.First()(p.Value))
                                  .ToList()
                                  .Where(p => predicate.Length == 0 || predicate.Last()(p.Value))
                                  .Select(p => p.Key)
                                  .ToList();
                        //返回符合正则表达式的行
                        dtObject.Add(lt.ToDataTable());
                    }
                });
            });

            if (dtArray.Count == 0 && dtObject.Count == 0)
            {
                return;
            }

            DataTable dd = new DataTable();

            dd.Columns.Add("path", typeof(string));
            dd.Columns.Add("id", typeof(string));
            dd.Columns.Add("key", typeof(string));
            dd.Columns.Add("value", typeof(string));
            dd.Columns.Add("value_zh_cn", typeof(string));

            if (dtArray.Count != 0)
            {
                foreach (System.Data.DataTable dataTable in dtArray)
                {
                    var header = dataTable.GetHeaderList();
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        foreach (string s in header.Skip(1))
                        {
                            if (string.IsNullOrEmpty(dr[s].ToString()))
                            {
                                continue;
                            }
                            if (regex != null && CheckMatches(regex, dr[s].ToString()))
                            {
                                continue;
                            }
                            dd.Rows.Add(dataTable.TableName, dr[0], s, dr[s], "");
                        }
                    }
                }
            }

            if (dtObject.Count != 0)
            {
                foreach (DataTable dataTable in dtObject)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        if (string.IsNullOrEmpty(dr[1].ToString()))
                        {
                            continue;
                        }
                        if (regex != null && CheckMatches(regex, dr[1].ToString()))
                        {
                            continue;
                        }
                        dd.Rows.Add(dataTable.TableName, dr[0], Path.GetFileName(dr[0].ToString()), dr[1], "");
                    }
                }
            }

            ExcelUtils.ExportToExcel(dd, InputPath);
        }