导出身份证号码是出错
导出代码如下:
SaveFileDialog saveFileDialog = new SaveFileDialog();//打开一个文件保存对话框
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = saveFileDialog.OpenFile();
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString()+".";
// tempStr += dataGridView1.Rows[j].Cells[k].EditedFormattedValue.ToString();
//tempStr += "" + dataGridView1.Rows[j].Cells[k].Value == "" ? "" : dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
导出的结果为:
4.30321199E+17 这是在表格中显示的。
430321199002113000 这是在编辑栏显示的
这是什么原因。导出的结果应该为430321199002113711
希望有高手教教
------解决方案--------------------tempStr += "'" + dataGridView1.Rows[j].Cells[k].Value.ToString()+".";
把红色的加上
------解决方案--------------------这个问题,再导出到excel之前,对datagridview执行以下操作,比如身份证号码是第2列
C# code
for(int i=0;i<datagridview.rows.count;i++)
{
dataGridView1.Rows[i].Cells[1].Value = "'" + dataGridView1.Rows[i].Cells[1].Value.ToString();
}