DataGridView直接导出EXCEL

 1public void DataToExcel(DataGridView m_DataView)
 2        {
 3            SaveFileDialog kk = new SaveFileDialog();
 4            kk.Title = "保存EXECL文件";
 5            kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
 6            kk.FilterIndex = 1;
 7            if (kk.ShowDialog() == DialogResult.OK)
 8            {
 9                string FileName = kk.FileName + ".xls";
10                if (File.Exists(FileName))
11                    File.Delete(FileName);
12                FileStream objFileStream;
13                StreamWriter objStreamWriter;
14                string strLine = "";
15                objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
16                objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
17                for (int i = 0; i  < m_DataView.Columns.Count; i++)
18                {
19                    if (m_DataView.Columns[i].Visible == true)
20                    {
21                        strLine = strLine + m_DataView.Columns[i].HeaderText.ToString() + Convert.ToChar(9);
22                    }
23                }
24                objStreamWriter.WriteLine(strLine);
25                strLine = "";
26
27                for (int i = 0; i  < m_DataView.Rows.Count; i++)
28                {
29                    if (m_DataView.Columns[0].Visible == true)
30                    {
31                        if (m_DataView.Rows[i].Cells[0].Value == null)
32                            strLine = strLine + " " + Convert.ToChar(9);
33                        else
34                            strLine = strLine + m_DataView.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9);
35                    }
36                    for (int j = 1; j  < m_DataView.Columns.Count; j++)
37                    {
38                        if (m_DataView.Columns[j].Visible == true)
39                        {
40                            if (m_DataView.Rows[i].Cells[j].Value == null)
41                                strLine = strLine + " " + Convert.ToChar(9);
42                            else
43                            {
44                                string rowstr = "";
45                                rowstr = m_DataView.Rows[i].Cells[j].Value.ToString();
46                                if (rowstr.IndexOf("\r\n") >  0)
47                                    rowstr = rowstr.Replace("\r\n", " ");
48                                if (rowstr.IndexOf("\t") >  0)
49                                    rowstr = rowstr.Replace("\t", " ");
50                                strLine = strLine + rowstr + Convert.ToChar(9);
51                            }
52                        }
53                    }
54                    objStreamWriter.WriteLine(strLine);
55                    strLine = "";
56                }
57                objStreamWriter.Close();
58                objFileStream.Close();
59                MessageBox.Show(this,"保存EXCEL成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
60            }
61        }

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

 1public void DataToExcel(DataTable m_DataTable)
 2        {
 3            SaveFileDialog kk = new SaveFileDialog();
 4            kk.Title = "保存EXECL文件";
 5            kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
 6            kk.FilterIndex = 1;
 7            if (kk.ShowDialog() == DialogResult.OK)
 8            {
 9                string FileName = kk.FileName + ".xls";
10                if (File.Exists(FileName))
11                    File.Delete(FileName);
12                FileStream objFileStream;
13                StreamWriter objStreamWriter;
14                string strLine = "";
15                objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
16                objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
17                for (int i = 0; i < m_DataTable.Columns.Count; i++)
18                {
19                    strLine = strLine + m_DataTable.Columns[i].Caption.ToString() + Convert.ToChar(9);
20                }
21                objStreamWriter.WriteLine(strLine);
22                strLine = "";
23
24                for (int i = 0; i < m_DataTable.Rows.Count; i++)
25                {
26                    for (int j = 0; j < m_DataTable.Columns.Count; j++)
27                    {
28                        if (m_DataTable.Rows[i].ItemArray[j] == null)
29                            strLine = strLine + " " + Convert.ToChar(9);
30                        else
31                        {
32                            string rowstr = "";
33                            rowstr = m_DataTable.Rows[i].ItemArray[j].ToString();
34                            if (rowstr.IndexOf("\r\n") > 0)
35                                rowstr = rowstr.Replace("\r\n", " ");
36                            if (rowstr.IndexOf("\t") > 0)
37                                rowstr = rowstr.Replace("\t", " ");
38                            strLine = strLine + rowstr + Convert.ToChar(9);
39                        }
40                    }
41                    objStreamWriter.WriteLine(strLine);
42                    strLine = "";
43                }
44                objStreamWriter.Close();
45                objFileStream.Close();
46                MessageBox.Show(this, "保存EXCEL成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
47            }
48        }
时间: 2024-11-01 18:51:14

DataGridView直接导出EXCEL的相关文章

StreamWriter(datagridview)导出EXCEL

public void WriteExcel(DataGridView ds, string path) { try { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveF

vb.net datagridview控件导出excel

导出数据的时候,经常需要将软件中的表格导出为excel,学习vb的时候用到了这个功能,当时用的是面向过程的编程方法,哪里需要了,就在哪个窗体写一遍代码.这次,我们使用面向对象的思维再次实现这个功能.对比一下,两个方法有什么区别. 这里使用vb.net作为载体,和面向过程一样,第一步还是要添加引用.为了增加复用性,需要把这个功能做成一个模块,因为是导出数据,所以,把它放到U层,需要在U层添加引用.如图: 将图中画红框的选中就可以了. 然后在U层添加模块,模块代码如下: Imports Micros

DataGridView如何快速导出Excel

从DataGridView或DataTable导出Excel文件,为了按照数据类型设置单元格格式,导出Excel时速度都比较慢,一直找不到好的办法. 最后从外文网站上找到解决办法,使用ws.get_Range(excelRange, Type.Missing).Value2方法批量设置单元格的值.详细代码如下: public void DataToExcelFast(DataGridView dgv, string fName) { try { Excel.Application excel =

C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(object sender, EventArgs e) { OpenFileDialog fd = new OpenFileDialog();//首先根据打开文件对话框,选择excel表格 ofd.Filter = "表格|*.xls";//打开文件对话框筛选器 string strPath;/

Winform DataGridView控件数据导出EXcel

/// <summary> /// 导出Excel /// </summary> /// <param name="dgv"></param> /// <param name="tempProgressBar"></param> /// <param name="toolstrip"></param> public void DataToExcel(D

NPOI DataGridView导出EXCEL

NPOI 官方网站 http://npoi.codeplex.com/ 加载NPOI.DLL ,引用这两个命名空间 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; #region NPOI DataGridView 导出 EXCEL /// <summary> /// NPOI DataGridView 导出 EXCEL /// </summary> /// <param name="fileName"

C#winform中datagridview导出Excel

只需要传入datagridview的name即可. 1 //导出Excel()方法 2 public void ToExcel(DataGridView dataGridView) 3 { 4 //实例化一个Excel.Application对象 5 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 6 7 if (excel == null)

从DataGridView导出Excel

从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 { public sta

Winform将DataGridView导出Excel,调用打印机打印DataGridView

DataGridView导出Excel (这个需要 Microsoft.Office.Interop.Excel.dll  在网上下载就可以) public static void ExportExcel(string fileName, DataGridView myDGV) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = &qu