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 }
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; } }
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); } } } }