C# 操作 Excel 要點(diǎn)
1、 引用Microsoft.Office.Interop.Excel.dll
打開(kāi)VS2008的“解決方案資源管理器”,在“引用”文件夾下點(diǎn)擊鼠標(biāo)右鍵,選擇“添加引用”,打開(kāi)“添加引用”對(duì)話框。
在“組件名稱(chēng)”那里找到“Microsoft.Office.Interop.Excel”,如上圖所示。這里顯示有兩個(gè)可供選擇的“Microsoft.Office.Interop.Excel”版本,我們選擇其中的一個(gè)就可以了。單擊“確定”,該組件就被正確引用進(jìn)來(lái)了,如下圖所示。 2、引用命名空間、使用別名 using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
3、簡(jiǎn)單例子
public void ToExcel(string strTitle)
{
int rowCount = nMax - nMin + 1;//總行數(shù)
const int columnCount = 4;//總列數(shù)
//創(chuàng)建Excel對(duì)象
Excel.Application excelApp = new Excel.ApplicationClass();
//新建工作簿
Excel.Workbook workBook = excelApp.Workbooks.Add(true);
//新建工作表
Excel.Worksheet worksheet = workBook.ActiveSheet as Excel.Worksheet;
//設(shè)置標(biāo)題
Excel.Range titleRange = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);//選取單元格
titleRange.Merge(true);//合并單元格
titleRange.Value2 = strTitle; //設(shè)置單元格內(nèi)文本
titleRange.Font.Name = "宋體";//設(shè)置字體
titleRange.Font.Size = 18;//字體大小
titleRange.Font.Bold = true;//加粗顯示
titleRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
titleRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
titleRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//設(shè)置邊框
titleRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//邊框常規(guī)粗細(xì)
//設(shè)置表頭
string[] strHead = new string[columnCount] { "序號(hào)", "分組", "頻數(shù)", "相對(duì)頻數(shù)" };
int[] columnWidth = new int[4] { 8, 16, 8, 10 };
for (int i = 0; i < columnCount; i++)
{
Excel.Range headRange = worksheet.Cells[2, i + 1] as Excel.Range;//獲取表頭單元格
headRange.Value2 = strHead[i];//設(shè)置單元格文本
headRange.Font.Name = "宋體";//設(shè)置字體
headRange.Font.Size = 12;//字體大小
headRange.Font.Bold = true;//加粗顯示
headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
headRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
headRange.ColumnWidth = columnWidth[i];//設(shè)置列寬
headRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//設(shè)置邊框
headRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//邊框常規(guī)粗細(xì)
}
//設(shè)置每列格式
for (int i = 0; i < columnCount; i++)
{
Excel.Range contentRange = worksheet.get_Range(worksheet.Cells[3, i+1], worksheet.Cells[rowCount-1 + 3, i+1]);
contentRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
contentRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
contentRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//設(shè)置邊框
contentRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//邊框常規(guī)粗細(xì)
contentRange.WrapText = true;//自動(dòng)換行
contentRange.NumberFormatLocal = "@";//文本格式
}
//填充數(shù)據(jù)
for (int i = nMin; i <= nMax; i++)
{
int k = i - nMin;
excelApp.Cells[k + 3, 1] = string.Format("{0}", k + 1);
excelApp.Cells[k + 3, 2] = string.Format("{0}-{1}", i - 0.5, i + 0.5);
excelApp.Cells[k + 3, 3] = string.Format("{0}", subordinativeValues[k].count);
excelApp.Cells[k + 3, 4] = string.Format("{0:0.0000}", subordinativeValues[k].frequency);
}
//設(shè)置Excel可見(jiàn)
excelApp.Visible = true;
}
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/zhaobisha/archive/2009/02/04/3862568.aspx
|
|
來(lái)自: Frank_Chia > 《.NET》