首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

将数据从DataGridView导入到Execel中抛出奇怪错误! 苦苦思索,毫无进展,希望

2012-04-09 
将数据从DataGridView导入到Execel中抛出奇怪异常!!苦苦思索,毫无进展,希望高手指点usingSystemusingSyst

将数据从DataGridView导入到Execel中抛出奇怪异常!! 苦苦思索,毫无进展,希望高手指点
using   System;
using   System.Collections.Generic;
using   System.Text;
using   System.Windows.Forms;
using   System.IO;

namespace   CangKuGuanli.BLL
{
    public   class   ExportXLS
        {
            private     string   tempStr;
            private     string   str;  
            public   void   ExportDataGridViewToExcel(DataGridView   dataGridview1)
                {
                    SaveFileDialog   saveFileDialog   =   new   SaveFileDialog();
                    saveFileDialog.Filter   =   "Execl   files   (*.xls)|*.xls ";
                    saveFileDialog.FilterIndex   =   0;
                    saveFileDialog.RestoreDirectory   =   true;
                    saveFileDialog.CreatePrompt   =   false;
                    saveFileDialog.Title   =   "导出Excel文件到 ";  
                 
                    if   (saveFileDialog.ShowDialog()   ==   DialogResult.Cancel)
                            return;
                    Stream   myStream   =   saveFileDialog.OpenFile();
                    StreamWriter   sw   =   new   StreamWriter(myStream,System.Text.Encoding.GetEncoding( "gb2312 "));   //gb2312

                    try
                        {
                                //写标题  
                                for   (int   i   =   0;   i <   dataGridview1.ColumnCount;   i++)
                                {
                                        if   (i   ==   0)   dataGridview1.Columns[i].HeaderText   =   "序号 ";
                                        if   (i   >   0)
                                        {
                                                str   +=   "\t ";


                                        }
                                     
                                        str   +=   dataGridview1.Columns[i].HeaderText;
                                }

                                sw.WriteLine(str);
                                //写内容
                 
  1                               for(int   j   =   0;   j   <   dataGridview1.Rows.Count;   j++)
  2                               {
  3                                       tempStr   =   " ";
  4                              
  5                                       for   (int   k   =   0;   k   <   dataGridview1.Columns.Count;   k++)
  6                                       {
  7                                            
  8                                               if(   k   ==   0   )
  9                                                       dataGridview1.Rows[j].Cells[k].Value   =   j   +   1;
  10                                              
  11                                               if   (k   >   0)
  12                                               {
  13                                                       tempStr   +=   "\t ";
  14                                               }


  15                                   tempStr   +=   dataGridview1.Rows[j].Cells         [k].Value.ToString();
  17                                       }
  18                                       sw.WriteLine(tempStr);
  19                             }
  20                               sw.Close();
  21                                 myStream.Close();
                        }
                    catch   (Exception   e)
                      {
                                MessageBox.Show(e.ToString());
                      }
                    finally
                      {
                              sw.Close();
                              myStream.Close();
                      }
                }

          }
}  

//抛出异常:System.NullReferenceException:Objec   reference   not   set   to   an   instance   of   an   object.
单步跟踪到1--18行,所有数据写入Execel文件后,不执行19,20行,却抛出以上异常。




[解决办法]
应该是你的哪个对象为空(null),你调试看看
[解决办法]
8 if( k == 0 )
{
tempStr += "\t ";
9 dataGridview1.Rows[j].Cells[k].Value = j + 1;
}

热点排行