Exemplo n.º 1
0
        /// <summary>
        /// 全部导出
        /// </summary>
        /// <param name="onCompleted">完成回调</param>
        /// <param name="onStatus">状态</param>
        public void ExportAll(OnCompletedHandler onCompleted, OnStatusHandler onStatus)
        {
            // 文件的状态,0 未处理,1 处理中,2 处理完
            Dictionary <string, int> status = _paths.ToDictionary(path => path, _ => 0);

            for (int i = 0; i < Math.Min(Config.ExportThreadCount, status.Count); ++i)
            {
                int  threadCount = 0;
                bool isBreak     = false;
                var  thread      = new Thread(() =>
                {
                    ++threadCount;

                    foreach (var path in status.Keys.ToArray())
                    {
                        if (isBreak)
                        {
                            break;
                        }
                        if (status[path] == 0)
                        {
                            status[path] = 1;
                            onStatus?.Invoke(null, "正在处理...", status.Values.ToArray().Count(e => e == 2) * 1.0 / status.Count + "");
                            if (Export(path, onStatus))
                            {
                                status[path] = 2;
                                onStatus?.Invoke(null, "正在处理...", status.Values.ToArray().Count(e => e == 2) * 1.0 / status.Count + "");
                            }
                            else
                            {
                                isBreak = true;
                            }
                        }
                    }

                    --threadCount;
                    if (threadCount == 0)
                    {
                        onStatus?.Invoke(null, isBreak ? "中断" : "完成", null, isBreak);
                        onCompleted?.Invoke(!isBreak);
                    }
                });
                thread.IsBackground = true;
                thread.Start();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="onStatus">状态回调</param>
        /// <returns></returns>
        public List <string> Load(OnStatusHandler onStatus)
        {
            _paths.Clear();
            var importDir = new DirectoryInfo(Config.ImportDir);

            if (importDir.Exists)
            {
                foreach (var fileInfo in importDir.GetFiles("*.xlsx"))
                {
                    _paths.Add(fileInfo.FullName);
                    onStatus?.Invoke(fileInfo.FullName, "就绪", "");
                }
            }
            onStatus?.Invoke(null, "载入完成  一共" + _paths.Count + "个文件", "-1");

            return(_paths);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 导出
 /// </summary>
 /// <param name="path">xlsx文件路径</param>
 /// <param name="onStatus">状态</param>
 public bool Export(string path, OnStatusHandler onStatus)
 {
     onStatus?.Invoke(path, "正在处理", "正在分析...");
     try
     {
         using (Workbook workbook = new Workbook(path))
         {
             int sheetIndex = 0;
             while (workbook.Read(out var worksheet))
             {
                 ++sheetIndex;
                 // 读取
                 string readProgressFormat = string.Format("表({0}/{1}), 正在读取, 表: {2}, ", sheetIndex, workbook.WorksheetCount, worksheet.Name) + "({0})";
                 if (Read(path, worksheet, out List <List <object> > sheet, readProgressFormat, onStatus))
                 {
                     /** 转换 */
                     string csvConvertProgressFormat = string.Format("表({0}/{1}), 正在转换CSV, 表: {2}, ", sheetIndex, workbook.WorksheetCount, worksheet.Name) + "{0}";
                     if (new CsvConverter().Convert(path, sheet, out string csvData, csvConvertProgressFormat, onStatus))
                     {
                         using (var stream = new FileStream(Config.ExportDir + "/" + worksheet.Name + ".csv", FileMode.Create))
                         {
                             var writer = new StreamWriter(stream, new System.Text.UTF8Encoding(false));
                             writer.Write(csvData);
                             writer.Flush();
                             writer.Close();
                         }
                     }
                 }
             }
             onStatus?.Invoke(path, "完成", null);
         }
     }
     catch (Exception e)
     {
         onStatus?.Invoke(path, "错误", e.Message, true);
         return(false);
     }
     return(true);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 转换工作表数据
 /// </summary>
 /// <param name="path">工作簿路径</param>
 /// <param name="sheet">工作表数据</param>
 /// <param name="content">工作表数据</param>
 /// <param name="progressFormat">进度提示格式化</param>
 /// <param name="onStatus">状态回调</param>
 public abstract bool Convert(string path, List <List <object> > sheet, out string content, string progressFormat = "{0}", OnStatusHandler onStatus = null);
Exemplo n.º 5
0
        public bool Read(string path, Worksheet worksheet, out List <List <object> > data, string progressFormat = "{0}", OnStatusHandler onStatus = null)
        {
            data = new List <List <object> >();
            onStatus?.Invoke(path, "正在处理", string.Format(progressFormat, "(0/" + worksheet.RowCount + ")"));

            int rowIndex = 0;

            while (worksheet.Read(out var row))
            {
                ++rowIndex;
                List <object> rowData = new List <object>();
                foreach (var cell in row)
                {
                    /** 校验单元格数据 */
                    //

                    rowData.Add(cell.Value);
                }
                if (rowData.Count > 0)
                {
                    data.Add(rowData);
                }

                onStatus?.Invoke(path, "正在处理", string.Format(progressFormat, "(" + rowIndex + "/" + worksheet.RowCount + ")"));
            }

            return(true);
        }
Exemplo n.º 6
0
        public override bool Convert(string path, List <List <object> > sheet, out string content, string progressFormat = "{0}", OnStatusHandler onStatus = null)
        {
            int rowIndex = 0;

            onStatus?.Invoke(path, "正在处理", string.Format(progressFormat, "0%"));
            StringBuilder data = new StringBuilder();

            foreach (var row in sheet)
            {
                StringBuilder rowData = new StringBuilder();
                foreach (var cell in row)
                {
                    string value = "";
                    if (cell is bool)
                    {
                        value = (bool)cell ? "1" : "0";
                    }
                    else if (cell is bool[])
                    {
                        foreach (var subRawValue in (bool[])cell)
                        {
                            if (value.Length > 0)
                            {
                                value += ",";
                            }
                            value += subRawValue ? "1" : "0";
                        }
                        if (value.Length > 0)
                        {
                            value = '"' + value + '"';
                        }
                    }
                    else if (cell is uint8 || cell is int8 ||
                             cell is uint16 || cell is int16 ||
                             cell is uint32 || cell is int32 ||
                             cell is uint64 || cell is int64 ||
                             cell is float || cell is float64)
                    {
                        value = cell.ToString();
                    }
                    else if (cell is uint8[] || cell is int8[] ||
                             cell is uint16[] || cell is int16[] ||
                             cell is uint32[] || cell is int32[] ||
                             cell is uint64[] || cell is int64[] ||
                             cell is float[] || cell is float64[])
                    {
                        foreach (var subRawValue in (Array)cell)
                        {
                            if (value.Length > 0)
                            {
                                value += ",";
                            }
                            value += subRawValue.ToString();
                        }
                        if (value.Length > 0)
                        {
                            value = '"' + value + '"';
                        }
                    }
                    else if (cell is string)
                    {
                        string rawValue = (string)cell;
                        if (rawValue.IndexOf(',') != -1 || rawValue.IndexOf('"') != -1 || rawValue.IndexOf('\r') != -1 || rawValue.IndexOf('\n') != -1)
                        {
                            value = '"' + rawValue.Replace("\"", "\"\"") + '"';
                        }
                        else
                        {
                            value = rawValue;
                        }
                    }
                    else if (cell is string[])
                    {
                        foreach (var subRawValue in (string[])cell)
                        {
                            if (value.Length > 0)
                            {
                                value += ",";
                            }
                            if (subRawValue.IndexOf(',') != -1 || subRawValue.IndexOf('"') != -1 || subRawValue.IndexOf('\r') != -1 || subRawValue.IndexOf('\n') != -1)
                            {
                                value += subRawValue.Replace("\"", "\"\"");
                            }
                            else
                            {
                                value += subRawValue;
                            }
                        }
                        if (value.Length > 0)
                        {
                            value = '"' + value + '"';
                        }
                    }

                    if (rowData.Length > 0)
                    {
                        rowData.Append(',');
                    }
                    rowData.Append(value);
                }

                rowData.Append("\r\n");
                data.Append(rowData.ToString());

                ++rowIndex;
                onStatus?.Invoke(path, "正在处理", string.Format(progressFormat, (int)(rowIndex * 100.0 / sheet.Count) + "%"));
            }

            content = data.ToString();
            return(true);
        }