C#操作Excell常用方法


這是在博客園看的文章,寫的很好,所以轉一下,方便自己以后用到的時候可以隨時查看。

操作range類需要在項目中引用兩個dll,不然點不出來,分別是:

microsoft office XX.0 Object Library

microsoft excelXX.0 Object Library

----------------------------------------------------------------------分割線

range.NumberFormatLocal = "@";     //設置單元格格式為文本


range = (Range)worksheet.get_Range("A1", "E1");     //獲取Excel多個單元格區域:本例做為Excel表頭

range.Merge(0);     //單元格合並動作

worksheet.Cells[1, 1] = "Excel單元格賦值";     //Excel單元格賦值

range.Font.Size = 15;     //設置字體大小

range.Font.Underline=true;     //設置字體是否有下划線

range.Font.Name="黑體";      設置字體的種類

range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //設置字體在單元格內的對其方式

range.ColumnWidth=15;     //設置單元格的寬度

range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //設置單元格的背景色

range.Borders.LineStyle=1;     //設置單元格邊框的粗細

range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //給單元格加邊框

range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //設置單元格上邊框為無邊框

range.EntireColumn.AutoFit();     //自動調整列寬

Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式

Range.VerticalAlignment= xlCenter     //文本垂直居中方式

Range.WrapText=true;     //文本自動換行

Range.Interior.ColorIndex=39;     //填充顏色為淡紫色

Range.Font.Color=clBlue;     //字體顏色

xlsApp.DisplayAlerts=false;     //保存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;
using System.IO;
namespace ExcalDemo
{
    public class ExcelFiles
    {
        public void CreateExcelFiles()
        {
            ExcelApplication excel = new ExcelApplication();
            try
            {
                excel.Visible = false;// 不顯示 Excel 文件,如果為 true 則顯示 Excel 文件
                excel.Workbooks.Add(Missing.Value);// 添加工作簿
                Worksheet sheet = (Worksheet)excel.ActiveSheet;// 獲取當前工作表
                Range range = null;// 創建一個空的單元格對象
               
                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(Path.Combine(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 實例設置為空
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM