zoukankan      html  css  js  c++  java
  • Winform快速导出

    public static void ExportExcel(DataGridView DataGridView01)
    {
    Stream stream = null;
    StreamWriter writer = null;
    SaveFileDialog dialog = new SaveFileDialog {
    Filter = "Execl files (*.xls)|*.xls",
    FilterIndex = 0,
    RestoreDirectory = true,
    CreatePrompt = true,
    Title = "Export Excel File To"
    };
    if (DialogResult.OK == dialog.ShowDialog())
    {
    try
    {
    stream = dialog.OpenFile();
    writer = new StreamWriter(stream, Encoding.GetEncoding(0));
    string str = "";
    for (int i = 0; i < DataGridView01.ColumnCount; i++)
    {
    if (i > 0)
    {
    str = str + " ";
    }
    str = str + DataGridView01.Columns[i].HeaderText;
    }
    writer.WriteLine(str);
    int columnCount = DataGridView01.ColumnCount;
    for (int j = 0; j < DataGridView01.Rows.Count; j++)
    {
    string str2 = "";
    for (int k = 0; k < columnCount; k++)
    {
    if (k > 0)
    {
    str2 = str2 + " ";
    }
    if (k > 0x16)
    {
    }
    if (DataGridView01.Rows[j].Cells[k].Value == null)
    {
    str2 = str2 ?? "";
    }
    else
    {
    str2 = str2 + DataGridView01.Rows[j].Cells[k].Value.ToString();
    }
    }
    writer.WriteLine(str2);
    }
    writer.Close();
    stream.Close();
    }
    catch (Exception exception)
    {
    MessageBox.Show("导出文件时出错,文件可能正被打开! " + exception.Message);
    }
    finally
    {
    writer.Close();
    stream.Close();
    }
    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
    }
    }

     //

    如果 ?? 运算符的左操作数非 null,该运算符将返回左操作数,否则返回右操作数。
  • 相关阅读:
    轮播图2
    点击按钮切换轮播图
    轮播图
    2016.5.5_十进制转二进制【ABAP】
    2016.4.26_longtext长文本【ABAP】
    2016.4.26_动态内表【ABAP】
    2016.4.26_下载abap代码【ABAP】
    2016.4.15_debug小技巧【ABAP】
    2016.4.1_js向controller传数据【笔记】
    2016.3.21_TABLE CONTROL【ABAP】
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/3738626.html
Copyright © 2011-2022 走看看