Пример #1
0
        public void BuildSplitFileWithRows(List <ReportRow> fileRows, string savePath)
        {
            StringBuilder lineBuilder = new StringBuilder();

            #region copy Header
            using (StringReader sr = new StringReader(_txtContent))
            {
                int crtLine = 0;
                while (crtLine < ReportFile_HeadLine_Length)
                {
                    crtLine++;
                    lineBuilder.AppendLine(sr.ReadLine());
                }
            }
            #endregion

            using (StreamWriter sw = new StreamWriter(savePath, false))
            {
                sw.Write(lineBuilder.ToString());

                string fieldStr = string.Empty;
                for (int i = 0, j = fileRows.Count; i < j; i++)
                {
                    lineBuilder.Clear();

                    ReportRow row = fileRows[i];
                    for (int m = 0, n = Columns.Count; m < n; m++)
                    {
                        var col = Columns[m];
                        if (m < n - 1)
                        {
                            fieldStr = row[col.ColumnName].PadRight(col.ColumnLength, ' ');
                            lineBuilder.Append(fieldStr);
                            lineBuilder.Append(new string(' ', ReportFile_Space_Length));
                        }
                        else
                        {
                            fieldStr = row[col.ColumnName];
                            lineBuilder.Append(fieldStr);
                        }
                    }

                    sw.WriteLine(lineBuilder.ToString());
                }
                sw.Close();
            }
        }
Пример #2
0
        public void FindRowAction(Action <ReportRow> rowAct, bool onErrorBreak = false)
        {
            Exception lastError = null;

            using (var c = new ReaderCursor(_txtContent))
            {
                c.SkipLine(ReportFile_HeadLine_Length);
                int chrPeek = -1;
                while ((chrPeek = c.__r__.Peek()) != -1)
                {
                    if (chrPeek == (int)'\r' || chrPeek == (int)'\n')
                    {
                        break;
                    }

                    ReportRow item = new ReportRow();
                    for (int i = 0, j = Columns.Count; i < j; i++)
                    {
                        var col = Columns[i];
                        if (i < j - 1)
                        {
                            char[] varChars  = new char[col.ColumnLength];
                            int    totalRead = c.__r__.Read(varChars, 0, varChars.Length);
                            item[col.ColumnName] = (new string(varChars, 0, totalRead)).TrimEnd();
                            c.SkipColumn(ReportFile_Space_Length);
                        }
                        else
                        {
                            StringBuilder sb       = new StringBuilder();
                            int           readChar = -1;
                            while ((readChar = c.__r__.Read()) != -1)
                            {
                                char chr = (char)readChar;
                                if (chr == '\n')
                                {
                                    break;
                                }
                                else
                                {
                                    sb.Append(chr);
                                }
                            }
                            item[col.ColumnName] = sb.ToString().TrimEnd();
                            continue;
                        }
                    }

                    try
                    {
                        rowAct(item);
                    }
                    catch (Exception ex)
                    {
                        lastError = ex;
                        if (onErrorBreak)
                        {
                            break;
                        }
                    }
                }
            }

            if (lastError != null)
            {
                throw lastError;
            }
        }