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();
                    }
                }
    }
  • 相关阅读:
    测试工作中需要用到的一些linux命令
    软件的性能测试到底是测试哪些方面
    软件测试中的安全测试包括哪些方面
    git一不小心上传了大文件,怎么破?
    接口测试校验返回数据格式,JasonSchema使用详解
    selenium定位中需要鼠标悬浮才能显示的按钮
    jenknis参数化构建,windows和shell引用变量
    selenium元素定位之父子节点、胞节点
    selenium元素定位之多个元素中选择其中的一个
    Python实现注册和三次验证登录
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1736264.html
Copyright © 2011-2022 走看看