C#中DataGridView导出Excel的两种方法
第一种是用数据流导出:
#region
SaveFileDialogsaveFileDialog=newSaveFileDialog();
saveFileDialog.Filter="Execlfiles(*.xls)|*.xls";
saveFileDialog.FilterIndex=0;
saveFileDialog.RestoreDirectory=true;
saveFileDialog.CreatePrompt=true;
saveFileDialog.Title="ExportExcelFile";
saveFileDialog.ShowDialog();
if(saveFileDialog.FileName=="")
return;
StreammyStream;
myStream=saveFileDialog.OpenFile();
StreamWritersw=newStreamWriter(myStream,System.Text.Encoding.GetEncoding(-0));
stringstr="";
try
{
for(inti=0;i0)
{
str+="\t";
}
str+=dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
for(intj=0;j0)
{
tempStr+="\t";
}
tempStr+=dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
#endregion
这种方法的优点就是导出比较快,但是excel的表格里面设置标题,字体样式等都不能弄,因为你是用数据流直接导出为excel的,除非你能在数据流中设置这些样式,这个我还没这本事,哪位大哥会的教一下...嘿嘿
第二种方法是直接写一个类,这个类直接操作EXCEL的:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
usingSystem.Text;
usingSystem.Diagnostics;
usingSystem.IO;
usingMicrosoft.Office.Interop.Excel;
namespaceExcel
{
publicclassExport
{
///
///DataGridView导出Excel
///
///Excel文件中的标题
///DataGridView控件
///0:成功;1:DataGridView中无记录;2:Excel无法启动;9999:异常错误
publicintExportExcel(stringstrCaption,DataGridViewmyDGV,SaveFileDialogsaveFileDialog)
{
intresult=9999;
//保存
saveFileDialog.Filter="Execlfiles(*.xls)|*.xls";
saveFileDialog.FilterIndex=0;
saveFileDialog.RestoreDirectory=true;
//saveFileDialog.CreatePrompt=true;
saveFileDialog.Title="ExportExcelFile";
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
if(saveFileDialog.FileName=="")
{
MessageBox.Show("请输入保存文件名!");
saveFileDialog.ShowDialog();
}
//列索引,行索引,总列数,总行数
intColIndex=0;
intRowIndex=0;
intColCount=myDGV.ColumnCount;
intRowCount=myDGV.RowCount;
if(myDGV.RowCount==0)
{
result=1;
}
//创建Excel对象
Microsoft.Office.Interop.Excel.ApplicationxlApp=newMicrosoft.Office.Interop.Excel.ApplicationClass();
if(xlApp==null)
{
result=2;
}
try
{
//创建Excel工作薄
Microsoft.Office.Interop.Excel.WorkbookxlBook=xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.WorksheetxlSheet=(Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1];
//设置标题
Microsoft.Office.Interop.Excel.Rangerange=xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ColCount]);//标题所占的单元格数与DataGridView中的列数相同
range.MergeCells=true;
xlApp.ActiveCell.FormulaR1C1=strCaption;
xlApp.ActiveCell.Font.Size=20;
xlApp.ActiveCell.Font.Bold=true;
xlApp.ActiveCell.HorizontalAlignment=Microsoft.Office.Interop.Excel.Constants.xlCenter;
//创建缓存数据
object[,]objData=newobject[RowCount+1,ColCount];
//获取列标题
foreach(DataGridViewColumncolinmyDGV.Columns)
{
objData[RowIndex,ColIndex++]=col.HeaderText;
}
//获取数据
for(RowIndex=1;RowIndex
这个优点是能设置样式什么的。缺点就是导出速度慢...
以上两种方法都是参考了很多料的..写在这里以便于相互学习..
补充一下:用第二种方法导出excel会有格式方面的变化,比如身份证号码按科学计算法导出了,不是按原先的模型
改进方法是在写入excel时添加一个格式声明:range.NumberFormatLocal="@";
如://写入Excel
range=xlSheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount+2,ColCount]);
range.NumberFormatLocal="@";
range.Value2=objData;
到此这篇关于C#中DataGridView导出Excel的两种方法的文章就介绍到这了,更多相关C#DataGridView导出Excel内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!