0

I'm using SpreadsheetLight library to read/write *.xlsx files. In some of my files there are a lot of cells which have extra whitespaces in them.

How do I remove them efficiently ?

Can something like below work and be efficient ?

        SLDocument sl1= new SLDocument(@"D:\sample.xlsx", "Sheet1");

        SLWorksheetStatistics st=sl1.GetWorksheetStatistics();

        for (int i = 2; i <= st.EndRowIndex; ++i)
        {
            for (int j = 1; j <= st.EndColumnIndex; ++j)
            {
                if (j==3 || j==5)
                {
                    Console.WriteLine(sl1.GetCellValueAsDateTime(i,j).ToString("dd-MM-yyyy").Trim());
                }

                else if (j==4)
                {
                    Console.WriteLine(sl1.GetCellValueAsDecimal(i,j).ToString("0.00").Trim());
                }

                else
                    Console.WriteLine(sl1.GetCellValueAsString(i,j).Trim());
            }
        }
  • If it has the Leading or Trailing spaces, the methods `GetCellValueAsDateTime` and `GetCellValueAsDecimal` may fail. So it would be better do before passing the string to those methods. – Sathish Guru V Jan 23 '23 at 13:23
  • @SathishGuruV How exactly ? Do I first do a `sl1.GetCellValueAsString(i,j).Trim()` and then do `GetCellValueAsDateTime` on the same cell ? Will it be efficient ? – Jayanta_KMC Jan 23 '23 at 13:32

0 Answers0