- 爱易网页
 
                        - 
                            C#教程
 
                        - 请问高人难题!C# 导出 Excel ,怎么单元格 设置 格式(以数据流的形式写出Excel)急 
 
                         
                    
                    
                    日期:2014-05-18  浏览次数:21167 次 
                    
                        
                         请教高人难题!C# 导出 Excel ,如何单元格 设置 格式(以数据流的形式写出Excel)急!!
将 DataGridView 数据 导出 Excel (以数据流的形式写出Excel)
但是文本类型的数字 导出 却是 数值型了,身份证、卡号等就是错误的了,因为数据太长。
请教高人,有什么好方法解决这个问题,着急!!!以下是代码:
public static void ExportDataGridViewToExcel(DataGridView MyDataGridView)
         {
             SaveFileDialog saveFileDialog = new SaveFileDialog();
             saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
             saveFileDialog.FilterIndex = 0;
             saveFileDialog.RestoreDirectory = true;
             saveFileDialog.CreatePrompt = true;
             saveFileDialog.Title = "导出Excel文件到";
             if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
             {
                 Stream myStream;
                 myStream = saveFileDialog.OpenFile();
                 StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                 string str = "";
                 try
                 {
                     //写标题  
                     for (int i = 0; i < MyDataGridView.ColumnCount; i++)
                     {
                         if (MyDataGridView.Columns[i].Visible == true)
                         {
                             if (i > 0)
                             {
                                 str += "\t";
                             }
                             str += MyDataGridView.Columns[i].HeaderText;
                         }
                     }
                     sw.WriteLine(str);
                     //写内容
                     for (int j = 0; j < MyDataGridView.Rows.Count; j++)
                     {
                         string tempStr = "";
                         for (int k = 0; k < MyDataGridView.Columns.Count; k++)
                         {
                             if (MyDataGridView.Columns[k].Visible == true)
                             {
                                 if (k > 0)
                                 {
                                     tempStr += "\t";
                                 }
                                 tempStr +=MyDataGridView.Rows[j].Cells[k].Value.ToString();
                             }
                         }
                         sw.WriteLine(tempStr);
                     }
                     sw.Close();
                     myStream.Close();
                 }
                 catch (Exception e)
                 {
                     MessageBox.Show(e.ToString());
                 }
                 finally
                 {
                     sw.Close();
                     myStream.Close();
                 }
             }
         }
------解决方案--------------------
      public void ExportExcel(DataSet tempds, string saveFileName)
       {
           if (tempds == null)
           {
               MessageBox.Show("要导出的数据为空!!!");
               this.Close();
               return;
           }
           Excel.Application xlApp = new Excel.Application();
           if (xlApp == null)
           {
               MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
               this.Close();
               return;
           }
           try