目录结构:
contents structure [+]
1.Microsoft.Office.Interop.Excel.Application
这.net 库自带的处理Excel的API,不过想要使用该类,电脑上必须已经安装上了Microsoft Office。
static void Main(string[] args)
{
//创建Application的对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
return;
} //获得 工作簿集合
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;
//向工作簿集合中,添加一个空白工作簿,并且获得添加工作簿的引用
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //获得 工作表
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[]; //得到Range对象
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[, ];
range.Value2 = "hello"; //保存
workbook.SaveAs(@"C:\Users\Administrator\Desktop\a.xlsx");
}
创建工作簿实例(Workbook)时,除了使用Add方法,还可以使用Open方法,从本地Excel文件创建Workbook实例,例如:
Workbook workbook=workbooks.Open(@"excel文件", 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);
使用系统自带的API操作Excel文件,过于繁琐,下面介绍使用Aspose.cell插件。
2.Aspose.cell插件
Asponse.cell是一个第三方插件,它解决了.net自带API操作Excel的繁琐。创建Aspose.cell.Workbook对象,Aspose.cell插件提供了6中Workbook的构造函数:
public Workbook();//创建默认的Workbook实例
public Workbook(FileFormatType fileFormatType);//以指定的文件格式类型创建Workbook实例,例如Pdf,Xlsx,Html,SVG....
public Workbook(Stream stream);//从指定的流中创建Workbook实例
public Workbook(string file);//从指定的文件中创建Workbook实例
public Workbook(Stream stream, LoadOptions loadOptions);//以指定的加载选项,从流中创建Workbook实例
public Workbook(string file, LoadOptions loadOptions);//以指定的加载选项,从文件中创建Workbook实例
例如:
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();//创建工作簿
Aspose.Cells.Worksheet workSheet=workbook.Worksheets[];//获得工作表
操作单元格
//获得B1单元格
Aspose.Cells.Cell cell = workSheet.Cells[,];//除了使用有参属性[int,int]获取单元格,还可以使用有参属性[String]获取,例如Cells["A1"]
//设置单元格值
cell.PutValue("word");
//创建Style对象
Aspose.Cells.Style style = new Aspose.Cells.Style();
style.SetBorder(Aspose.Cells.BorderType.LeftBorder, Aspose.Cells.CellBorderType.Dotted, System.Drawing.Color.Red);//设置边框格式
style.Font.Color = System.Drawing.Color.Green;//设置字体颜色
style.Font.Size = ;//设置字体大小
style.Font.Underline = Aspose.Cells.FontUnderlineType.Single;//设置下划线
style.BackgroundColor = System.Drawing.Color.White;//设置背景颜色
style.RotationAngle = ;//设置旋转角度
//设置单元格样式
cell.SetStyle(style);
保存
既可以保存到本地,也可以保存到流中
workbook.Save("地址");
Stream stream= workbook.SaveToStream();
在Web应用中,有时候需要让用户下载文件,那么这时候可以把流设置到响应流中,例如:
FileName += "_"+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";
HttpResponseBase response = (HttpResponseBase)HttpContext.Response;
response.Buffer = true;
response.Charset = "utf-8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
response.BinaryWrite(workbook.SaveToStream().ToArray());
response.End();
例如下面是操作Excel的简单案例:
//创建 工作簿 Workbook对象
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(@"读取文件的路径");
//获得工作表
Aspose.Cells.Worksheet workSheet=workbook.Worksheets[];
//获得0,0单元格
Aspose.Cells.Cell cell = workSheet.Cells[,];
//设置单元格值
Console.WriteLine(cell.Value);
cell.PutValue("你好");
//创建Style对象
Aspose.Cells.Style style = new Aspose.Cells.Style();
style.Font.Color = System.Drawing.Color.Green;//设置字体颜色
//设置单元格样式
cell.SetStyle(style);
workbook.Save(@"保存文件的路径");