Excel導(dǎo)出在C#代碼中應(yīng)用己經(jīng)很廣泛了,我這里就做些總結(jié),供自己和讀者學(xué)習(xí)用。
Excel知識(shí)點(diǎn)。
一、添加引用和命名空間
添加Microsoft.Office.Interop.Excel引用,它的默認(rèn)路徑是C:PRogram FilesMicrosoft Visual
Studio 9.0Visual Studio Tools for
OfficePIAOffice12Microsoft.Office.Interop.Excel.dll
代碼中添加引用using
Microsoft.Office.Interop.Excel;
二、Excel類的簡(jiǎn)單介紹
此命名空間下關(guān)于Excel類的結(jié)構(gòu)分別為:
applicationClass - 就是我們的excel應(yīng)用程序。
Workbook -
就是我們平常見(jiàn)的一個(gè)個(gè)excel文件,經(jīng)常是使用Workbooks類對(duì)其進(jìn)行操作。
Worksheet - 就是excel文件中的一個(gè)個(gè)sheet頁(yè)。
Worksheet.Cells[row, column] -
就是某行某列的單元格,注意這里的下標(biāo)row和column都是從1開(kāi)始的,跟我平常用的數(shù)組或集合的下標(biāo)有所不同。
知道了上述基本知識(shí)后,利用此類來(lái)操作excel就清晰了很多。
三、Excel的操作
任何操作Excel的動(dòng)作首先肯定是用excel應(yīng)用程序,首先要new一個(gè)ApplicationClass 實(shí)例,并在最后將此實(shí)例釋放。
ApplicationClass xlsApp = new ApplicationClass(); // 1. 創(chuàng)建Excel應(yīng)用程序?qū)ο蟮囊粋€(gè)實(shí)例,相當(dāng)于我們從開(kāi)始菜單打開(kāi)Excel應(yīng)用程序。
if (xlsApp == null)
{
//對(duì)此實(shí)例進(jìn)行驗(yàn)證,如果為null則表示運(yùn)行此代碼的機(jī)器可能未安裝Excel
}1. 打開(kāi)現(xiàn)有的Excel文件
| 代碼如下 | 復(fù)制代碼 |
Workbook workbook = xlsApp.Workbooks.Open(excelFilePath, 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); | |
mySheet.Copy(Type.Missing, workbook.Sheets[1]); //復(fù)制mySheet成一個(gè)新的sheet頁(yè),復(fù)制完后的名稱是mySheet頁(yè)名稱后加一個(gè)(2),這里就是testsheet(2),復(fù)制完后,Worksheet的數(shù)量增加一個(gè)注意
這里Copy方法的兩個(gè)參數(shù),指是的復(fù)制出來(lái)新的sheet頁(yè)是在指定sheet頁(yè)的前面還是后面,上面的例子就是指復(fù)制的sheet頁(yè)在第一個(gè)sheet頁(yè)的后面。
3.刪除sheet頁(yè)
| 代碼如下 | 復(fù)制代碼 |
xlsApp.DisplayAlerts = false; //如果想刪除某個(gè)sheet頁(yè),首先要將此項(xiàng)設(shè)為fasle。
workbook.Saved = true; workbook.Close(true, Type.Missing, Type.Missing); /// <summary> if (xlApp == null) | |
如果要在excel中插入圖片,我們需要把代碼加入一行即可,如下所示
| 代碼如下 | 復(fù)制代碼 |
protected void ExportExcel(DataTable dt) if (xlApp == null) public void GenerateExcel() | |
運(yùn)行結(jié)果如下所示:

其中如下代碼的作用是
| 代碼如下 | 復(fù)制代碼 |
worksheet.Shapes.AddPicture("C:/Users/spring/Desktop/1.gif", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 200, 200, 300);在Excel的指定位置加入圖片 worksheet.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "123456", "Red", 15, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 150, 200); | |
在Excel的指定位置加入文本框,和里面的內(nèi)容.
我們可以這樣來(lái)設(shè)計(jì)一個(gè)ExcelBase的基類:
先創(chuàng)建一個(gè)ExcelBE.cs:
| 代碼如下 | 復(fù)制代碼 |
public ExcelBE(int row, int col, string text, string startCell, string endCell, string interiorColor, bool isMerge, int size, string fontColor, string format) public ExcelBE() public int Row public int Col public string Text public string StartCell public string EndCell public string InteriorColor public bool IsMerge public int Size public string FontColor public string Formart } | |
接下來(lái)創(chuàng)建ExcelBase.cs:
| 代碼如下 | 復(fù)制代碼 |
public class ExcelBase public ExcelBase() public void createDoc() public void InsertData(ExcelBE be) private int GetColorValue(string interiorColor) default: | |
調(diào)用的代碼如下:
| 代碼如下 | 復(fù)制代碼 |
private void btnRun_Click(object sender, EventArgs e) } | |

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注