0

I'm using C# and EPPlus to import .csv data into a workbook. My only issue is that after the import into Excel, I am losing trailing zeroes on the data.

For example:

  • in the .CSV file, the data is this: 1250.80
  • in the .xlsx file, the data ends up being 1250.8

Seems minor but I need this to compare workbook to workbook and not see all of these diffs.

Here is the read routine

string[] csvInputfile = File.ReadAllLines(filePathName);

Here is how I am trying to put double quotes around the data

for (int idx = 0; idx < csvInputfile.Length; idx++)
{
    csvInputfile[idx] = csvInputfile[idx].Replace(",", ",\"");
}

Here I am writing the contents to the Excel instance

for (int i = 0; i < csvInputfile.Length; i++)
{
    ExcelWorksheet curWorksheet = excelPackage.Workbook.Worksheets.Add(csvInputfile[outerLoopCnt]);

    for (int j = 0; j < csvInputfile.Length; j++)
    {
        innerLoopCnt++;

        curWorksheet.Cells[j + 1, 1, j + 1, 100].Style.Numberformat.Format = "@";
        curWorksheet.Cells[j + 1, 1].LoadFromText(csvInputfile[innerLoopCnt], format);

        if (csvInputfile[innerLoopCnt + 1] == "")
        {
            outerLoopCnt = innerLoopCnt + 2;
            innerLoopCnt += 2;

            break;
        }
    }

    if (innerLoopCnt == csvInputfile.Length) 
    {
        break; 
    }
}

Here I save the file:

excelPackage.SaveAs(file);

This is the general idea and its not working very well. This is a crude approach but I'm not sure how else to make this work, I would appreciate any help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Try to set numeric format for columns with numbers https://stackoverflow.com/a/40214134/7357322 – Andrii Khomiak Jun 02 '22 at 18:31
  • Sorry if I am missing something, however, you should be able to directly open the CSV file from Excel. Excel can read CSV files and gives you the option to set the delimiters. It just appears to me that you are re-inventing the wheel unnecessarily. – JohnG Jun 02 '22 at 19:22

0 Answers0