Skip to main content
4 of 5
fixed formatting
mseancole
  • 6.2k
  • 1
  • 16
  • 27

Is there any room to optimize this class (code)

Reading the data from Excel sheet and holding on the variable in C# and I have created the class that read from Excel but I am not sure If my code is optimized meaning I will be reading heavy from excel

Is there a room to optimized this code more?, I will be using this class heavy in my code and thought get some opinion/feedback -

I will be calling something like this...:

ExcelReader xl = new ExcelReader();
string sss = xl.ExcelOpenSpreadsheets("mysheet");
//etc...

public class ExcelReader  
{             
    Application _excelApp;
    
    public ExcelReader()
    {
        _excelApp = new Application();
    }
    
    public string ExcelOpenSpreadsheets(string sheetName)
    {
        string _txt = string.Empty;
        try
        {
            Workbook workBook = _excelApp.Workbooks.Open("filename_here",....);
            _txt = ExcelScanIntenal(workBook, sheetName);
        }
        
        catch
        {
            //
            // Deal with exceptions.
            //
        }
        return _txt;
    }
    
    private string ExcelScanIntenal(Workbook workBookIn, string sheetName)
    {
        Worksheet sheet = workBookIn.Sheets[sheetName] as Worksheet;
        
        Range a1 = sheet.get_Range("A1", "B2");
        if (a1 != null)
        {
            string formattedText = r.Text;                   
        }
        
        return formattedText; 
    }
Nick Kahn
  • 259
  • 1
  • 2
  • 7