zoukankan      html  css  js  c++  java
  • 【原】将datagridview里的数据导出到excel(二)

    哥本哈根
    private void ExportExcel(string fileName, DataGridView myDGV)
            {
    if (myDGV.Rows.Count > 0)
                {
                    SaveFileDialog saveFileDialog 
    = new SaveFileDialog();
                    saveFileDialog.DefaultExt 
    = "xls";
                    saveFileDialog.Filter 
    = "EXCEL文件(*.XLS)|*.xls";
                    saveFileDialog.FilterIndex 
    = 0;
                    saveFileDialog.FileName 
    =fileName;
                    saveFileDialog.RestoreDirectory 
    = true;
                    saveFileDialog.CreatePrompt 
    = true;
                    saveFileDialog.Title 
    = "导出到EXCEL";
                    saveFileDialog.ShowDialog();
                    
    if (saveFileDialog.FileName == "")
                        
    return;
                    Stream myStream;
                    myStream 
    = saveFileDialog.OpenFile();
                    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();
                            }
                            sw.WriteLine(tempStr);
                        }
                        sw.Close();
                        myStream.Close();
                    }
                    
    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    
    finally
                    {
                        sw.Close();
                        myStream.Close();
                    }
                }
    }
  • 相关阅读:
    初试PL/SQL并行编程
    SVN 让项目某些文件不受版本控制
    分析php获取客户端ip
    一道月薪3W的java面试题 (小明和小强都是张老师的学生,张老师的生日是某月某日,2人都不知道张老师的生日)
    js遍历对象的属性并且动态添加属性
    CodeForces 150B- Quantity of Strings 推算..
    linux 中多线程使用
    Microsoft Deployment Toolkit 2013 Preview Release Now Available
    谁有SMI-S Provider的一些源码,能参考一下吗
    hdu1395-2^x mod n = 1
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1736264.html
Copyright © 2011-2022 走看看