using System;
using System.Data;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable exporeDataToTable(DataGridView dataGridView)
{
//将datagridview中的数据导入到表中
DataTable tempTable = new DataTable("tempTable");
//定义一个模板表,专门用来获取列名
DataTable modelTable = new DataTable("ModelTable");
//创建列
for (int column = 0; column < dataGridView1.Columns.Count; column++)
{
//可见的列才显示出来
if (dataGridView1.Columns[column].Visible == true)
{
DataColumn tempColumn = new DataColumn(dataGridView1.Columns[column].HeaderText, typeof(string));
tempTable.Columns.Add(tempColumn);
DataColumn modelColumn = new DataColumn(dataGridView1.Columns[column].Name, typeof(string));
modelTable.Columns.Add(modelColumn);
}
}
//添加datagridview中行的数据到表
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
if (dataGridView1.Rows[row].Visible == false)
{
continue;
}
DataRow tempRow = tempTable.NewRow();
for (int i = 0; i < tempTable.Columns.Count; i++)
{
tempRow[i] = dataGridView1.Rows[row].Cells[modelTable.Columns[i].ColumnName].Value;
}
tempTable.Rows.Add(tempRow);
}
return tempTable;
}
private void OutputAsExcelFile(DataGridView dataGridView)
{
//将datagridView中的数据导出到一张表中
DataTable tempTable = this.exporeDataToTable(dataGridView);
//导出信息到Excel表
Microsoft.Office.Interop.Excel.ApplicationClass myExcel;
Microsoft.Office.Interop.Excel.Workbooks myWorkBooks;
Microsoft.Office.Interop.Excel.Workbook myWorkBook;
Microsoft.Office.Interop.Excel.Worksheet myWorkSheet;
char myColumns;
Microsoft.Office.Interop.Excel.Range myRange;
object[,] myData = new object[500, 35];
int i, j;//j代表行,i代表列
myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
//显示EXCEL
myExcel.Visible = true;
if (myExcel == null)
{
MessageBox.Show("本地Excel程序无法启动!请检查您的Microsoft Office正确安装并能正常使用", "提示");
return;
}
myWorkBooks = myExcel.Workbooks;
myWorkBook = myWorkBooks.Add(System.Reflection.Missing.Value);
myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets[1];
myColumns = (char)(tempTable.Columns.Count + 64);//设置列
myRange = myWorkSheet.get_Range("A1", myColumns.ToString() + "5");//设置列宽
int count = 0;
//设置列名
foreach (DataColumn myNewColumn in tempTable.Columns)
{
myData[0, count] = myNewColumn.ColumnName;
count = count + 1;
}
//输出datagridview中的数据记录并放在一个二维数组中
j = 1;
foreach (DataRow myRow in tempTable.Rows)//循环行
{
for (i = 0; i < tempTable.Columns.Count; i++)//循环列
{
myData[j, i] = myRow[i].ToString();
}
j++;
}
//将二维数组中的数据写到Excel中
myRange = myRange.get_Resize(tempTable.Rows.Count + 1, tempTable.Columns.Count);//创建列和行
myRange.Value2 = myData;
myRange.EntireColumn.AutoFit();
}
private void button1_Click(object sender, EventArgs e)
{
///
/// 临时在dataGridView1添加50行数据,用来测试,没什么实际意义。
///
for (int i = 1; i < 50; i++)
{
dataGridView1.Rows.Add("qwq", "qwq", "rwrwr", "sfksfa");//加入要显示的内容,第一列qwq,第二列qwq,第三列rwrwr,第四列sfksfa
}
exporeDataToTable(dataGridView1);//将datagridview中的数据导入到表中
OutputAsExcelFile(dataGridView1);//将datagridView中的数据导出到一张表中
}
}
}
需要添加引用:
你这不是将dataGridView1中一键给全导出了吗?
如果是选中整行的,这样就好办,你选中的行有标志,在table添加行时做个判断,选中的行才添加进去就行了其他不用变。
比如: foreach (DataGridViewRow row in dataGridView1.SelectedRows),随便你用什么方法,大概就这个意思