首页 > 编程 > .NET > 正文

asp.net(C#)操作excel(上路篇)

2020-01-18 00:57:07
字体:
来源:转载
供稿:网友
1.作业环境 

开发环境:vs2005 /vs2008
数据库:sql2005

excel:2003
首先
vs加入com件(当然也可以加入.net下的excel组件):
之后vs引用子目录会多出下面三个dll:
 简单操作流程如下:
复制代码 代码如下:

using Excel;

// from bill example
public void writeExcelAdvance(String outputFile)
{
string[,] myData =
{
{ "车牌号", "类型", "品 牌", "型 号", "颜 色", "附加费证号", "车架号" },
{ "浙KA3676", "危险品", "货车", "铁风SZG9220YY", "白", "1110708900", "022836" },
{ "浙KA4109", "危险品", "货车", "解放CA4110P1K2", "白", "223132", "010898" },
{ "浙KA0001A", "危险品", "货车", "南明LSY9190WS", "白", "1110205458", "0474636" },
{ "浙KA0493", "上普货", "货车", "解放LSY9190WS", "白", "1110255971", "0094327" },
{ "浙KA1045", "普货", "货车", "解放LSY9171WCD", "蓝", "1110391226", "0516003" },
{ "浙KA1313", "普货", "货车", "解放9190WCD", "蓝", "1110315027", "0538701" },
{ "浙KA1322", "普货", "货车", "解放LSY9190WS", "蓝", "24323332", "0538716" },
{ "浙KA1575", "普货", "货车", "解放LSY9181WCD", "蓝", "1110314149", "0113018" },
{ "浙KA1925", "普货", "货车", "解放LSY9220WCD", "蓝", "1110390626", "00268729" },
{ "浙KA2258", "普货", "货车", "解放LSY9220WSP", "蓝", "111048152", "00320" }
};
//引用Excel Application类别
Application myExcel = null;
//引用活页簿类别
Workbook myBook = null;
//引用工作表类别
Worksheet mySheet = null;
//引用Range类别
Range myRange = null;
//开启一个新的应用程式
myExcel = new Microsoft.Office.Interop.Excel.Application();
//打开一个已经存在的excel excel2003和2007版打开参数不一样,具体可以上网上去查:
//excelSql.Workbooks.Open(@"C:/08.xls"(已经存在的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);
//加入新的活页簿
myExcel.Workbooks.Add(true);
//停用警告讯息
myExcel.DisplayAlerts = false;
//让Excel文件可见
myExcel.Visible = true;
//引用第一个活页簿
myBook = myExcel.Workbooks[1];
//设定活页簿焦点
myBook.Activate();
//引用第一个工作表
mySheet = (Worksheet)myBook.Worksheets[1];
//命名工作表的名称为 "Array"
mySheet.Name = "Cells";
//设工作表焦点
mySheet.Activate();
int a = 0;
int UpBound1 = myData.GetUpperBound(0);
//二维阵列数上限
int UpBound2 = myData.GetUpperBound(1);
//二维阵列数上限
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//以下的Select方法可省略,加速Excel运行,但VBA有些功能必须要用到Select方法。
//逐行写入数据
for (int i = 0; i < UpBound1; i++)
{
for (int j = 0; j < UpBound2; j++)
{
//以单引号开头,表示该单元格为纯文字
a++;
//用offset写入阵列资料
myRange = mySheet.get_Range("A2", Type.Missing);
myRange.get_Offset(i, j).Select();
myRange.Value2 = "'" + myData[i, j];
//用Cells写入阵列资料
myRange.get_Range(myExcel.Cells[2 + i, 1 + j], myExcel.Cells[2 + i, 1 + j]).Select();
myExcel.Cells[2 + i, 1 + j] = "'" + myData[i, j];
}
}
//加入新的工作表在第1张工作表之后
myBook.Sheets.Add(Type.Missing, myBook.Worksheets[1], 1, Type.Missing);
//引用第2个工作表
mySheet = (Worksheet)myBook.Worksheets[2];
//命名工作表的名称为 "Array"
mySheet.Name = "Array";
//Console.WriteLine(mySheet.Name);
//写入报表名称
myExcel.Cells[1, 4] = "普通报表";
//设定范围
myRange = (Range)mySheet.get_Range(myExcel.Cells[2, 1], myExcel.Cells[UpBound1 + 1, UpBound2 + 1]);
myRange.Select();
//用阵列一次写入资料
myRange.Value2 = "'" + myData;
//设定储存路径
//string PathFile = Directory.GetCurrentDirectory() + @"/button4.xls";
//另存活页簿
myBook.SaveAs(outputFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//关闭活页簿
myBook.Close(false, Type.Missing, Type.Missing);
//关闭Excel
myExcel.Quit();
//释放Excel资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
mySheet = null;
myRange = null;
myExcel = null;
GC.Collect();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表