轉載於https://blog.csdn.net/lp244392323/article/details/7075587
C# 中使用 Excel
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass;
namespace ExcalDemo
{
public class ExcelFiles
{
public void CreateExcelFiles()
{
//創建一個 Excel 實例
ExcelApplication excel = new ExcelApplication();
try
{
// 注釋掉的語句是:從磁盤指定位置打開一個 Excel 文件
//excel.Workbooks.Open("demo.xls", Missing.Value, Missing.Value,
//Missing.Value,Missing.Value, Missing.Value, Missing.Value,
//Missing.Value, Missing.Value, Missing.Value, Missing.Value,
//Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.Visible = false;// 不顯示 Excel 文件,如果為 true 則顯示 Excel 文件
excel.Workbooks.Add(Missing.Value);// 添加工作簿
Worksheet sheet = (Worksheet)excel.ActiveSheet;// 獲取當前工作表
Range range = null;// 創建一個空的單元格對象
sheet.get_Range(sheet.Cells[29,2],sheet.Cells[29,2]).Orientation=Excel.XlOrientation.xlVertical;//字體豎直居中在單元格內
range = sheet.get_Range("A1", Missing.Value);// 獲取單個單元格
range.RowHeight = 20; // 設置行高
range.ColumnWidth = 20; // 設置列寬
range.Borders.LineStyle = 1; // 設置單元格邊框
range.Font.Bold = true; // 加粗字體
range.Font.Size = 20; // 設置字體大小
range.Font.ColorIndex = 5; // 設置字體顏色
range.Interior.ColorIndex = 6; // 設置單元格背景色
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設置單元格水平居中
range.VerticalAlignment = XlVAlign.xlVAlignCenter;// 設置單元格垂直居中
range.Value2 = "設置行高和列寬";// 設置單元格的值
range = sheet.get_Range("B2", "D4");// 獲取多個單元格
range.Merge(Missing.Value); // 合並單元格
range.Columns.AutoFit(); // 設置列寬為自動適應
range.NumberFormatLocal = "#,##0.00";// 設置單元格格式為貨幣格式
// 設置單元格左邊框加粗
range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
// 設置單元格右邊框加粗
range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
range.Value2 = "合並單元格";
// 頁面設置
sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; // 設置頁面大小為A4
sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 設置垂直版面
sheet.PageSetup.HeaderMargin = 0.0; // 設置頁眉邊距
sheet.PageSetup.FooterMargin = 0.0; // 設置頁腳邊距
sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 設置左邊距
sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 設置右邊距
sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575); // 設置上邊距
sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 設置下邊距
sheet.PageSetup.CenterHorizontally = true; // 設置水平居中
// 打印文件
sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// 保存文件到程序運行目錄下
sheet.SaveAs(System.Windows.Forms.Application.StartupPath + "\demo.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.ActiveWorkbook.Close(false, null, null); // 關閉 Excel 文件且不保存
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit(); // 退出 Excel
excel = null; // 將 Excel 實例設置為空
// 即便是這樣, 有時在系統任務管理器中仍能看到一個 Excel 進程, 但有時也看不到
// 據非權威人事說,這是 Office 2003 的 Bug. 這個問題有待與考證.
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit(); // 退出 Excel
excel = null; // 將 Excel 實例設置為空
// 即便是這樣, 有時在系統任務管理器中仍能看到一個 Excel 進程, 但有時也看不到
// 據非權威人事說,這是 Office 2003 的 Bug. 這個問題有待與考證.
}
}
}
}
range = sheet.get_Range("B2", "D4");// 獲取多個單元格
range.Merge(Missing.Value); // 合並單元格
range.Columns.AutoFit(); // 設置列寬為自動適應
range.NumberFormatLocal = "#,##0.00";// 設置單元格格式為貨幣格式
// 設置單元格左邊框加粗
range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
// 設置單元格右邊框加粗
range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
range.Value2 = "合並單元格";
// 頁面設置
sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; // 設置頁面大小為A4
sheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 設置垂直版面
sheet.PageSetup.HeaderMargin = 0.0; // 設置頁眉邊距
sheet.PageSetup.FooterMargin = 0.0; // 設置頁腳邊距
sheet.PageSetup.LeftMargin = excel.InchesToPoints(0.354330708661417); // 設置左邊距
sheet.PageSetup.RightMargin = excel.InchesToPoints(0.354330708661417);// 設置右邊距
sheet.PageSetup.TopMargin = excel.InchesToPoints(0.393700787401575); // 設置上邊距
sheet.PageSetup.BottomMargin = excel.InchesToPoints(0.393700787401575);// 設置下邊距
sheet.PageSetup.CenterHorizontally = true; // 設置水平居中
// 打印文件
sheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// 保存文件到程序運行目錄下
sheet.SaveAs(System.Windows.Forms.Application.StartupPath + "\demo.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.ActiveWorkbook.Close(false, null, null); // 關閉 Excel 文件且不保存
}
運行環境: Microsoft Office Excel 2003 + Microsoft Visual Studio 2005
1. 為項目添加 COM 組件的引用
方法如下:
項目 -> 添加引用 -> com -> Microsoft Excel Object 11.0 Object Library -> 確定
此時項目里會增加 3 個引用, 分別是: Excel、Microsoft.Office.Core、VBIDE
2. 在類文件里添加如下命名空間的引用
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.ApplicationClass; //為了與 System.Windows.Forms 命名空間下的 Application 類有所區分, 純屬方便代碼的編寫.
注: 最好不要用在窗體類中使用. 因為,當引入 Microsoft.Office.Interop.Excel 命名空間后, Microsoft.Office.Interop.Excel 命名空間下的 Application 接口會同 System.Windows.Forms 命名空間下的 Application 類產生二義性沖突. 要使用 Application 類, 就必須在 Application 前面添加完整的命名空間名.
經過以上 2 步的操作就可以使用 Excel 了. 但是, 關於 Excel 的 MSDN 很少, 多數都是 English 的. 這樣對使用 Excel 帶來了不便. 想調整單元格的大小都十分困難. 這里我告訴大家一個簡單實用的笨方法. 那就是: 打開你的 Excel, 錄制宏. 然后根據生成的 VB 代碼, 去到 MSDN 里查相關 C# 的幫助.
</article>