将数据集中的数据导出到Excle中.docx
- 文档编号:6105543
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:49
- 大小:29.59KB
将数据集中的数据导出到Excle中.docx
《将数据集中的数据导出到Excle中.docx》由会员分享,可在线阅读,更多相关《将数据集中的数据导出到Excle中.docx(49页珍藏版)》请在冰点文库上搜索。
将数据集中的数据导出到Excle中
将数据集中的数据导出到Excle中
首先需要了解一下excle对象的相关方法,application,workbooks,workbook,range对象,网上有好多介绍的。
了解了这几个对象后就开始导入导出了,我自己总结了两种方法导出数据集中(suchasdatatable)的内容。
第一种方式是利用response不端的向客户端写datatable中的内容,只需要给response增加一个header就可以了,那你写的所有数据流都是可以放到excle中,直接贴代码咯:
Response.Clear();
DataCollectionsdc=DataCollections.getDataCollection();
Response.AppendHeader("Content-Disposition","attachment;filename=notexcelObj.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType="application/ms-excel";
Response.Write("机型");Response.Write("\t");/*跳到下一个制表格*/Response.Write("机型号");Response.Write("\r\n");
foreach(DataRowitemindc.Rows)
{
Response.Write(item[0].ToString());
Response.Write("\t");
Response.Write(item[1].ToString());
Response.Write("\r\n");
}
Response.End();
不断遍历数据集输出到Excel,这种方式效率还是比较快的,但是不够灵活,比如要给某一个单元格加批注好像就做不了了。
第二种方式是用excel对象,个人感觉这种方式微软做的还不太好,excel对象在退出后还是杀不了excel这个进程,网上有好多解决的方案,可以搜一下。
并且excel这个方式效率还是比较慢的,不过操作灵活,可以控制每一个单元格的属性和方法。
我没有想到过怎么样将excel内容直接输出到客户端,现在是先写了excel文件保存到本地,然后将excel文件输出到客户端同时删除该exce文件,如果有更好的方法,希望大家多多指教。
---输出excel表
objectobjReflector=System.Reflection.Missing.Value;
Excel.ApplicationappExcel=null;
Excel.WorkbookworkBook=null;
try
{
stringtempFilePath=Server.MapPath("~")+"\\temp\\"+Guid.NewGuid().ToString()+".xls";
Excel.Sheetssheets=initExcelObj(refappExcel,refworkBook);
Excel.WorksheetworkSheet=(Excel.Worksheet)sheets.get_Item
(1);
operationExcelObj(workSheet,DataCollections.getDataCollection());
workBook.SaveAs(tempFilePath,Excel.XlFileFormat.xlXMLSpreadsheet,objReflector,objReflector,false,false,
Excel.XlSaveAsAccessMode.xlNoChange,objReflector,objReflector,objReflector,objReflector,objReflector);
appExcel.Workbooks.Close();
exportExcelToClient(tempFilePath);
deleteTempExcel(tempFilePath);
}
catch(Exceptionex)
{
thrownewException(ex.Message);
}
finally
{
if(appExcel!
=null)
{
appExcel.Workbooks.Close();
appExcel.Quit();
appExcel=null;
GC.Collect(2,GCCollectionMode.Forced);
}
}
///
///数据集写excel表
///
/// "> privatevoidoperationExcelObj(Excel.WorksheetworkSheet,DataCollectionsdc) { workSheet.Name="ldlovewf"; workSheet.Cells["1",columnA]="数码品牌"; workSheet.Cells["1",columnB]="数码描述"; for(inti=0,j=2;i { for(intk=0;k { if(k%2==0) workSheet.Cells[j.ToString(),columnA]=dc.Rows[i][k].ToString(); else workSheet.Cells[j.ToString(),columnB]=dc.Rows[i][k].ToString(); } } } /// ///初始化excel对象 /// /// /// privateExcel.SheetsinitExcelObj(refExcel.ApplicationappExcel,refExcel.WorkbookworkBook) { appExcel=newExcel.ApplicationClass(); //appExcel.Visible=true; Excel.WorkbooksworkBooks=appExcel.Workbooks; workBook=workBooks.Add(System.Reflection.Missing.Value); Excel.Sheetssheets=workBook.Worksheets; returnsheets; } --读取临时的excel表,输出到客户端 privatevoidexportExcelToClient(stringfilePath) { Response.Clear(); Response.AppendHeader("Content-Disposition","attachment;filename=notexcelObj.xls"); Response.ContentType="application/ms-excel"; Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312"); Response.Buffer=true; Response.WriteFile(filePath); Response.End(); } --删除临时excel表 privatevoiddeleteTempExcel(stringfilePath) { if(System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } 这种方式比较繁琐,待续,以后研究一下有没有更好的方法。 打开VisualStudio2008在文件(File)菜单上,单击新建项目(NewProject)。 在新建项目(NewProject)对话框的模板(Templates)窗格中,单击Windows应用程序(WindowsApplication)。 单击确定(OK) 窗体应用技巧一,创建浮动窗体。 创建新工程后,选择Form1窗体,添加Timer1和Timer2控件。 为窗体选择一个好看的背景,当然你也可以使用系统默认的背景。 进入代码编辑器,输入代码: PublicClassForm1 InheritsSystem.Windows.Forms.Form PrivateSubForm1_Load(ByValsenderAsSystem.Object, ByValeAsSystem.EventArgs)HandlesMyBase.Load DimposAsPoint=NewPoint(100,50)'设置窗体初始位置 Me.DesktopLocation=pos Timer1.Interval=10'设置Timer的值 Timer1.Enabled=True Timer2.Interval=10 Timer2.Enabled=False EndSub 进入Timer1_Tick事件 PrivateSubTimer1_Tick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs) HandlesTimer1.Tick DimposAsPoint=NewPoint(Me.DesktopLocation.X+2,Me.DesktopLocation.Y+1) '窗体左上方横坐标的timer1加 Ifpos.X<600Orpos.Y<400Then Me.DesktopLocation=pos Else Timer1.Enabled=False Timer2.Enabled=True EndIf EndSub 进入Timer2_Tick事件 PrivateSubTimer2_Tick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs) HandlesTimer2.Tick DimposAsPoint=NewPoint(Me.DesktopLocation.X-2,Me.DesktopLocation.Y-1) '窗体的左上方横坐标随着timer2减一 Ifpos.X>100Orpos.Y>50Then Me.DesktopLocation=pos Else Timer1.Enabled=True Timer2.Enabled=False EndIf EndSub 创建完成后我们来运行程序测试一下,测试成功,程序在屏幕中不断地来回走动了。 窗体应用技巧二,创建透明的窗体。 创建新工程后,选择Form1窗体,添加Label1、TrackBar1、Timer1控件。 为了突出效果为窗体选择一个好看的背景。 相关的属性设置如下: TrackBar1Value属性: TickFrequency: 属性: Maximum属性: 100 10 100 Label1Text属性: 选择窗体的透明度: Timer1Interval属性: 100 进入代码编辑器,输入代码: 首先进行声明: PublicClassForm1 InheritsSystem.Windows.Forms.Form DimtpsAsInteger DimbolAsBoolean 进入TrackBar1_Scroll事件 PrivateSubTrackBar1_Scroll(ByValsenderAsObject, ByValeAsSystem.EventArgs)HandlesTrackBar1.Scroll Me.Opacity=TrackBar1.Value/100 Label1.Text="窗体透明度: "&CStr(Me.Opacity*100)&"%" EndSub 进入Timer1_Tick事件 PrivateSubTimer1_Tick(ByValsenderAsObject, ByValeAsSystem.EventArgs)HandlesTimer1.Tick Ifbol=FalseThen tps=tps+1 Me.Opacity=tps/100 IfMe.Opacity>=1Then Timer1.Enabled=False bol=True EndIf Else tps=tps-1 Me.Opacity=tps/100 IfMe.Opacity<=0Then Timer1.Enabled=False bol=False EndIf EndIf EndSub 进入Form1_Load事件 PrivateSubForm1_Load(ByValsenderAsSystem.Object, ByValeAsSystem.EventArgs)HandlesMyBase.Load Timer1.Enabled=True EndSub 进入Form1_Closing事件 PrivateSubForm1_Closing(ByValsenderAsObject, ByValeAsSystem.ComponentModel.CancelEventArgs)HandlesMyBase.Closing Timer1.Enabled=True IfMsgBox("你确实要关闭窗体吗? ",MsgBoxStyle.OkCancel)=MsgBoxResult.OkThen e.Cancel=False Else Timer1.Enabled=False Me.Opacity=1 tps=100 bol=True e.Cancel=True EndIf EndSub 创建完成后我们来运行程序测试一下,测试成功,程序窗体是不是变得透明了,通过调节滚动条我们甚至可以使得窗体消失达到完全隐形的目的。 这是不是很神奇呢? 摘要: 刚赶工写了一个获取新浪天气预报的功能,顺便把代码分享了出来,以后再有这样的工作,大家可以直接把代码拿来使用。 标签: ASP.NET 新浪 天气 Oracle帮您准确洞察各个物流环节 1.利用新浪提供给的iframe直接嵌入,这种方式非常的简单,但是却没有交互性。 代码如下: 2.抓取当天的天气,以指定格式输出。 涉及的核心代码如下: publicstaticArrayListGetWeather(stringcode) { /* [0]"北京"string [1]"雷阵雨"string [2]"9℃"string [3]"29℃"string [4]"小于3级"string */ stringhtml=""; try { HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create("+code+"_w.html"); request.Method="Get"; //request.Timeout = 1; request.ContentType="application/x-www-form-urlencoded"; WebResponseresponse=request.GetResponse(); Streams=response.GetResponseStream(); StreamReadersr=newStreamReader(s,System.Text.Encoding.GetEncoding("GB2312")); html=sr.ReadToEnd(); s.Close(); sr.Close(); } catch(Exceptionerr) { thrownewException("访问地址出错~~~"); } intcount=html.Length; intstarIndex=html.IndexOf(" ",starIndex,count-starIndex); html=html.Substring(starIndex,endIndex-starIndex+8); //得到城市 intcityStartIndex=html.IndexOf("",0,html.Length); intcityEndIndex=html.IndexOf("",0,html.Length); stringCity=html.Substring(cityStartIndex+3,cityEndIndex-cityStartIndex-3); //得到天气 intweatherStartIndex=html.IndexOf("",cityEndIndex); intweatherEndIndex=html.IndexOf("",weatherStartIndex); stringWeather=html.Substring(weatherStartIndex+3,weatherEndIndex-weatherStartIndex-3); //得到温度 inttemperatureStartIndex=html.IndexOf(" inttemperatureEndIndex=html.IndexOf("",weatherEndIndex+3); stringTemperature=html.Substring(temperatureStartIndex+21,temperatureEndIndex-temperatureStartIndex-21); intint1=Temperature.IndexOf("℃",0); intint2=Temperature.IndexOf("~",0); intint3=Temperature.IndexOf("℃",int2); stringMinTemperature=Temperature.Substring(int2+1,int3-int2); stringMaxTemperature=Temperature.Substring(0,int2-int1+2); //得到风力 intwindforceStartIndex=html.IndexOf("风力: ",temperatureEndIndex); intwindforceEndIndex=html.IndexOf(" ",windforceStartIndex); stringWindforce=html.Substring(windforceStartIndex+3,windforceEndIndex-windforceStartIndex-3); if(Windforce.Contains("小于")&&(! Windforce.Contains("等于"))) //判断风力是否含有"小于"或"小于等于"字样将,如果有的话,将其替换为2- { //Windforce=Windforce.Replace("小于","2-"); stringstrWindforce=Windforce.Substring(2,Windforce.Length-3); intminWindforce=Int32.Parse(strWindforce)-1; Windforce=Windforce.Replace("小于",minW
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据 集中 导出 Excle
![提示](https://static.bingdoc.com/images/bang_tan.gif)