第一种办法:直接在button的Click事件中写代码进行导出:privatevoidbutton6_Click(objectsender,EventArgse)
{
SaveFileDialogsaveFileDialog=newSaveFileDialog();
saveFileDialog.Filter="Excelfiles(*.xls)|*.xls";
saveFileDialog.FilterIndex=0;
saveFileDialog.RestoreDirectory=true;
saveFileDialog.CreatePrompt=true;
saveFileDialog.Title="导出Excel文件到";
saveFileDialog.ShowDialog();
try
{
StreammyStream;
myStream=saveFileDialog.OpenFile();
StreamWritersw=newStreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312"));
stringstr="";
//写标题
for(inti=0;i
{
if(i>0)
{
str+="\t";
}
str+=dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for(intj=0;j
{
stringtempStr="";
for(intk=0;k
{
if(k>0)
{
tempStr+="\t";
}
tempStr+="'"+dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
MessageBox.Show("导出成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
sw.Close();
myStream.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
}
finally
{
//sw.Close();
//myStream.Close();
}
}
这种方法速度较快。
第二种办法:通过编写公共类,然后进行调用。
类usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Windows.Forms;
usingSystem.Reflection;
usingSystem.Runtime.InteropServices;
usingMicrosoft.Office.Interop.Excel;
namespaceWindowsFormsApplication3
{
classToExcel
{
publicstaticvoidDataGridViewToExcel(DataGridViewdgv,stringtitle)
{
introwCount=dgv.RowCount;
intcolumnCount=0;
foreach(DataGridViewColumndHeaderindgv.Columns)
{
if(dHeader.Visible==true)
columnCount++;
}
Microsoft.Office.Interop.Excel.Applicationexc=newMicrosoft.Office.Interop.Excel.Application();
if(exc==null)
{
thrownewException("Excel无法启动");
}
//
//
Workbooksworkbooks=exc.Workbooks;
//
_Workbookworkbook=workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//
Sheetssheets=exc.Sheets;
_Worksheetworksheet=(_Worksheet)sheets[1];
if(worksheet==null)
{
thrownewException("Worksheeterror");
}
Ranger=worksheet.get_Range(exc.Cells[1,1],exc.Cells[1,columnCount]);
exc.Visible=false;
r.MergeCells=true;
if(r==null)
{
MessageBox.Show("Range无法启动");
thrownewException("Rangeerror");
}
//以上是一些例行的初始化工作,下面进行具体的信息填充
//标题
exc.ActiveCell.FormulaR1C1=title;
exc.ActiveCell.Font.Size=12;
exc.ActiveCell.Font.Bold=true;
//列头
intColIndex=1;
foreach(DataGridViewColumndHeaderindgv.Columns)
{
if(dHeader.Visible==true)
worksheet.Cells[2,ColIndex++]=dHeader.HeaderText;
}
//填充单元格
ColIndex=0;
foreach(DataGridViewColumncolindgv.Columns)
{
if(col.Visible==true)
{
ColIndex++;
for(inti=0;i
{
if(dgv.Rows[i].Cells[col.Index].Value==null)
continue;
worksheet.Cells[i+3,ColIndex]="'"+dgv.Rows[i].Cells[col.Index].Value.ToString();
}
}
}
exc.Cells.EntireColumn.AutoFit();
exc.Cells.VerticalAlignment=Microsoft.Office.Interop.Excel.Constants.xlCenter;
exc.Cells.HorizontalAlignment=Microsoft.Office.Interop.Excel.Constants.xlCenter;
exc.Visible=true;
}
}
}
调用代码:privatevoidbutton4_Click(objectsender,EventArgse)
{
if(dataGridView1.RowCount==0)
{
return;
}
else
{
ToExcel.DataGridViewToExcel(dataGridView1,"学生表");
}
}
这种办法可以添加上表头
第三种办法:添加一个公共类,然后进行调用,这种调用会显示存储在哪个位置,然后进行保存。usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsFormsApplication3
{
classDataGridToExcel
{
publicstaticboolDataGridviewShowToExcel(DataGridViewdgv)
{
if(dgv.Rows.Count==0)
{
MessageBox.Show("数据源为空,导出无效");
returnfalse;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Applicationexcel=
newMicrosoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible=true;
//生成字段名称
for(inti=0;i
{
excel.Cells[1,i+1]=dgv.Columns[i].HeaderText;
}
//填充数据
for(inti=0;i
{
for(intj=0;j
{
excel.Cells[i+2,j+1]="'"+dgv[j,i].Value.ToString();
}
}
returntrue;
}
}
}
这种代码比较简单,导出的列默认都设置为了文本格式,避免转换保存出现数据错误。