资源描述:
《C#操作Excel电子表格》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、C#操作Excel电子表格2011-01-2112:51:47
2、 分类:默认分类
3、 标签:
4、字号大中小 订阅添加引用:MicrosoftExcel11.0ObjectLibrary;添加:usingMicrosoft.Office.Interop.Excel;★打开Excel文件============================Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Interop.Excel.Ap
5、plication();Workbookworkbook1=excel1.Workbooks.Open(@"E:aaa.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);excel1
6、.Visible=true;★新建Excel对象============================Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Interop.Excel.Application();Workbookworkbook1=excel1.Workbooks.Add(XlWBATemplate.xlWBATWorksheet或true);worksheet1.Activate();//激活sheet
7、1表excel1.Visible=true;★新建Excel表============================ Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Interop.Excel.Application();Workbookworkbook1=excel1.Workbooks.Add(true);Worksheetworksheet1=(Worksheet)workbook1.Worksheets[
8、"sheet1"];Worksheetworksheet1=(Worksheet)workbook1.Worksheets.Add(Type.Missing,workbook1.Worksheet[1],1,Type.Missing);excel1.Visible=true; ★保存Excel============================== Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Intero
9、p.Excel.Application();Workbookworkbook1=excel1.Workbooks.Add(true);Worksheetworksheet1=(Worksheet)workbook1.Worksheets["sheet1"];worksheet1=(Worksheet)workbook1.Worksheets.Add(Type.Missing,workbook1.Worksheets[1],1,Type.Missing);worksheet1.Activate();work
10、sheet1.Cells[2,2]=3455555;excel1.Visible=true;excel1.DisplayAlerts=false;//不显示提示框workbook1.Close(true,"d:\1.xls",null);//关闭 worksheet1=null;workbook1=null;excel1.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);excel1=null;System.G
11、C.Collect(); ★关闭Excel============================== Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Interop.Excel.Application();Workbookworkbook1=excel1.Workbooks.Open(@"E:aaa.xls",Type.Missi