Office Excel API (三) Worksheet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace OfficeUtility
{
    /// <summary>
    /// Worksheet related
    /// </summary>
    public class ExcelWorksheetUtil : IDisposable
    {
        /// <summary>
        /// Get Worksheet accoring to the worksheet name
        /// </summary>
        /// <param name="name"></param>
        /// <param name="workbook"></param>
        /// <returns></returns>
        public static Worksheet GetWorksheet(string name, Workbook workbook)
        {
            foreach (Worksheet ws in workbook.Worksheets)
            {
                if (ws.Name == name)
                {
                    return ws;
                }
            }
            return null;
        }

        /// <summary>
        /// Get the last used row number
        /// </summary>
        /// <param name="worksheet">The worksheet which is working on</param>
        /// <returns>The last uesed row number</returns>
        public int GetLastUsedRow(Worksheet worksheet)
        {
            return worksheet.UsedRange.Row + worksheet.UsedRange.Rows.Count - 1;
        }

        /// <summary>
        /// Get the last used column number
        /// </summary>
        /// <param name="worksheet">The worksheet which is working on</param>
        /// <returns>The last used column number</returns>
        public int GetLastUsedCol(Worksheet worksheet)
        {
            return worksheet.UsedRange.Column + worksheet.UsedRange.Columns.Count - 1;
        }

        /// <summary>
        /// Print excel worksheet
        /// </summary>
        /// <param name="worksheet">The worksheet which will be printed</param>
        /// <param name="printerName">The printer which will be used to print the worksheet</param>
        /// <param name="pageOrientation">The page orientation</param>
        public static void PrintWorksheet(Worksheet worksheet, string printerName, XlPageOrientation pageOrientation)
        {
            worksheet.Activate();
            if (!string.IsNullOrEmpty(printerName))
            {
                worksheet.PageSetup.Orientation = pageOrientation;
                worksheet.PrintOut(Missing.Value,
                                     Missing.Value,
                                     1,
                                     Missing.Value,
                                     printerName,
                                     Missing.Value,
                                     Missing.Value,
                                     Missing.Value);
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值