C#操作excel(上路篇)

1.作业环境 

 

开发环境:vs2005 /vs2008
 
数据库:sql2005
 
excel:2003
首先
vs加入com組件(当然也可以加入.net下的excel组件):
之后vs引用子目录会多出下面三个dll:
 简单操作流程如下:
1 using Excel;
2 // from bill example
3 public void writeExcelAdvance(String outputFile)
4 {
5 string[,] myData =
6 {
7 { "車牌號", "類型", "品 牌", "型 號", "顏 色", "附加費證號", "車架號" },
8 { "浙KA3676", "危險品", "貨車", "鐵風SZG9220YY", "", "1110708900", "022836" },
9 { "浙KA4109", "危險品", "貨車", "解放CA4110P1K2", "", "223132", "010898" },
10 { "浙KA0001A", "危險品", "貨車", "南明LSY9190WS", "", "1110205458", "0474636" },
11 { "浙KA0493", "上普貨", "貨車", "解放LSY9190WS", "", "1110255971", "0094327" },
12 { "浙KA1045", "普貨", "貨車", "解放LSY9171WCD", "", "1110391226", "0516003" },
13 { "浙KA1313", "普貨", "貨車", "解放9190WCD", "", "1110315027", "0538701" },
14 { "浙KA1322", "普貨", "貨車", "解放LSY9190WS", "", "24323332", "0538716" },
15 { "浙KA1575", "普貨", "貨車", "解放LSY9181WCD", "", "1110314149", "0113018" },
16 { "浙KA1925", "普貨", "貨車", "解放LSY9220WCD", "", "1110390626", "00268729" },
17 { "浙KA2258", "普貨", "貨車", "解放LSY9220WSP", "", "111048152", "00320" }
18 };
19 //引用Excel Application類別
20 Application myExcel = null;
21 //引用活頁簿類別
22 Workbook myBook = null;
23 //引用工作表類別
24 Worksheet mySheet = null;
25 //引用Range類別
26 Range myRange = null;
27 //開啟一個新的應用程式
28 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);

29 //加入新的活頁簿
30 myExcel.Workbooks.Add(true);
31 //停用警告訊息
32 myExcel.DisplayAlerts = false;
33 //讓Excel文件可見
34 myExcel.Visible = true;
35 //引用第一個活頁簿
36 myBook = myExcel.Workbooks[1];
37 //設定活頁簿焦點
38 myBook.Activate();
39 //引用第一個工作表
40 mySheet = (Worksheet)myBook.Worksheets[1];
41 //命名工作表的名稱為 "Array"
42 mySheet.Name = "Cells";
43 //設工作表焦點
44 mySheet.Activate();
45 int a = 0;
46 int UpBound1 = myData.GetUpperBound(0);
47 //二維陣列數上限
48 int UpBound2 = myData.GetUpperBound(1);
49 //二維陣列數上限
50 //寫入報表名稱
51 myExcel.Cells[1, 4] = "普通報表";
52 //以下的Select方法可省略,加速Excel運行,但VBA有些功能必須要用到Select方法。
53 //以下的Select方法可省略,加速Excel運行,但VBA有些功能必須要用到Select方法。
54 //逐行寫入數據
55 for (int i = 0; i < UpBound1; i++)
56 {
57 for (int j = 0; j < UpBound2; j++)
58 {
59 //以單引號開頭,表示該單元格為純文字
60 a++;
61 //用offset寫入陣列資料
62 myRange = mySheet.get_Range("A2", Type.Missing);
63 myRange.get_Offset(i, j).Select();
64 myRange.Value2 = "'" + myData[i, j];
65 //用Cells寫入陣列資料
66 myRange.get_Range(myExcel.Cells[2 + i, 1 + j], myExcel.Cells[2 + i, 1 + j]).Select();
67 myExcel.Cells[2 + i, 1 + j] = "'" + myData[i, j];
68 }
69 }
70 //加入新的工作表在第1張工作表之後
71 myBook.Sheets.Add(Type.Missing, myBook.Worksheets[1], 1, Type.Missing);
72 //引用第2個工作表
73 mySheet = (Worksheet)myBook.Worksheets[2];
74 //命名工作表的名稱為 "Array"
75 mySheet.Name = "Array";
76 //Console.WriteLine(mySheet.Name);
77 //寫入報表名稱
78 myExcel.Cells[1, 4] = "普通報表";
79 //設定範圍
80 myRange = (Range)mySheet.get_Range(myExcel.Cells[2, 1], myExcel.Cells[UpBound1 + 1, UpBound2 + 1]);
81 myRange.Select();
82 //用陣列一次寫入資料
83 myRange.Value2 = "'" + myData;
84 //設定儲存路徑
85 //string PathFile = Directory.GetCurrentDirectory() + @"\button4.xls";
86 //另存活頁簿
87 myBook.SaveAs(outputFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
88 , XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
89 //關閉活頁簿
90 myBook.Close(false, Type.Missing, Type.Missing);
91 //關閉Excel
92 myExcel.Quit();
93 //釋放Excel資源
94 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
95 myBook = null;
96 mySheet = null;
97 myRange = null;
98 myExcel = null;
99 GC.Collect();
100

 

posted on 2010-02-24 15:01  MR_ke  阅读(4923)  评论(15编辑  收藏  举报

导航