L
lpv kumar Gullapalli
I have written a code to Export the data from DataTable to Excel File. this
is code working fine.However, i need the Format cells=Text. The entire work
sheet should appear in Text. even when a number is inthe cell it should be
treated a Text. plz give me necessary solutions for this Code snippet.
public static void GenerateReport(DataTable exportData)
{
Excel.Application objExcel = new Excel.ApplicationClass();
objExcel.Application.Workbooks.Add(true);
objExcel.Visible = true;
objExcel.ActiveCell.NumberFormat = objExcel.ActiveCell.Text;
objExcel.Cells.NumberFormat = objExcel.Cells.Text;
int ColumnIndex = 0;
foreach (DataColumn dCol in exportData.Columns)
{
ColumnIndex++;
objExcel.Cells[1, ColumnIndex] = dCol.ColumnName;
}
int rowIndex = 0;
foreach (DataRow dr in exportData.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn rcol in exportData.Columns)
{
ColumnIndex++;
objExcel.Cells[rowIndex + 1, ColumnIndex]
=dr[rcol.ColumnName];
}
}
objExcel.Visible = true;
Excel.Worksheet worksheet =
(Excel.Worksheet)objExcel.ActiveSheet;
worksheet.Activate();
}
is code working fine.However, i need the Format cells=Text. The entire work
sheet should appear in Text. even when a number is inthe cell it should be
treated a Text. plz give me necessary solutions for this Code snippet.
public static void GenerateReport(DataTable exportData)
{
Excel.Application objExcel = new Excel.ApplicationClass();
objExcel.Application.Workbooks.Add(true);
objExcel.Visible = true;
objExcel.ActiveCell.NumberFormat = objExcel.ActiveCell.Text;
objExcel.Cells.NumberFormat = objExcel.Cells.Text;
int ColumnIndex = 0;
foreach (DataColumn dCol in exportData.Columns)
{
ColumnIndex++;
objExcel.Cells[1, ColumnIndex] = dCol.ColumnName;
}
int rowIndex = 0;
foreach (DataRow dr in exportData.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn rcol in exportData.Columns)
{
ColumnIndex++;
objExcel.Cells[rowIndex + 1, ColumnIndex]
=dr[rcol.ColumnName];
}
}
objExcel.Visible = true;
Excel.Worksheet worksheet =
(Excel.Worksheet)objExcel.ActiveSheet;
worksheet.Activate();
}