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();
                    }
                }
    }
  • 相关阅读:
    用python写一个魔塔50层怪物伤害计算器
    一个奇怪的方法解决华为ENSP模拟器路由器启动后命令行一直“#”的问题
    安卓数据库sqllite查看工具Android Debug Database使用教程
    GNS3错误’Could not start Telnet console with command 'Solar-PuTTY.exe‘
    解决从其他地方拷贝过来的Android项目在本机不能运行(报错)的问题
    python不换行输出
    哈希查找对比普通遍历查找所需时间
    第二个爬虫之爬取知乎用户回答和文章并将所有内容保存到txt文件中
    我的第一个爬虫之爬取搜狗壁纸并按分类存入本地文件夹
    What is Double 11 in China? Is it a famous festival?
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1736264.html
Copyright © 2011-2022 走看看