我用如下方法把DataGridView的数据导入Excel后:
因为我有些字段的值是全数字的并以0开头,Excel默认的情况下会当作数字型,这样会把开头的0去掉。能不能把Excel单元格的类型都设置为文本型??
SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";            saveFileDialog.ShowDialog();            if (saveFileDialog.FileName != "")
            {
                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                string str = "";
                try
                {
                    //写标题 
                    for (int i = 0; i < this.sc_madanDataGridView.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            str += "\t";
                        }
                        str += sc_madanDataGridView.Columns[i].HeaderText;
                    }                    sw.WriteLine(str);
                    //写内容
                    for (int j = 0; j < sc_madanDataGridView.Rows.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < sc_madanDataGridView.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "\t";
                            }
                            tempStr += sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }

解决方案 »

  1.   

    可单独设置EXCEL文件的格式为文本。如因版本问题不能引用COM进行设置的话就请上网搜索并参照late binding 方法可调用到EXCEL中的Selection.NumberFormatLocal = "@"或是你将所有的数据前面都加上"@",这个没试过,不知道会有什么效果,呵呵~~~
      

  2.   

    由于是采用stream读写,而非com组件,其他的方法都不行。在需要作处理的数据前加"'"单引号。
      

  3.   

    由于是采用stream读写,而非com组件
    对的,但是我加了单引号后是直接在Excel里输出,没有作用。
    tempStr +="'" + sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
      

  4.   

    楼主你把问题在描述清楚点吧。你可以新建一个excel文件,看看在一个单元格里输入"'01234"和"01234"的区别
      

  5.   

    tempStr += sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
    然后
    sw.WriteLine(tempStr);
    这样写进去,是将一行写到一个单元格里还是多个格里?
      

  6.   

    for (int k = 0; k < sc_madanDataGridView.Columns.Count; k++)
       {
          if (k > 0)
            {
                tempStr += "\t";
            }
         tempStr +="'" + sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
       }
    sw.WriteLine(tempStr);比较完整的是这样的。现在写到Excel里显示是“'001001”,当双击单元格后“'”就不见了
      

  7.   

    worksheet.get_Range(xlApp.Cells[1, 3], xlApp.Cells[10, 3]).NumberFormatLocal = "000000000000";//设置单元格格式 此处为3列的1到10行显示为12位的数字,不足的位置用0补齐
      

  8.   

    to:ericsama()你的方法是调用com的方法,我现在是采用stream读写.谢谢!