从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数)。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Data;

namespace cnblogs

{

public static class LoadExcel

{

//从DataGridView中导出Excel,若s为null则直接导出DataGridView中的内容

public static void DataToExcel(DataGridView m_DataView,string s)

{

SaveFileDialog kk = new SaveFileDialog();

kk.Title = "保存EXECL文件";

kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";

kk.FilterIndex = 1;

if (kk.ShowDialog() == DialogResult.OK)

{

string FileName = kk.FileName;// +".xls";

if (File.Exists(FileName))

File.Delete(FileName);

FileStream objFileStream;

StreamWriter objStreamWriter;

string strLine = "";

objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);

objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);

objStreamWriter.WriteLine(s); //统计结果信息

for (int i = 0; i < m_DataView.Columns.Count; i++)

{

if (m_DataView.Columns[i].Visible == true)

{

strLine = strLine + m_DataView.Columns[i].HeaderText.ToString() + Convert.ToChar(9);

}

}

objStreamWriter.WriteLine(strLine);

strLine = "";

for (int i = 0; i < m_DataView.Rows.Count; i++)

{

if (m_DataView.Columns[0].Visible == true)

{

if (m_DataView.Rows[i].Cells[0].Value == null)

strLine = strLine + " " + Convert.ToChar(9);

else

strLine = strLine + m_DataView.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9);

}

for (int j = 1; j < m_DataView.Columns.Count; j++)

{

if (m_DataView.Columns[j].Visible == true)

{

if (m_DataView.Rows[i].Cells[j].Value == null)

strLine = strLine + " " + Convert.ToChar(9);

else

{

string rowstr = "";

rowstr = m_DataView.Rows[i].Cells[j].Value.ToString();

if (rowstr.IndexOf("\r\n") > 0)

rowstr = rowstr.Replace("\r\n", " ");

if (rowstr.IndexOf("\t") > 0)

rowstr = rowstr.Replace("\t", " ");

strLine = strLine + rowstr + Convert.ToChar(9);

}

}

}

objStreamWriter.WriteLine(strLine);

strLine = "";

}

objStreamWriter.Close();

objFileStream.Close();

MessageBox.Show("导出成功!");

}

}

//用DataGridView绑定的DataTable为参数,导出Excel

public static void DataToExcel(DataTable m_DataTable)

{

SaveFileDialog kk = new SaveFileDialog();

kk.Title = "保存EXECL文件";

kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";

kk.FilterIndex = 1;

if (kk.ShowDialog() == DialogResult.OK)

{

string FileName = kk.FileName + ".xls";

if (File.Exists(FileName))

File.Delete(FileName);

FileStream objFileStream;

StreamWriter objStreamWriter;

string strLine = "";

objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);

objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);

for (int i = 0; i < m_DataTable.Columns.Count; i++)

{

strLine = strLine + m_DataTable.Columns[i].Caption.ToString() + Convert.ToChar(9);

}

objStreamWriter.WriteLine(strLine);

strLine = "";

for (int i = 0; i < m_DataTable.Rows.Count; i++)

{

for (int j = 0; j < m_DataTable.Columns.Count; j++)

{

if (m_DataTable.Rows[i].ItemArray[j] == null)

strLine = strLine + " " + Convert.ToChar(9);

else

{

string rowstr = "";

rowstr = m_DataTable.Rows[i].ItemArray[j].ToString();

if (rowstr.IndexOf("\r\n") > 0)

rowstr = rowstr.Replace("\r\n", " ");

if (rowstr.IndexOf("\t") > 0)

rowstr = rowstr.Replace("\t", " ");

strLine = strLine + rowstr + Convert.ToChar(9);

}

}

objStreamWriter.WriteLine(strLine);

strLine = "";

}

objStreamWriter.Close();

objFileStream.Close();

}

}

}

}

从DataGridView导出Excel的更多相关文章

  1. c# datagridview导出Excel文件 问题

    今天vs2010c#开发做datagridview导出Excel文件时,发现一个问题,和大家探讨一下: 第一种方式:写流的方式 private void button_Excel_Click(obje ...

  2. 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)

    假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的 ...

  3. C# DataGridView 导出 Excel(根据Excel版本显示选择不同后缀格式xls或xlsx)

    /// <summary> /// DataGridView导出至Excel,解决问题:打开Excel文件格式与扩展名指定格式不一致 /// </summary> /// &l ...

  4. DataGridView 导出Excel (封装)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  5. DataGridView导出Excel

    将DataGridView里面的数据,导出到表格里面去. 首先,需要添加三个引用 直接在解决方案里,右键添加引用,找到路径即可.然后再把这三个文件复制到项目的根目录下. 然后定义导出表格的函数: pu ...

  6. C# DataGridView导出Excel

    using Microsoft.Office.Interop.Excel;                using Excel=Microsoft.Office.Interop.Excel; //这 ...

  7. winform datagridview 导出excel

    using System;using System.Collections.Generic;using System.Text;using System.IO;using Microsoft.Offi ...

  8. 在窗体中把DataGridView中的数据导出Excel

    //DataGridView导出Excel private void bt_Excl_Click(object sender, EventArgs e) { SaveFileDialog saveFi ...

  9. Winform 导出Excel

    private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e) { ) { var saveFileDialog1 = ...

随机推荐

  1. C# *= 运算顺序

    a *= a + b *c; 不管等号右边有没有括号,总是先算右边: 即等价于 a = a *(a + b*c); using System; using System.Collections.Gen ...

  2. C#学习链接

    .NET Framework — 针对 .NET 构建企业级搜索:https://msdn.microsoft.com/zh-cn/magazine/dn904674.aspx .net 动态编译:h ...

  3. 检查PHP文件中是否含有bom的PHP函数

    <?php /*检测并清除BOM*/ if(isset($_GET['dir'])){ $basedir=$_GET['dir']; }else{ $basedir = '.'; } $auto ...

  4. Go - 路径、目录名、包名、文件名

    先看一个示例: 1.目录结构 bin pkg src pk1 pk2 function1.go function2.go index.go 2.function1.go 文件内容: package p ...

  5. 为何Apache下.htaccess不起作用,Linux、Windows详解

    可能出现下面这三种的错误可能性: 第一种:启用 rewrite 和 .htaccess 设置 rewrite设置:找到apache的配置文件httpd.conf文件,找到:#LoadModule re ...

  6. 处理bin文件

    1. fs.Position  写入的位置,从哪个位置开始写 fs.Write(byte1,0,byte1.Length); byte1写入的byte[], 写入内容从第几位开始取,length取多长 ...

  7. 在VS中建立.aspx,.cs,.designer.cs之间的级联关系

    <Compile Include="..\Admin\Actions.aspx.cs"> <DependentUpon>Actions.aspx</D ...

  8. centos6.3安装python2.7, pip2.7, mysql

    参考: https://github.com/h2oai/h2o-2/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-ex ...

  9. input文本框设置和移除默认值

    input文本框设置和移除默认值 这里想实现的效果是:设置和移除文本框默认值,如下图鼠标放到文本框中的时候,灰字消失. 1.可以用简单的方式,就是给input文本框加上onfocus属性,如下代码: ...

  10. 阿里云部署Java web项目初体验(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在 ...