win8开机密码怎么取消-消灭星星电脑版
![gridview导出excel](/uploads/image/0237.jpg)
2023年4月3日发(作者:siri怎么打开)
原文地址:转:winform中将datagirdview中数据导出到EXCEL中作者:雪枫
如何使用c#将Winform下DataGridView中内容导出到Excel?
方法1:
添加dll引用
右击选择你所在的项目的“引用”,选择“添加引用”。
弹出“添加引用”对话框。
选择“COM”选项卡。
选择“MicrosoftExcel12.0ObjectLibrary”
单击“确定”按钮。
说明:
如何发现导入的程序集不能正常工作,可能是由于office安装的缘故,请确保自定义安装office时,选择“.net可编程性支持”:
代码:
publicstaticboolExportForDataGridview(DataGridViewgridView,stringfileName,boolisShowExcle)
{
//建立Excel对象
ationapp=ation();
try
{
if(app==null)
{
returnfalse;
}
e=isShowExcle;
Workbooksworkbooks=oks;
_Workbookworkbook=(Worksheet);
Sheetssheets=eets;
_Worksheetworksheet=(_Worksheet)_Item(1);
if(worksheet==null)
{
returnfalse;
}
stringsLen="";
//取得最后一列列名
charH=(char)(64+Count/26);
charL=(char)(64+Count%26);
if(Count<26)
{
sLen=ng();
}
else
{
sLen=ng()+ng();
}
//标题
stringsTmp=sLen+"1";
RangeranCaption=_Range(sTmp,"A1");
string[]asCaption=newstring[Count];
for(inti=0;i
{
asCaption[i]=s[i].HeaderText;
}
2=asCaption;
//数据
object[]obj=newobject[];
for(intr=0;r
{
for(intl=0;l<;l++)
{
if(gridView[l,r].ValueType==typeof(DateTime))
{
obj[l]=gridView[l,r].ng();
}
else
{
obj[l]=gridView[l,r].Value;
}
}
stringcell1=sLen+((int)(r+2)).ToString();
stringcell2="A"+((int)(r+2)).ToString();
Rangeran=_Range(cell1,cell2);
2=obj;
}
//保存
pyAs(fileName);
=true;
}
finally
{
//关闭
ntrol=false;
();
}
returntrue;
}
方法2
用流保存成xls文件.这种方法比较好,不用引用Excel组件.下面是具体例子,可以参考
;
;
///
///另存新档按钮
///
privatevoidSaveAs()//另存新档按钮导出成Excel
{
SaveFileDialogsaveFileDialog=newSaveFileDialog();
="Execlfiles(*.xls)|*.xls";
Index=0;
eDirectory=true;
Prompt=true;
="ExportExcelFileTo";
alog();
StreammyStream;
myStream=le();
StreamWritersw=newStreamWriter(myStream,oding(-0));
stringstr="";
try
{
//写标题
for(inti=0;i
{
if(i>0)
{
str+="t";
}
str+=s[i].HeaderText;
}
ine(str);
//写内容
for(intj=0;j<;j++)
{
stringtempStr="";
for(intk=0;k<;k++)
{
if(k>0)
{
tempStr+="t";
}
tempStr+=[j].Cells[k].ng();
}
ine(tempStr);
}
();
();
}
catch(Exceptione)
{
(ng());
}
finally
{
();
();
}
}
C#WinForm下DataGridView导出Excel
的实现
1.说明:导出的效率说不上很高,但至少是可以接收的.参考网上很多高效导出Excel的方法,实
现到时能够实现的,导出速度也很快,不过缺陷在与不能很好的进行单元格的格式化,比如上
图中的"拼音码"字段中的值"120",在导出后就显示"12120",挺郁闷的!o(∩_
∩)o,废话不说了,进入正题.......
2.首先添加Excel引用
3.实现代码
///
///DataGridView导出Excel
///
///
///
///
privateintExportExcel(stringstrCaption,DataGridViewmyDGV)
{
intresult=9999;
//列索引,行索引,总列数,总行数
intColIndex=0;
intRowIndex=0;
intColCount=Count;
intRowCount=nt;
if(nt==0)
{
result=1;
}
//创建Excel对象
ationxlApp=new
ationClass();
if(xlApp==null)
{
result=2;
}
try
{
//创建Excel工作薄
okxlBook=(true);
eetxlSheet=
(eet)eets[1];
//设置标题
ange=_Range([1,1],[1,
ColCount]);//标题所占的单元格数与DataGridView中的列数相同
ells=true;
aR1C1=strCaption;
=20;
=true;
ntalAlignment=er;
//创建缓存数据
object[,]objData=newobject[RowCount+1,ColCount];
//获取列标题
foreach(s)
{
objData[RowIndex,ColIndex++]=Text;
}
//获取数据
for(RowIndex=1;RowIndex
{
for(ColIndex=0;ColIndex
{
if(myDGV[ColIndex,RowIndex-1].ValueType==typeof(string)
||myDGV[ColIndex,RowIndex-1].ValueType==typeof(DateTime))//这里就是验证
DataGridView单元格中的类型,如果是string或是DataTime类型,则在放入缓存时在该内容前
加入"";
{
objData[RowIndex,ColIndex]=""+myDGV[ColIndex,RowIndex-1].Value;
}
else
{
objData[RowIndex,ColIndex]=myDGV[ColIndex,RowIndex-1].Value;
}
}
ts();
}
//写入Excel
range=_Range([2,1],[RowCount,ColCount]);
2=objData;
//保存
=true;
pyAs("C:测试"+ng("yyyyMMddHHmmss")+".xls");
//返回值
result=0;
}
catch(Exceptionerr)
{
result=9999;
}
finally
{
();
t();//强制回收
}
returnresult;
}
4.调用方法(上图中"生成Excel文件"按钮的on
Click事件)
privatevoidbutton4_Click(objectsender,EventArgse)
{
intresult=Excel("测试",idView1);//idView1ataGridView控
件
(ng());
}
更多推荐
gridview导出excel
发布评论