C# Excel Interop: How to format cells to store values as text

前端 未结 3 1755
一生所求
一生所求 2021-02-07 04:05

I\'m writing numbers to an Excel spreadsheet from a DataTable and all of these numbers are 5 digits long with preceding 0s if the number itself is less than 5 digit

3条回答
  •  日久生厌
    2021-02-07 04:24

    This answer just solved a major problem with a solution from one of our company's software, I had to retrieve the value as displayed, but once I set it to the new sheet, it was being inserted as a number. Simple solution. I cant vote up as yet, but down follows how it ended up.

    for (int h = 1; h <= 1; h++)
    {
        int col = lastColl(sheets);
        for (int r = 1; r <= src.Count; r++)
        {
            sheets.Cells[r, col + 1] = "'"+src.Cells[r, h].Text.ToString().Trim();
        }
    }
    

提交回复
热议问题