欢迎来到天天文库
浏览记录
ID:14860187
大小:57.50 KB
页数:19页
时间:2018-07-30
《c#生成excel,可建立sheet,设定sheet名称,调整列宽度》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、c#生成Excel,可建立sheet,设定sheet名称,调整列宽度Excel生成类ExcelRW.csC#代码1.usingSystem;2.usingSystem.Data;3.usingSystem.Configuration;4.usingSystem.Web;5.usingSystem.Web.Security;6.usingSystem.Web.UI;7.usingSystem.Web.UI.WebControls;8.usingSystem.Web.UI.WebControls.WebParts;9.usingSystem.Web.UI.HtmlCont
2、rols;10.usingMicrosoft.Office.Interop.Excel;11.usingSystem.Collections;12.usingSystem.Diagnostics;13.14.///15.///SummarydescriptionforExcelRW16.///17.publicstaticclassExcelRW18.{19.20.21.publicstaticvoidExcelSave(Hashtableht)22.{23.24.Applicationexcel=newApplication()
3、;//引用Excel对象25.26.WorkbooksoBooks;27.WorkbookoBook;28.SheetsoSheets;29.WorksheetoSheet;30.RangeoCells;31.stringsFile=ht["excelName"]asstring;32.//excel.Save("ddd.xls");33.ArrayListalSheet=ht["sheetNameList"]asArrayList;34.Workbookwb=excel.Workbooks.Add(true);//引用Excel工作簿35.excel.Visible
4、=false;//使Excel可视36.oBooks=excel.Workbooks;37.oBook=oBooks.get_Item(1);38.oSheets=oBook.Worksheets;39.oSheet=(Worksheet)oSheets.get_Item(oSheets.Count);40.foreach(HashtablehtOneSheelinalSheet)41.{42.43.44.oSheet=(Worksheet)oSheets.get_Item(oSheets.Count);45.46.ArrayListColNameList=htOne
5、Sheel["ColName"]asArrayList;47.//命名该sheet48.oSheet.Name=htOneSheel["sheetName"]asstring;49.System.Data.DataTabledtable=htOneSheel["dataRows"]asSystem.Data.DataTable;50.Rangecolumn=((Range)oSheet.Cells[1,1]).EntireColumn;51.column.ColumnWidth=10;52.intiRow=1;53.foreach(DataRowdrindtable.
6、Rows)54.{55.inticel=1;56.for(intiCol=0;iCol7、s.get_Item(1)).Activate();66.oSheet=(Worksheet)oSheets.get_Item(1);67.68.}69.stringstrFilePathAndname=HttpContext.Current.Server.MapPath(".")+"/Excel/"+sFile;70.71.if(System.IO.File.Exists(strFilePathAndname))72.{73.System.IO.File.Delete(strFilePathAndname);74.}75.//excel.Workb
7、s.get_Item(1)).Activate();66.oSheet=(Worksheet)oSheets.get_Item(1);67.68.}69.stringstrFilePathAndname=HttpContext.Current.Server.MapPath(".")+"/Excel/"+sFile;70.71.if(System.IO.File.Exists(strFilePathAndname))72.{73.System.IO.File.Delete(strFilePathAndname);74.}75.//excel.Workb
此文档下载收益归作者所有