DATATABLE,DATAGRIDVIEW导入EXCEL中
来源:互联网 发布:韶关市仁化县网络问政 编辑:程序博客网 时间:2024/06/06 02:49
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using System.Data;
using Microsoft;
namespace Recon.Log
{
class GridViewToExcel
{
/// <summary>
/// 把一个内在表的数据导入到EXCEL中去
/// </summary>
/// <param name="ReportTitle"></param>
/// <param name="myTable"></param>
public static void ExportDataGridToExcel(string ReportTitle, DataTable myTable)
{
try
{
Excel.Application xlApp = new Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
if (myTable.Rows.Count > 0)
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
foreach (DataColumn colu in myTable.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myTable.Rows[row][col].ToString().Trim();
}
}
}
else
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
//将表中的栏位名称填到Excel的第一行
foreach (DataColumn Col in myTable.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myTable.Rows[row][col].ToString().Trim();
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).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;
xlApp.Visible = true;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 把DataGridView中的数据导入到EXCEL中,如果DataGridView具有分页的话,只能导入当前页面记录到期 EXCEL中
/// </summary>
/// <param name="grid"></param>
/// <param name="ReportTitle"></param>
/// <param name="myTable"></param>
public static void ExportDataGridToExcel(DataGridView grid, string ReportTitle, DataTable myTable)
{
try
{
Excel.Application xlApp = new Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
if (grid.Rows.Count > 0)
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, grid.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
foreach (DataGridViewColumn colu in grid.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.HeaderText;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < grid.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = grid.Rows[row].Cells[col].Value.ToString().Trim();
}
}
}
else
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
//将表中的栏位名称填到Excel的第一行
foreach (DataColumn Col in myTable.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = grid.Rows[row].Cells[col].Value.ToString().Trim();
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).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;
xlApp.Visible = true;
}
catch (Exception e)
{
throw e;
}
}
}
}
- DATATABLE,DATAGRIDVIEW导入EXCEL中
- 提升 dataGridView 效率,让万笔数据绑定 datatable (dataGridView 中快速导入 datatable)
- 将Excel的数据导入DataGridView中
- 将DataGridView中的数据导入Excel中
- 将excel中的数据导入datagridview中
- datagridview中的数据导入到excel中
- WinForm中DataGridView数据导入Excel
- 将Excel导入到虚拟DataTable中
- DataTable不要导入重复项,datagridview中设定
- dataGridView直接绑定DataRow数组(导入datatable中)
- C#中 dataGridView和DataTable 导出Excel的方法
- 将DataGridView中的数据导入Excel中,并显示Excel
- c#excel导入datagridview
- Excel导入datagridview
- Excel导入到DataGridView
- Excel导入到DataGridView
- C#-DataGridView导入Excel
- datagridview数据导入Excel
- C#如何获取本地计算机的状态
- Dojo学习笔记
- SQLAGENT无法启动--BUILTIN/Administrators账号问题
- linux source
- 盖茨在微软的最后一天搞笑片
- DATATABLE,DATAGRIDVIEW导入EXCEL中
- (转)数组名与指针问题
- Leo 的内核精简笔记(2) 进程模型
- JAVA,OpenLDAP使用心得(1)
- 在visual studio 2003工程中增加宏定义的方法
- J2ME网络编程(获得Web中的文字,图片,多媒体,与JSP交互)以及网络游戏的实现
- 服装ERP应用(三):用ERP帮助实施代理商“削蕃计划”
- ASP.NET GridView 有多強? 範例下載位置
- .NET REMOTING 技朮(值得用這一 技朮)