예제 #1
0
        internal static void Read(TextReader InString, ExcelFile Workbook, int[] ColumnWidths, int FirstRow, int FirstCol, ColumnImportType[] ColumnFormats, string[] DateFormats)
        {
            if (Workbook.VirtualMode)
            {
                Workbook.OnVirtualCellStartReading(Workbook, new VirtualCellStartReadingEventArgs(Workbook));
            }

            int         r  = FirstRow - 1;
            TextWithPos sr = new TextWithPos(InString);
            {
                while (true)
                {
                    string line = sr.GetLine();
                    if (line == null)
                    {
                        return;
                    }
                    r++;

                    int c         = FirstCol;
                    int FirstChar = 0;
                    for (int i = 0; i < ColumnWidths.Length; i++)
                    {
                        if (line.Length <= FirstChar)
                        {
                            break;
                        }
                        int    cw = Math.Min(ColumnWidths[i], line.Length - FirstChar);
                        string s  = line.Substring(FirstChar, cw);
                        FirstChar += cw;

                        if ((ColumnFormats != null) && (i < ColumnFormats.Length))
                        {
                            switch (ColumnFormats[i])
                            {
                            case ColumnImportType.Text: TextDelim.SetCellValue(Workbook, r, c, s.ToString()); c++; break;

                            case ColumnImportType.Skip: break;

                            default: TextDelim.SetCellFromString(Workbook, r, c, s.ToString(), DateFormats); c++; break;
                            } //case
                        }
                        else
                        {
                            TextDelim.SetCellFromString(Workbook, r, c, s.ToString(), DateFormats);
                            c++;
                        }
                    }
                }
            }
        }
예제 #2
0
 private static void ReadNString(TextWithPos sr, char Delim, StringBuilder s, ref int ch)
 {
     s.Length = 0;
     s.Append((char)ch);
     while (ch >= 0)
     {
         ch = sr.GetNextChar();
         if ((ch == Delim) || (ch == 13) || (ch == 10) || (ch < 0))
         {
             return;
         }
         s.Append((char)ch);
     } //while
 }
예제 #3
0
        private static void ReadQString(TextWithPos sr, StringBuilder s, ref int ch)
        {
            bool InQuote = false;

            s.Length = 0;
            while (ch >= 0)
            {
                ch = sr.GetNextChar();
                if (ch < 0)
                {
                    return;
                }
                if ((ch != '"') && InQuote)
                {
                    return;
                }
                if (InQuote || (ch != '"'))
                {
                    s.Append((char)ch);
                }
                InQuote = (ch == '"') && !InQuote;
            }
        }
예제 #4
0
        internal static void Read(TextReader InString, ExcelFile Workbook, char Delim, int FirstRow, int FirstCol,
                                  ColumnImportType[] ColumnFormats, string[] DateFormats)
        {
            if (Workbook.VirtualMode)
            {
                Workbook.OnVirtualCellStartReading(Workbook, new VirtualCellStartReadingEventArgs(Workbook));
            }
            int           r  = FirstRow;
            int           c  = FirstCol;
            TextWithPos   sr = new TextWithPos(InString);
            StringBuilder s  = new StringBuilder();
            {
                int ch = sr.GetNextChar();
                while (ch >= 0)
                {
                    if (ch == '"')
                    {
                        ReadQString(sr, s, ref ch);
                    }
                    else if (ch == Delim)
                    {
                        c++;
                        ch = sr.GetNextChar();
                        continue;
                    }
                    else if (ch == 10) //there are 3 types of EOL: Win (13 10) Mac (10)  and Ms.Dos(13)
                    {
                        c = FirstCol;
                        r++;
                        ch = sr.GetNextChar();
                        continue;
                    }
                    else if (ch == 13)
                    {
                        c = FirstCol;
                        r++;
                        ch = sr.GetNextChar();
                        if (ch == 10)
                        {
                            ch = sr.GetNextChar();
                        }
                        continue;
                    }
                    else
                    {
                        ReadNString(sr, Delim, s, ref ch);
                    }

                    if ((ColumnFormats != null) && (c - FirstCol < ColumnFormats.Length))
                    {
                        switch (ColumnFormats[c - FirstCol])
                        {
                        case ColumnImportType.Text: SetCellValue(Workbook, r, c, s.ToString()); break;

                        case ColumnImportType.Skip: break;

                        default: SetCellFromString(Workbook, r, c, s.ToString(), DateFormats); break;
                        } //case
                    }
                    else
                    {
                        SetCellFromString(Workbook, r, c, s.ToString(), DateFormats);
                    }
                }
            }
        }