把DataGridView的结果传入Excel
来源:互联网 发布:日本动漫推荐 知乎 编辑:程序博客网 时间:2024/05/16 03:31
#region Export the data in DataGridView into Excel
/// <summary>
/// Export the data in DataGridView into Excel
/// </summary>
/// <param name="gridView">DataGridView object</param>
/// <param name="isShowExcle">whether show the Excel interface</param>
/// <returns></returns>
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
//Build Excel Object.
try
{
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
Worksheet sheet = excel.ActiveSheet as Worksheet;
//Generate Report Title
Range range = null;
range = sheet.get_Range(excel.Cells[1, 1], excel.Cells[1, gridView.Columns.Count]);
range.MergeCells = true; // merge the cells
range.RowHeight = 20; // set row heigth
range.ColumnWidth = 20; // set coloum width
range.Font.Bold = true; // bold the font
range.Font.Size = 20; // set font size
range.Font.ColorIndex = 5; // set font color
range.HorizontalAlignment = XlHAlign.xlHAlignCenter; // set align center
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// set cell align vertial center
range.Value2 = "Asset Disposition Table";// set the value of cell
//Generate the column name of the gridview.
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[2, i + 1] = gridView.Columns[i].HeaderText;
}
//Fill the data from the gridview.
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 3, j + 1] = "'" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 3, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
}
catch (Exception ex)
{
return false;
MessageBox.Show("Please install the excel application first before you use export function", "Exception");
}
finally
{
excel.Quit();
excel = null;
}
}
#endregion
转载自:http://hi.baidu.com/%C4%AB%C1%E9%D2%C1%C8%BB/blog/item/f5bec154dcb4cd51574e002e.html
- 把DataGridView的结果传入Excel
- 要利用datagridview实现把SQL的查询结果导出Excel.cs
- 要利用datagridview实现把SQL的查询结果导出Excel.cs
- 把Excel导入DataGridView
- 把WinForm的DataGridView的数据导出到Excel
- 如何把DataGridView中的数据导入Excel
- 把datagridview的数据输出为Excel,Word的二种应用
- 把datagridview的数据输出为Excel,Word的二种应用
- 把datagridview的数据输出为Excel,Word的简单应用
- 关于把解析SQL文件和把解析的结果导入Excel中
- c#如何实现把datagridview里的数据导出到Excel里
- java 传入结果集,和一个Class,把结果集转化为List对象
- 把DatagridView中的数据导入到Excel中
- 把datagridview中的数据导入到excel中
- C#把DataGridVIew数据导出到Excel函数代码
- 把DatagridView中的数据导出到Excel中
- SQL中,把SQL查询分析器查询出来的结果,导出到EXCEL表格
- 关于POI把数据库结果集导出到EXCEL和WORD的方法
- 被感冒困扰 4.10
- ajax中URL中传递“#”丢失的解决办法
- 十个感悟人生的故事
- 软件包javax.servlet不存在 问题
- 技术专家谈SOA——转载
- 把DataGridView的结果传入Excel
- 爆笑:面试官与JAVA应聘者的对话
- 《荒漠甘泉》4月19日
- Unity学习笔记(一):StopLight QuickStart
- 重构笔记批注
- 职场人生:唐骏的职业生涯战术
- 经典电影台词
- 远程数据库备份还原
- c# 写ip 程序