从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# 使用Silverlight toolkit Chart

    一.基础介绍 Silverlight ToolKit是微软发布的基于Microsoft-Public License(MS-PL)许可协议的控件集.MS-PL许可协议允许商业或非商业的发布,所以我们可 ...

  2. jQuery radio取值,checkbox取值,select取值

    语法解释: $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkTex ...

  3. 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值

    这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...

  4. html嵌入样式表

    1.针对文件中的字体还有属性进行设置主要设置文字的大小及其颜色问题,未涉及div飘操作 处理页面CSS 先检测该内容部分是否已经设定了样式,如果没有单独设定再按照总体设计进行限定. eg:  h1 h ...

  5. and or bool and a or b 原理解释

    python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值. or也是从左到有计算表达式,返回第一个为真的值. 代码如下: IDLE 1.2.4>&g ...

  6. highcharts的表名

    line:直线图 spline:曲线图 area:面积图 areaspline:曲线面积图 arearange:面积范围图 areasplinerange:曲线面积范围图 column:柱状图 col ...

  7. sql server 2008 数据的行转列

    create table tb(id int, value varchar(10)) insert into tb values(1,'aa') insert into tb values(1,'bb ...

  8. NEFU 558 迷宫寻路

    题目链接 简单搜索题 #include <cstdio> #include <iostream> #include <cstring> using namespac ...

  9. MySql 连接字符串

    一.MySQL Connector/ODBC 2.50 (MyODBC 2.50)连接方式 1.本地数据库连接Driver={MySQL};Server=localhost;Option=16834; ...

  10. WINFORM中的COMBOX模糊查询

    有的时候下拉框中的元素过多不好查询,可以考虑进行模糊过滤查询. 在类文件的designer.cs中找到定义combox的模块,加入以下两行代码即可: this.combox.AutoCompleteM ...