| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1556 人关注过本帖, 1 人收藏
标题:[转载]分享一个用于将DataGridView中的数据输出到Excel或Text中的类
只看楼主 加入收藏
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏(1)
 问题点数:0 回复次数:4 
[转载]分享一个用于将DataGridView中的数据输出到Excel或Text中的类

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace BusinessRuler

{

public class ExportExcel

{

public static void DoForExcel(DataGridView dgv, string reportTitle)

{

Excel.Application xlApp = new Excel.ApplicationClass();

if (xlApp == null)

{

MessageBox.Show("Excel无法启动", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

int rowIndex = 2;

int colIndex = 0;

Excel.Workbook xlBook = xlApp.Workbooks.Add(true);

Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dgv.ColumnCount]);

range.MergeCells = true;

xlApp.ActiveCell.FormulaR1C1 = reportTitle;

xlApp.ActiveCell.Font.Size = 18;

xlApp.ActiveCell.Font.Bold = true;

foreach (DataGridViewColumn column in dgv.Columns)

{

colIndex = colIndex + 1;

xlApp.Cells[2, colIndex] = column.HeaderText;

}

for (int row = 0; row < dgv.Rows.Count; row++)

{

rowIndex = rowIndex + 1;

for (int col = 0; col < dgv.Columns.Count; col++)

{

xlApp.Cells[rowIndex, col + 1] = dgv.Rows[row].Cells[col].Value.ToString();

}

}

xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, dgv.Columns.Count]).Font.Bold = true;

xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;

xlApp.Cells.EntireColumn.AutoFit();

xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;

xlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;

try

{

xlApp.Save(System.DateTime.Now.Millisecond.ToString());

}

catch

{

}

finally

{

xlApp.Quit();

}

}

public static void DoForText(DataGridView dgv,string reportTitle)

{

SaveFileDialog dlg = new SaveFileDialog();

dlg.Title = "输出报表";

dlg.Filter = "文本文件(*.txt)|*.txt";

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

{

//遍历求出各列内容的最大长度,以便按格式对齐

int[] colContentLength = new int[dgv.ColumnCount];

for (int row = 0; row < dgv.Rows.Count; row++)

{

for (int col = 0; col < dgv.ColumnCount; col++)

{

if (dgv.Rows[row].Cells[col].Value.ToString().Length > colContentLength[col])

{

colContentLength[col] = dgv.Rows[row].Cells[col].Value.ToString().Length;

}

}

}

string fileName = dlg.FileName;

FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);

StreamWriter sw = new StreamWriter(fs);

//通过流来写文件

try

{

sw.WriteLine(reportTitle);

sw.WriteLine();

sw.WriteLine("----------------------------------------------------------");

//写列名

int position = 0;

foreach (DataGridViewColumn column in dgv.Columns)

{

sw.Write(column.HeaderText.PadRight(colContentLength[position++] + 4));

}

//写内容

for (int row = 0; row < dgv.Rows.Count; row++)

{

sw.WriteLine();

for (int col = 0; col < dgv.ColumnCount; col++)

{

sw.Write(dgv.Rows[row].Cells[col].Value.ToString().PadRight(colContentLength[col] + 8));

}

}

sw.WriteLine();

sw.WriteLine("----------------------------------------------------------");

sw.Flush();

}

catch

{

MessageBox.Show("导出文件出错,请重试!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

finally

{

sw.Close();

fs.Close();

}

}

}

}

}

搜索更多相关主题的帖子: Excel DataGridView Text 数据 输出 
2007-03-10 20:53
cqlmp
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2007-1-29
收藏
得分:0 
具体是怎么用的啊,版主能说明下简单的使用步奏吗?
2007-03-10 22:53
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
以下是引用cqlmp在2007-3-10 22:53:18的发言:
具体是怎么用的啊,版主能说明下简单的使用步奏吗?

这是一个完整的类呀, 其中包含两具静态的方法, 直接调用就行的.

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-03-19 00:49
zsh_vbnet
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-4-15
收藏
得分:0 
谢谢了。
2007-04-16 10:50
rebellikun
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-6-26
收藏
得分:0 
谢谢
2007-05-05 10:59
快速回复:[转载]分享一个用于将DataGridView中的数据输出到Excel或Text中的类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019459 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved