做数据库处理的时候,经常需要将数据导出,一般都是导出到EXCEL,网上很多方法都是用EXCEL组件,自己感觉效率比较低,于是重新用流处理的方式导出数据,在数据量大的情况下,速度不知道快了多少,非常快,而且导出格式可以是EXCEL,WORD,TXT等,自由设定,不多说,贴出代码,直接传入对应参数即可!
//
类名:EcanOutPutData
// 作用:导出数据(二进制流的形式)
// 作者:刘典武
// 时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data ;
namespace Ecan
{
public class EcanOutPutData
{
/// <summary>
/// 从listbox中导出数据
/// </summary>
/// <param name="lbox"> listbox控件 </param>
/// <param name="txtTitle"> 导出数据的标题 </param>
/// <param name="filter"> 导出数据的格式(拓展名) </param>
public void outPutListBoxData(ListBox lbox, string txtTitle, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写标题
sw.WriteLine(txtTitle);
// 循环写内容
for ( int i = 0 ; i < lbox.Items.Count; i ++ )
{
string tempStr = "" ;
tempStr += lbox.Items[i].ToString();
tempStr += " \t " ;
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
/// <summary>
/// 导出DataSet数据
/// </summary>
/// <param name="ds"> 数据源 </param>
/// <param name="txtTitle"> 导出数据的标题 </param>
/// <param name="filter"> 导出数据的格式 </param>
public void outPutDataSet(DataSet ds, string txtTitle, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写标题
sw.WriteLine(txtTitle);
// 写数据字段
string tempTitle = "" ;
for ( int i = 0 ; i < ds.Tables[ 0 ].Columns.Count;i ++ )
{
if (i > 0 )
{
tempTitle += " \t " ;
}
tempTitle += ds.Tables[ 0 ].Columns[i].ColumnName;
}
sw.WriteLine(tempTitle);
// 循环写内容
for ( int j = 0 ; j < ds.Tables [ 0 ].Rows .Count ; j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < ds.Tables[ 0 ].Columns.Count; k ++ )
{
if (k > 0 )
{ tempStr += " \t " ; }
tempStr += ds.Tables[ 0 ].Rows[j][k].ToString();
}
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
/// <summary>
/// 导出DataGridView数据
/// </summary>
/// <param name="dgv"> DataGridView控件 </param>
/// <param name="txtTitle"> 导出数据标题 </param>
/// <param name="filter"> 导出数据格式 </param>
public void outPutDataGridViewData(DataGridView dgv, string txtTitle, string filter)
{
DataSet myds = (DataSet ) dgv.DataSource;
this .outPutDataSet(myds, txtTitle, filter);
}
/// <summary>
/// 导出DataGridView数据
/// </summary>
/// <param name="dgv"> DataGridView控件 </param>
/// <param name="filter"> 导出数据格式 </param>
public void outPutDataGridViewData(DataGridView dgv, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写数据字段
string tempTitle = "" ;
for ( int i = 0 ; i < dgv.Columns .Count ; i ++ )
{
if (i > 0 )
{
tempTitle += " \t " ;
}
tempTitle += dgv.Columns[i].Name;
// tempTitle += ds.Tables[0].Columns[i].ColumnName;
}
sw.WriteLine(tempTitle);
// 循环写内容
for ( int j = 0 ; j < dgv.Rows .Count ;j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < dgv.Columns.Count; k ++ )
{
if (k > 0 )
{ tempStr += " \t " ; }
tempStr += dgv.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
}
}
// 作用:导出数据(二进制流的形式)
// 作者:刘典武
// 时间:2010-12-01
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data ;
namespace Ecan
{
public class EcanOutPutData
{
/// <summary>
/// 从listbox中导出数据
/// </summary>
/// <param name="lbox"> listbox控件 </param>
/// <param name="txtTitle"> 导出数据的标题 </param>
/// <param name="filter"> 导出数据的格式(拓展名) </param>
public void outPutListBoxData(ListBox lbox, string txtTitle, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写标题
sw.WriteLine(txtTitle);
// 循环写内容
for ( int i = 0 ; i < lbox.Items.Count; i ++ )
{
string tempStr = "" ;
tempStr += lbox.Items[i].ToString();
tempStr += " \t " ;
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
/// <summary>
/// 导出DataSet数据
/// </summary>
/// <param name="ds"> 数据源 </param>
/// <param name="txtTitle"> 导出数据的标题 </param>
/// <param name="filter"> 导出数据的格式 </param>
public void outPutDataSet(DataSet ds, string txtTitle, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写标题
sw.WriteLine(txtTitle);
// 写数据字段
string tempTitle = "" ;
for ( int i = 0 ; i < ds.Tables[ 0 ].Columns.Count;i ++ )
{
if (i > 0 )
{
tempTitle += " \t " ;
}
tempTitle += ds.Tables[ 0 ].Columns[i].ColumnName;
}
sw.WriteLine(tempTitle);
// 循环写内容
for ( int j = 0 ; j < ds.Tables [ 0 ].Rows .Count ; j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < ds.Tables[ 0 ].Columns.Count; k ++ )
{
if (k > 0 )
{ tempStr += " \t " ; }
tempStr += ds.Tables[ 0 ].Rows[j][k].ToString();
}
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
/// <summary>
/// 导出DataGridView数据
/// </summary>
/// <param name="dgv"> DataGridView控件 </param>
/// <param name="txtTitle"> 导出数据标题 </param>
/// <param name="filter"> 导出数据格式 </param>
public void outPutDataGridViewData(DataGridView dgv, string txtTitle, string filter)
{
DataSet myds = (DataSet ) dgv.DataSource;
this .outPutDataSet(myds, txtTitle, filter);
}
/// <summary>
/// 导出DataGridView数据
/// </summary>
/// <param name="dgv"> DataGridView控件 </param>
/// <param name="filter"> 导出数据格式 </param>
public void outPutDataGridViewData(DataGridView dgv, string filter)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = " user files( " + filter + " )| " + filter;
save.Title = " 导出文件到 " ;
if (save.ShowDialog() == DialogResult.OK)
{
Stream myStream = save.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " GB2312 " ));
try
{
// 写数据字段
string tempTitle = "" ;
for ( int i = 0 ; i < dgv.Columns .Count ; i ++ )
{
if (i > 0 )
{
tempTitle += " \t " ;
}
tempTitle += dgv.Columns[i].Name;
// tempTitle += ds.Tables[0].Columns[i].ColumnName;
}
sw.WriteLine(tempTitle);
// 循环写内容
for ( int j = 0 ; j < dgv.Rows .Count ;j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < dgv.Columns.Count; k ++ )
{
if (k > 0 )
{ tempStr += " \t " ; }
tempStr += dgv.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
MessageBox.Show( " 导出数据成功! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
sw.Close();
myStream.Close();
}
}
}
}
}
一个C#资源分享平台,专业分享学习高质量代码,每周期布置学习任务,激发学习C#兴趣!(QQ群:128874886)