急:excel列格式的控制????
………………
GC.Collect();// clean up any other excel guys hangin' around...
excel.Visible = false;
//Get a new workbook.
oWB = (Excel._Workbook)(excel.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
for(int j=0;j<dt.Columns.Count;j++)
{
oSheet.Cells[1, j+1] = dt.Columns[j].ColumnName.ToString();
}
// build the sheet contents
foreach(DataRow Row in dt.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn Col in dt.Columns)
{
colIndex++;
if(Col.DataType == System.Type.GetType("System.DateTime"))
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(Row[Col.ColumnName].ToString())).ToString("yyyy-MM-dd");
else
oSheet.Cells[rowIndex,colIndex]=Row[Col.ColumnName].ToString();
}
}
//Format A1:Z1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "Z1").Font.Bold = true;
oSheet.get_Range("A1", "Z1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
……………………
用此种方法可以生成excel,但小弟有一个疑问比较着急
比如我的有一列表示材料编码,有材料编码 01010002
则按照上面的方法生成excel后,打开,看到的是 1010002,是系统认为此列为数字了。请问如何讲列的格式设置为文本,以及其他的。