1

I am working on a C# Console application which export data from a DataTable to an excel file. My DataTable has few columns of type Text and I also have a column which just has a month of type Text in the form of "May-15" but in the excel file it is automatically converting this to a date. How do I just export plain text to my excel.

I am using the Excel Interop assembly in C# as follows

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = excelApp.Workbooks.Add(Type.Missing);


            Microsoft.Office.Interop.Excel._Worksheet WhiteBoardData = null;
            WhiteBoardData = workbook.Sheets[1];
            // changing the name of active sheet
            WhiteBoardData.Name = "WhiteBoard-Data";
            int z = 1;
            foreach (DataColumn dc in dtWhiteBoardData.Columns)
            {

                WhiteBoardData.Cells[1, z] = dc.ColumnName.ToString();
                z++;
            }
            for (int k = 0; k < dtWhiteBoardData.Rows.Count; k++)
            {
                for (int x = 0; x < dtWhiteBoardData.Columns.Count; x++)
                {
                    WhiteBoardData.Cells[k + 2, x + 1] = dtWhiteBoardData.Rows[k][x].ToString();
                }
            }

How do I fix this?

pnuts
  • 58,317
  • 11
  • 87
  • 139
DoIt
  • 3,270
  • 9
  • 51
  • 103
  • You have to set cell's format to text - http://stackoverflow.com/questions/13827709/set-data-type-like-number-text-and-date-in-excel-column-using-microsoft-office. – Eugene Podskal Apr 10 '15 at 17:39
  • try adding a single apostrophe `'` before every piece of data.... as you are displaying every column as string. this wont show up in the excel too.. – Saagar Elias Jacky Apr 10 '15 at 17:50

0 Answers0