您的位置:首页 > 编程语言 > C#

C#下使用Excel 2007 COM接口将数据保存为Excel文件

2010-04-29 12:25 501 查看
首先需引用COM,Microsoft Excel 12.0 Object Library
visual studio 会自动添加

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace Excel输出DLL
{
public class ExcelOutput
{
public void ExcelTest()
{
//创建Excel应用程序
ApplicationClass excalApp = new ApplicationClass();
Workbook wb = excalApp.Workbooks.Add(Type.Missing);
Worksheet ws = (Worksheet)wb.ActiveSheet;
ws.Name = "第一个测试";
string item = "我是一个测试字";
ws.Cells[1, 1] = (object)item;
// 按格式保存工作簿
//XlFileFormat是一个枚举,提供很多存储格式,其中xlExcel8是Excel97-2003格式,xlExcel12是Excel2007格式
excalApp.ActiveWorkbook.SaveAs("f://test22", XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// 关闭Excel应用程序
excalApp.Quit();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: