标签:
C#操作Excel可以分为客户端和插件版本,区别就是是否需要Excel环境,功能实现一样
c:\program files\Microsoft office\offiece11\excel.exe
using Microsoft.office.Interop.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);
//C#6.0后面的参数可以省略
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];
Application app=new Application();
Workbook wbook=app.Workbooks.Add(Type.missing);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];
worksheet.Cells[1,2]="列内容";
string temp=((Range)worksheet.Cells[1,2]).Text;
Microsoft.Office.Interop.Excel.Range rtemp=worksheet.get_Range("A1","A1");
rtemp.Font.Name="宋体";
rtemp.Font.FontStyle="加粗";
rtemp.Font.Size=5;
//设置颜色
rtemp.Interior.Color = ColorTranslator.ToOle(Color.DodgerBlue);
worksheet.SaveAs("c:\\temp.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing);
Cells[18, 8] = "=SUM(D5:D17)";
下次进行补充哈
int k1=worksheet.UsedRange.Rows.Count;
获取列只需要将Rows修改为Columns
sheet.Cells[i, j].HasFormula
Workbook book = app.Workbooks.Open(file.FullName);
foreach (Worksheet sheet in book.Worksheets)
{
for (int i = 1; i < sheet.UsedRange.Rows.Count; i++)
{
for (int j = 1; j < sheet.UsedRange.Columns.Count; j++)
//此处进行函数操作
}
}
本人承接一些程序开发项目(能力范围内,例如Office开发和C#网络爬虫,数据库读写等)欢迎联系洽谈,QQ207708848,请注明来意。
更多内容还可以参考官方API
标签:
原文地址:http://www.cnblogs.com/hxh88/p/5875194.html