datagridview导出0开头的数字串后,开头的0没了
winform中的datagridview中的带0开头的字符串,导出后,开头的0不见了。比如:员工编号:00011,导出成EXCEL后,只有11,而不是00011.
有人说在导出的语句加上“‘”,但是这样导出后把’也加进了并显示了,变成了‘00011.
请问有没有更好的办法,代码如下,请指导。
saveFileDialog.Filter = "execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
progreesBar.Visible = true;
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 < gridView.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += gridView.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < gridView.Rows.Count - 1; j++)
{
string tempStr = "";
for (int k = 0; k < gridView.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += "' " + gridView.Rows[j].Cells[k].Value.ToString(); }
sw.WriteLine(tempStr);
progreesBar.Value += 100 / gridView.RowCount;
}
sw.Close();
myStream.Close();
progreesBar.Value = 100;
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
progreesBar.Value = 0;
progreesBar.Visible = false;
------解决方案--------------------以前也遇到过这问题,去网上找找设置单元格的格式,如果默认的单元格格式是数字型的,则0开头的数字就会自动省略0,判断当前要写入的数字如果是0开头这个单元格,则单元格设置成文本型的,就OK了
------解决方案--------------------设置单元格格式、合并单元格