C# 操作 EXCEL
以下是一些对excel的一些基本操作
1:工程对excel类库的导入,如:c:"program files"Microsoft office"offiece11"excel.exe
2:命名控件的引入: using Microsoft.office.Interop.Excel;
3:如果是对一个已经存在的excel文件进行操作则:
Application app=new Application();
Workbook wbook=app.Workbooks.Open("c:""temp.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);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];
4:如果是新建一个excel文件:
Application app=new Application();
Workbook wbook=app.Workbook.Add(Type.missing);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];
5:设置某个单元格里的内容:
worksheet.Cells[1,2]="列内容"
6读取某个单元格里的内容
string temp=((Range)worksheet.Cells[1,2]).Text;
7设置某个单元格里的格式
Excel.Range rtemp=worksheet.get_Range("A1","A1");
rtemp.Font.Name="宋体";
rtemp.Font.F;
rtemp.Font.Size=5;
8 保存新建的内容:
worksheet.SaveAs("c:""temp.xls",Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing);
9 释放资源
app.quit();