using System.IO; using GemBox.Spreadsheet; public sealed class CsvFileReader : TextReader { private const int MaxRow = 1048576; private readonly TextReader reader; private int currentRow; private bool isCompleted; public static ExcelFile Read(string path, CsvLoadOptions options) { var workbook = new ExcelFile(); int sheetIndex = 0; using (var reader = new CsvFileReader(path)) while (reader.CanReadNextSheet()) { ++sheetIndex; workbook.Worksheets.AddCopy( string.Format("Sheet {0}", sheetIndex), ExcelFile.Load(reader, options).Worksheets.ActiveWorksheet); } return workbook; } private CsvFileReader(string path) { this.reader = File.OpenText(path); } private bool CanReadNextSheet() { if (this.isCompleted) return false; this.currentRow = 0; return true; } public override string ReadLine() { if (this.currentRow < MaxRow) { ++this.currentRow; string line = this.reader.ReadLine(); if (line == null) this.isCompleted = true; return line; } return null; } protected override void Dispose(bool disposing) { this.reader.Dispose(); } }