0. 導(dǎo)入命名空間:
1 |
using Microsoft.Office.Core; |
2 |
using Microsoft.Office.Interop.Excel; |
4 |
using System.Reflection; |
1. 如何打開已有excel文檔,或者創(chuàng)建一個新的excel文檔
1 |
Application app = new Application(); |
2 |
Workbooks wbks = app.Workbooks; |
3 |
_Workbook _wbk = wbks.Add(xxx); |
若打開已有excel,把“xxx”替換成該excel的文件路徑;
注:若新建一個excel文檔,“xxx”替換成true即可;不過這里新建的excel文檔默認(rèn)只有一個sheet。
2. 取得、刪除和添加sheet
1 |
Sheets shs = _wbk.Sheets; |
2.1取得:
2 |
_Worksheet _wsh = (_Worksheet)shs.get_Item(i) |
2.2 刪除:
2 |
app.DisplayAlerts = false; |
2.3 添加:
2 |
app.Worksheets.Add(a,b,c,d); |
2.4 sheet的重命名
3. 刪除行和列
3.1 刪除行:
1 |
((Range)_wsh.Rows[3, Missing.Value]).Delete(XlDeleteShiftDirection.xlShiftUp); |
3.2 刪除列:
3 |
_wsh.Cells[_wsh.Rows.Count, 2]).Delete(XlDeleteShiftDirection.xlShiftToLeft |
4. 添加行和列
4.1 添加行:
1 |
((Range)_wsh.Rows[11, Missing.Value]) |
2 |
.Insert(Missing.Value, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); |
4.2 添加列:
2 |
_wsh.Cells[1, 1], _wsh.Cells[_wsh.Rows.Count, 1]) |
3 |
.Insert(Missing.Value, XlInsertShiftDirection.xlShiftToRight); |
5. 單元格操作
5.1 單元格的取得
5.2 設(shè)置公式
2 |
_wsh.Cells[row, cell] = "=Sum(A1/B1)"; |
5.3 合并單元格
1 |
((Range)_wsh.Rows[1, Missing.Value]).Merge(Missing.Value); |
5.4 設(shè)置行高和列寬
1 |
((Range)_wsh.Rows[3, Missing.Value]).RowHeight = 5; |
2 |
((Range)_wsh.Rows[3, Missing.Value]).ColumnWidth = 5; |
5.5 設(shè)置單元格顏色 顏色共有56中,詳情請參照附錄的[顏色對照表]
1 |
((Range)_wsh.Rows[1, Missing.Value]).Interior.ColorIndex = 3; |
5.6 設(shè)置字號
1 |
((Range)_wsh.Cells[1, "B"]).Font.Size = 8; |
5.7 是否設(shè)置粗體
1 |
((Range)_wsh.Rows[1, Missing.Value]).Font.Bold = false; |
5.8 單元格/區(qū)域、水平垂直居中
1 |
((Range)_wsh.Cells[2, 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter; |
5.9 設(shè)置區(qū)域邊框
1 |
((Range)_wsh.Cells[3, 3]).Borders.LineStyle = 3; |
5.10 設(shè)置邊框的上、下、左、右線條
03 |
_wsh.Cells[2, 1], _wsh.Cells[2, 2]) |
04 |
.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick; |
08 |
_wsh.Cells[2, 1], _wsh.Cells[2, 2]) |
09 |
.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick; |
13 |
_wsh.Cells[2, 1], _wsh.Cells[2, 2]) |
14 |
.Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick; |
18 |
_wsh.Cells[2, 1], _wsh.Cells[2, 2]) |
19 |
.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick; |
6. 指定區(qū)域的復(fù)制
01 |
_Worksheet _wsh = (_Worksheet)shs.get_Item(1); |
03 |
Range range = _wsh.get_Range(_wsh.Cells[7, 1], _wsh.Cells[10, _wsh.Columns.Count]); |
06 |
range.Copy(Type.Missing); |
09 |
Range test = ((Range)_wsh.Cells[11, 1]); |
13 |
app.DisplayAlerts = false; |
14 |
test.Parse(Missing.Value, Missing.Value); |
注:Type.Missing和Missing.Value,在excel的操作中被視為某些參數(shù)的默認(rèn)值,他們起到的作用很多時候是形式補足參數(shù)
7. excel文件的保存,及后續(xù)處理
7.1 文件保存
2 |
app.AlertBeforeOverwriting = false; |
5 |
SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); |
注:這個地方只能采用該方法保存,不然在指定路徑下保存文件外,在我的文檔中也會生成一個對應(yīng)的副本
7.2 后續(xù)處理:退出和釋放
6 |
System.Runtime.InteropServices.Marshal.ReleaseComObject(app); |
說明:在application關(guān)閉的過程中,通常我們有兩種方案:
#直接退出app
#先關(guān)閉workbook,然后關(guān)閉workbooks,最后在退出app
鑒于這兩種方式,或許本質(zhì)上是一樣的(這點需要證明),但是依據(jù)我們軟件開發(fā)的原則:哪里需要哪里聲明,哪里結(jié)束哪里釋放回收。
既然在直接退出app的時候,我們不清楚workbook和workbooks具體在什么時間關(guān)閉,不如在結(jié)束的時候直接手動關(guān)閉,這樣做可以做到資源的快速直接回收;
所以,建議采用先關(guān)閉workbook,然后關(guān)閉workbooks,最后在退出app。
8. 關(guān)于單元格設(shè)置域和取得域里需要的數(shù)據(jù)
8.1 若單元格已經(jīng)設(shè)置為下拉框
2 |
((Range)_wsh.Cells[2, 1]) |
3 |
.Validation.Modify(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, Type.Missing, "1,2,3", Type.Missing); |
8.2 若單元格還沒有設(shè)置為下拉框的形式
1 |
((Range)_wsh.Cells[2, 1]) |
2 |
.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, Type.Missing,"1,2,3", Type.Missing); |
8.3 取得下拉框域的值
1 |
string strValue = ((Range)_wsh.Cells[2, 1]).Validation.Formula1; |
注:若在excel模板中通過有效性設(shè)定了下拉框的值,strValue得到的將會是excel里的公式,需將其轉(zhuǎn)換, 取得strValue后,可以根據(jù)其索引得到你需要的數(shù)值;
9. 隱藏行和隱藏列
9.1 隱藏行
1 |
_wsh.get_Range(_wsh.Cells[19, 1], _wsh.Cells[22, 1]).EntireRow.Hidden = true; |
9.2 隱藏列
1 |
_wsh.get_Range(_wsh.Cells[1, 1], _wsh.Cells[_wsh.Rows.Count, 1]) |
2 |
.EntireColumn.Hidden = true; |
|