DataGrid中的数据导入Excel后,数据格式怎么转换??
例如:学号为200601001 导入Excel后就变成为了科学计数法,请高手们看看 应该怎么改代码!!!!!!谢谢!代码如下所示:
private void button4_Click(object sender, System.EventArgs e)
{
OleDbConnection cn = new OleDbConnection("Data Source=xsxxdb.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0");
cn.Open();
string sql = "select StudentInfo.StudentId as 自动编号,studentinfo.studentnumber as 学号,studentinfo.name as 姓名,studentinfo.sex as 性别,studentinfo.race as 族别,studentinfo.address as 家庭住址,studentinfo.telephone as 联系方式,studentinfo.role as 政治面貌,studentinfo.postalcode as 邮政编码,classinfo.classname as 班级名称,studentinfo.remark as 备注 from studentinfo inner join classinfo on studentinfo.classid = classinfo.classid where classinfo.classname='" + comboBox2.Text.ToString() + "' order by studentnumber";
OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
DataSet ds = new DataSet();
adp.Fill(ds, "student");
AddExcel(ds);
}
protected void AddExcel(DataSet ds)
{
DataTable dt = ds.Tables["student"];
string fileName = Guid.NewGuid() + ".xls";
Excel.Application excel = new Excel.ApplicationClass();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
foreach (DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
{
excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
}
}
excel.Visible = true;
excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
GC.Collect();
}